Packages
riptide
0.4.0
0.5.2
0.5.1
0.5.0-beta9
0.5.0-beta8
0.5.0-beta7
0.5.0-beta6
0.5.0-beta5
0.5.0-beta4
0.5.0-beta3
0.5.0-beta2
0.5.0-beta11
0.5.0-beta10
0.5.0-beta
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.13
0.3.12
0.3.11
0.3.10
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-bd63a38
0.2.79
0.2.78
0.2.74
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
A data first framework for building realtime applications
Current section
Files
Jump to
Current section
Files
lib/riptide/connection/server.ex
defmodule Riptide.Websocket.Server do
@moduledoc false
@behaviour :cowboy_websocket
def start_link(opts) do
opts =
[port: 12_000]
|> Keyword.merge(opts)
|> Enum.into(%{})
:cowboy.start_clear(:http, [{:port, opts.port}], %{
env: %{
dispatch:
:cowboy_router.compile([
{
:_,
[
{"/socket", __MODULE__, opts},
{"/", Riptide.Websocket.OK, []}
]
}
])
}
})
end
def init(req, state) do
{
:cowboy_websocket,
req,
Riptide.Processor.init(state),
%{
compress: true,
idle_timeout: :timer.hours(24)
}
}
end
def websocket_handle({:text, msg}, state) do
case Riptide.Processor.process_data(msg, state) do
{:reply, val, next} -> {:reply, {:text, val}, next}
{:noreply, state} -> {:ok, state}
end
end
def websocket_handle(:ping, state) do
{:reply, :pong, state}
end
def handle_info({:riptide_call, action, body, from}, state) do
{:reply, data, next} = Riptide.Processor.send_call(action, body, from, state)
{:reply, {:text, data}, next}
end
def handle_info({:riptide_cast, action, body}, state) do
{:reply, data, next} = Riptide.Processor.send_cast(action, body, state)
{:reply, {:text, data}, next}
end
def websocket_info(msg, state) do
case Riptide.Processor.process_info(msg, state) do
{:reply, val, next} -> {:reply, {:text, val}, next}
{:noreply, state} -> {:ok, state}
end
end
def child_spec(opts) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]}
}
end
end
defmodule Riptide.Websocket.OK do
def init(req, state) do
handle(req, state)
end
def handle(request, state) do
reply =
:cowboy_req.reply(
200,
request
)
{:ok, reply, state}
end
def terminate(_reason, _request, _state), do: :ok
end