diff --git a/lib/index.ex b/lib/index.ex index e9c5d1a..ab59afa 100644 --- a/lib/index.ex +++ b/lib/index.ex @@ -12,7 +12,7 @@ defmodule Sample.Index do event({:client, {user, message}}) end) end - def event(:logout), do: :n2o.user([]) ; :nitro.redirect("/app/login.htm") + def event(:logout) do :n2o.user([]) ; :nitro.redirect("/app/login.htm") end def event(:chat), do: chat(:nitro.q(:message)) def event(N2O.ftp(sid: s, filename: f, status: {:event, :stop})) do name = hd(:lists.reverse(:string.tokens(:nitro.to_list(f), '/'))) diff --git a/lib/login.ex b/lib/login.ex index 2ac31c4..06e9196 100644 --- a/lib/login.ex +++ b/lib/login.ex @@ -4,18 +4,16 @@ defmodule Sample.Login do require Logger def event(:init) do - :io.format 'LOGIN INIT' - login_button = NITRO.button(id: :loginButton, body: "HELO", postback: :login, source: [:user, :room]) - :nitro.update(:loginButton, login_button) + login_button = NITRO.button(id: :loginButton, body: "HELO", postback: :login, source: [:user, :room]) + :nitro.update(:loginButton, login_button) end def event(:login) do - :io.format 'LOGIN PRESSED' - user = :nitro.to_list(:nitro.q(:user)) - room = :nitro.to_binary(:nitro.q(:room)) - :n2o.user(user) - :n2o.session(:room, room) - :nitro.redirect(["/app/index.htm?room=", room]) + user = :nitro.to_list(:nitro.q(:user)) + room = :nitro.to_binary(:nitro.q(:room)) + :n2o.user(user) + :n2o.session(:room, room) + :nitro.redirect(["/app/index.htm?room=", room]) end def event(unexpected) do diff --git a/lib/ws.ex b/lib/ws.ex index 8910564..ec4ccc4 100644 --- a/lib/ws.ex +++ b/lib/ws.ex @@ -1,26 +1,19 @@ defmodule Sample.WS do - - use Plug.Router require N2O + use Plug.Router plug :match plug :dispatch + get "/ws/app/:mod", do: conn |> WebSockAdapter.upgrade(Sample.WS, [module: extract(mod)], timeout: 60_000) |> halt() + def extract("index" <> __), do: Sample.Index def extract("login" <> __), do: Sample.Login - get "/", do: send_resp(conn, 200, "NONE") - get "/ws/app/:mod", do: conn |> WebSockAdapter.upgrade(Sample.WS, [module: extract(mod)], timeout: 60_000) |> halt() - def init(args), do: {:ok, N2O.cx(module: Keyword.get(args, :module)) } def handle_in({"N2O," <> _ = message, _}, state), do: response(:n2o_proto.stream({:text,message},[],state)) - def handle_in({"PING", _}, state), do: {:reply, :ok, {:text, "PONG"}, state} - def handle_in({message, _}, state) when is_binary(message) do - bin = :erlang.binary_to_term(message) - :io.format 'Message: ~p~n', [bin] - x = :n2o_proto.stream({:binary,message},[],state) - :io.format 'X: ~p~n', [x] - response(x) - end + def handle_in({"PING", _}, state), do: {:reply, :ok, {:text, "PONG"}, state} + def handle_in({message, _}, state) when is_binary(message), do: response(:n2o_proto.stream({:binary,message},[],state)) + def handle_info(message, state), do: response(:n2o_proto.info(message,[],state)) def response({:reply,{:binary,rep},_,s}), do: {:reply,:ok,{:binary,rep},s} def response({:reply,{:text,rep},_,s}), do: {:reply,:ok,{:text,rep},s}