Forked from ashneyderman/gist:5eca8b1bed16a7b6f4be
Created
September 15, 2015 21:52
-
-
Save dch/14946c1cc46cdde5acec to your computer and use it in GitHub Desktop.
Use of gun from elixir
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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