- browse to localhost:5000
- open javascript console
ws = new WebSocket("ws://localhost:5000/my_websocket")
ws.onmessage = function(resp){console.log(resp.data)}
ws.send("Hey")
-
-
Save jeregrine/af7ecb4c7d87767411a0 to your computer and use it in GitHub Desktop.
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 Totem.Router do | |
use Phoenix.Router, port: 5000 | |
resources "/user", UserController | |
websocket "/my_websocket", Totem.SocketController | |
end | |
defmodule Totem.SocketController do | |
use Phoenix.Controller.Websocket | |
def start(_transport, socket, _state) do | |
:erlang.start_timer(1000, self(), "Hello!") | |
IO.puts "INIT" | |
:ok | |
end | |
def stream(_data, socket, _state) do | |
IO.puts "RECIEVED DATA" | |
reply socket, {:text, "Thats what she said!"} | |
end | |
def info({:timeout, _ref, msg}, socket, _state) do | |
IO.puts "TIMEOUT SENDING DATA" | |
reply socket, {:text, msg} | |
:erlang.start_timer(1000, self(), "How' you doin'?") | |
end | |
def closed(reason, _req, _state) do | |
IO.inspect reason | |
:ok | |
end | |
end |
I am so looking forward for this to be merged to master!
+1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically same example as here https://github.com/extend/cowboy/blob/master/examples/websocket/src/ws_handler.erl