Add phoenix_gen_socket_client

parent 9ef10c28
......@@ -29,3 +29,5 @@ use Mix.Config
#
import_config "#{Mix.env}.exs"
config :logger, level: :info
defmodule GrainsEngine do
use Application
require Logger
@moduledoc """
Documentation for GrainsEngine.
"""
def start(_type, _args) do
Logger.info "Application starting"
import Supervisor.Spec, warn: false
children = [
worker(GrainsEngine.SocketClient, [])
]
opts = [strategy: :one_for_one, name: GrainsEngine.Supervisor]
Supervisor.start_link(children, opts)
end
end
defmodule GrainsEngine.SocketClient do
@moduledoc false
require Logger
alias Phoenix.Channels.GenSocketClient
@behaviour GenSocketClient
def start_link() do
Logger.info "SocketClient.start_link"
GenSocketClient.start_link(
__MODULE__,
Phoenix.Channels.GenSocketClient.Transport.WebSocketClient,
"ws://localhost:4000/socket/websocket?token=foobar"
)
end
def init(url) do
Logger.info "SocketClient.init #{url}"
{:connect, url, %{first_join: true, ping_ref: 1}}
end
def handle_connected(transport, state) do
Logger.info("connected")
GenSocketClient.join(transport, "ping")
{:ok, state}
end
def handle_disconnected(reason, state) do
Logger.error("disconnected: #{inspect reason}")
Process.send_after(self(), :connect, :timer.seconds(1))
{:ok, state}
end
def handle_joined(topic, _payload, _transport, state) do
Logger.info("joined the topic #{topic}")
if state.first_join do
:timer.send_interval(:timer.seconds(1), self(), :ping_server)
{:ok, %{state | first_join: false, ping_ref: 1}}
else
{:ok, %{state | ping_ref: 1}}
end
end
def handle_join_error(topic, payload, _transport, state) do
Logger.error("join error on the topic #{topic}: #{inspect payload}")
{:ok, state}
end
def handle_channel_closed(topic, payload, _transport, state) do
Logger.error("disconnected from the topic #{topic}: #{inspect payload}")
Process.send_after(self(), {:join, topic}, :timer.seconds(1))
{:ok, state}
end
def handle_message(topic, event, payload, _transport, state) do
Logger.warn("message on topic #{topic}: #{event} #{inspect payload}")
{:ok, state}
end
def handle_reply("ping", _ref, %{"status" => "ok"} = payload, _transport, state) do
Logger.info("server pong ##{payload["response"]["ping_ref"]}")
{:ok, state}
end
def handle_reply(topic, _ref, payload, _transport, state) do
Logger.warn("reply on topic #{topic}: #{inspect payload}")
{:ok, state}
end
def handle_info(:connect, _transport, state) do
Logger.info("connecting")
{:connect, state}
end
def handle_info({:join, topic}, transport, state) do
Logger.info("joining the topic #{topic}")
case GenSocketClient.join(transport, topic) do
{:error, reason} ->
Logger.error("error joining the topic #{topic}: #{inspect reason}")
Process.send_after(self(), {:join, topic}, :timer.seconds(1))
{:ok, _ref} -> :ok
end
{:ok, state}
end
def handle_info(:ping_server, transport, state) do
Logger.info("sending ping ##{state.ping_ref}")
GenSocketClient.push(transport, "ping", "ping", %{ping_ref: state.ping_ref})
{:ok, %{state | ping_ref: state.ping_ref + 1}}
end
def handle_info(message, _transport, state) do
Logger.warn("Unhandled message #{inspect message}")
{:ok, state}
end
end
......@@ -15,8 +15,8 @@ defmodule GrainsEngine.Mixfile do
# Type "mix help compile.app" for more information
def application do
# Specify extra applications you'll use from Erlang/Elixir
[extra_applications: [:logger, :ex_osc],
mod: {GrainsEngine.Application, []}]
[extra_applications: [:logger, :ex_osc, :phoenix_gen_socket_client, :websocket_client],
mod: {GrainsEngine, []}]
end
# Dependencies can be Hex packages:
......@@ -30,6 +30,9 @@ defmodule GrainsEngine.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[ {:ex_osc, git: "git://github.com/jwarwick/ex_osc.git"},
{:phoenix_gen_socket_client, "~> 1.0.0"},
{:websocket_client, github: "sanmiguel/websocket_client", tag: "1.1.0"},
{:poison, "~> 1.5.2"},
{:mix_test_watch, "~> 0.3", only: :dev, runtime: false}]
end
end
%{"ex_osc": {:git, "git://github.com/jwarwick/ex_osc.git", "2f695afc837670d022a90a7954a2cf53d023ad80", []},
"fs": {:hex, :fs, "2.12.0", "ad631efacc9a5683c8eaa1b274e24fa64a1b8eb30747e9595b93bec7e492e25e", [:rebar3], []},
"mix_test_watch": {:hex, :mix_test_watch, "0.3.3", "70859889a8d1d43d1b75d69d87258a301f43209a17787cdb2bd9cab42adf271d", [:mix], [{:fs, "~> 2.12", [hex: :fs, optional: false]}]}}
"mix_test_watch": {:hex, :mix_test_watch, "0.3.3", "70859889a8d1d43d1b75d69d87258a301f43209a17787cdb2bd9cab42adf271d", [:mix], [{:fs, "~> 2.12", [hex: :fs, optional: false]}]},
"phoenix_gen_socket_client": {:hex, :phoenix_gen_socket_client, "1.0.0", "621d9537ccaea71960de9da6483b03817646018471f5e1d5efd7fcc65b1af854", [:mix], []},
"poison": {:hex, :poison, "1.5.2", "560bdfb7449e3ddd23a096929fb9fc2122f709bcc758b2d5d5a5c7d0ea848910", [:mix], []},
"websocket_client": {:git, "https://github.com/sanmiguel/websocket_client.git", "16cd139a71d99813ec0c0cd1d8cb92dbde93d9c7", [tag: "1.1.0"]}}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment