Created
November 14, 2018 14:54
-
-
Save shufo/542954e124630dac5b7fdfd76aa90550 to your computer and use it in GitHub Desktop.
Websocket handler for Cowboy 2
This file contains 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 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