Skip to content

Instantly share code, notes, and snippets.

@shufo
Created November 14, 2018 14:54
Show Gist options
  • Save shufo/542954e124630dac5b7fdfd76aa90550 to your computer and use it in GitHub Desktop.
Save shufo/542954e124630dac5b7fdfd76aa90550 to your computer and use it in GitHub Desktop.
Websocket handler for Cowboy 2
defmodule MyApp.WebSocketHandlerCowboy2 do
def init(req, state) do
opts = %{idle_timeout: 60000}
{:cowboy_websocket, req, state, opts}
end
def websocket_init(state) do
{:ok, state}
end
def terminate(_reason, _req, _state) do
:ok
end
def websocket_handle({:text, message}, state) do
{:reply, {:text, message}, state}
end
def websocket_handle(_data, state) do
{:ok, state}
end
def websocket_info({:timeout, _ref, message}, state) do
{:reply, {:text, message}, state}
end
def websocket_info({:broadcast, message}, state) do
{:reply, {:text, message}, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment