Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use of gun from elixir
defmodule Transport do
def connect(params) do
hostname = params.hostname
port = params.port
path = params.path
timeout = params.connection_timeout
{:ok, conn} = :gun.open(hostname, port)
{:ok, :http} = :gun.await_up(conn)
:gun.ws_upgrade(conn, path)
receive do
{:gun_ws_upgrade, ^conn, :ok, _} ->
established(conn)
after timeout ->
raise "timeout"
end
end
def established(conn) do
receive do
{:gun_down, ^conn, _, _, _, _} ->
IO.puts "Transport: received down event"
{:gun_ws, ^conn, {:binary, data}} ->
IO.puts "Transport: received: #{inspect data}"
established(state)
{:send, data} ->
IO.puts "Transport: sending frame #{inspect data}"
:gun.ws_send(conn, {:binary, data})
established(conn)
:shutdown ->
IO.puts "Transport: good bye!"
:gun.shutdown(conn)
_msg ->
IO.puts "Transport: unhandled message: #{inspect _msg}"
established(conn)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.