Skip to content

Commit

Permalink
index
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Sep 5, 2024
1 parent 73dbc24 commit d74053c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lib/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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), '/')))
Expand Down
16 changes: 7 additions & 9 deletions lib/login.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 6 additions & 13 deletions lib/ws.ex
Original file line number Diff line number Diff line change
@@ -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}
Expand Down

0 comments on commit d74053c

Please sign in to comment.