Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/cowboy_elixir_example.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ defmodule CowboyElixirExample do
"""
def start(_type, _args) do
dispatch_config = build_dispatch_config
{ :ok, _ } = :cowboy.start_http(:http,
{ :ok, _ } = :cowboy.start_clear(
100,
[{:port, 8080}],
[{ :env, [{:dispatch, dispatch_config}]}]
%{env: %{dispatch: dispatch_config}}
)

end
Expand Down
31 changes: 18 additions & 13 deletions lib/websocket_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ defmodule WebsocketHandler do
@behaviour :cowboy_websocket

# We are using the regular http init callback to perform handshake.
# http://ninenines.eu/docs/en/cowboy/2.0/manual/cowboy_handler/
# http://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_handler/
#
# Note that handshake will fail if this isn't a websocket upgrade request.
# Also, if your server implementation supports subprotocols,
# init is the place to parse `sec-websocket-protocol` header
# then add the same header to `req` with value containing
# supported protocol(s).
def init(req, state) do
:erlang.start_timer(1000, self, [])
{:cowboy_websocket, req, state}
end

def websocket_init(state) do
:erlang.start_timer(1000, self(), [])

{:ok, state}
end

# Put any essential clean-up here.
def terminate(_reason, _req, _state) do
:ok
Expand All @@ -22,20 +27,20 @@ defmodule WebsocketHandler do
# websocket_handle deals with messages coming in over the websocket,
# including text, binary, ping or pong messages. But you need not
# handle ping/pong, cowboy takes care of that.
def websocket_handle({:text, content}, req, state) do
def websocket_handle({:text, content}, state) do

# Use JSEX to decode the JSON message and extract the word entered
# Use Poison to decode the JSON message and extract the word entered
# by the user into the variable 'message'.
{ :ok, %{ "message" => message} } = JSEX.decode(content)
{ :ok, %{ "message" => message} } = Poison.decode!(content)

# Reverse the message and use JSEX to re-encode a reply contatining
# Reverse the message and use Poison to re-encode a reply contatining
# the reversed message.
rev = String.reverse(message)
{ :ok, reply } = JSEX.encode(%{ reply: rev})
{ :ok, reply } = Poison.encode(%{ reply: rev})

# All websocket callbacks share the same return values.
# See http://ninenines.eu/docs/en/cowboy/2.0/manual/cowboy_websocket/
{:reply, {:text, reply}, req, state}
{:reply, {:text, reply}, state}
end

# Fallback clause for websocket_handle. If the previous one does not match
Expand All @@ -52,27 +57,27 @@ defmodule WebsocketHandler do
#
# In a larger app various clauses of websocket_info might handle all kinds
# of messages and pass information out the websocket to the client.
def websocket_info({_timeout, _ref, _msg}, req, state) do
def websocket_info({_timeout, _ref, _msg}, state) do

time = time_as_string()

# encode a json reply in the variable 'message'
{ :ok, message } = JSEX.encode(%{ time: time})
{ :ok, message } = Poison.encode(%{ time: time })

# set a new timer to send a :timeout message back to this
# process a second from now. This will recursively call
# this handler, acting as a tick.
:erlang.start_timer(1000, self, [])
:erlang.start_timer(1000, self(), [])

# send the new message to the client. Note that even though there was no
# incoming message from the client, we still call the outbound message
# a 'reply'. That makes the format for outbound websocket messages
# exactly the same as websocket_handle()
{ :reply, {:text, message}, req, state}
{ :reply, {:text, message}, state}
end

# fallback message handler
def websocket_info(_info, _req, state) do
def websocket_info(_info, state) do
{:ok, state}
end

Expand Down
8 changes: 5 additions & 3 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule CowboyElixirExample.Mixfile do
[app: :cowboy_elixir_example,
version: "0.0.3",
elixir: ">= 1.0.0",
deps: deps]
deps: deps()]
end

# Configuration for the OTP application
Expand All @@ -28,7 +28,9 @@ defmodule CowboyElixirExample.Mixfile do
#
# Type `mix help deps` for more examples and options
defp deps do
[ { :cowboy, github: "ninenines/cowboy", tag: "2.0.0-pre.3" },
{ :jsex, "~> 2.0.0" } ]
[
{ :cowboy, "~> 2.4.0" },
{ :poison, "~> 4.0.1" }
]
end
end
9 changes: 6 additions & 3 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
%{"cowboy": {:git, "https://github.com/ninenines/cowboy.git", "dbb636034f20736e16eb9d6c809217c9525b6cbd", [tag: "2.0.0-pre.3"]},
"cowlib": {:git, "https://github.com/ninenines/cowlib", "8cf98c9f3d1292c1b08fca5ca99772ae912eaa86", [ref: "master"]},
%{
"cowboy": {:hex, :cowboy, "2.4.0", "f1b72fabe9c8a5fc64ac5ac85fb65474d64733d1df52a26fad5d4ba3d9f70a9f", [:rebar3], [{:cowlib, "~> 2.3.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.5.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.3.0", "bbd58ef537904e4f7c1dd62e6aa8bc831c8183ce4efa9bd1150164fe15be4caa", [:rebar3], [], "hexpm"},
"jsex": {:hex, :jsex, "2.0.0", "d54a39e49418b34b1a78236a92f73e4d7c46329c220275d7223fab4b3dc6120f", [:mix], [{:jsx, "~> 2.0", [hex: :jsx, optional: false]}]},
"jsx": {:hex, :jsx, "2.0.4", "6f22bd27bf0cf89626caa27f93ed4bfbf7ba5cc9d42a43656f1d247b0e170eaa", [:rebar], []},
"ranch": {:git, "https://github.com/ninenines/ranch", "741bfb73ca663394ecf7a73410506d66f01985a6", [ref: "1.1.0"]}}
"poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.5.0", "f04166f456790fee2ac1aa05a02745cc75783c2bfb26d39faf6aefc9a3d3a58a", [:rebar3], [], "hexpm"},
}