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 ff20b4d commit 641475c
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 57 deletions.
9 changes: 1 addition & 8 deletions lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,10 @@ defmodule Sample.Application do
end
end

def env(_app) do
[
{:port, 8002}
]
end

def start(_, _) do
opts = [ strategy: :one_for_one, name: Sample.Supervisor]
children = [ { Bandit, scheme: :http, plug: Sample.Static, port: 8004 },
{ Bandit, scheme: :http, plug: Sample.WS, port: 8002 } ]
opts = [strategy: :one_for_one, name: Sample.Supervisor]
# :cowboy.start_clear(:http, env(:sample), %{env: %{dispatch: :n2o_cowboy.points()}})
:kvs.join()
Supervisor.start_link(children, strategy: :one_for_one, name: Sample.Supervisor)
end
Expand Down
21 changes: 0 additions & 21 deletions lib/routes.ex

This file was deleted.

3 changes: 1 addition & 2 deletions lib/static.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
defmodule Sample.Static do
use Plug.Router
plug Plug.Static, from: {:sample, "priv/static"}, at: "/app"
match _ do send_resp(conn, 404,
"Please refer to https://n2o.dev for information about endpoints addresses.") end
match _ do send_resp(conn, 404, "Please refer to https://n2o.dev for more information.") end
end
36 changes: 12 additions & 24 deletions lib/ws.ex
Original file line number Diff line number Diff line change
@@ -1,37 +1,25 @@
defmodule Sample.WS do

use Plug.Router
require N2O
plug :match
plug :dispatch

get "/" do send_resp(conn, 200, "NONE") end
get "/ws/app/index.htm" do conn |> WebSockAdapter.upgrade(Sample.WS, [], timeout: 60_000) |> halt() end
get "/ws/app/login.htm" do conn |> WebSockAdapter.upgrade(Sample.WS, [], timeout: 60_000) |> halt() end
def extract("index" <> __), do: Sample.Index
def extract("login" <> __), do: Sample.Login

def init(args) do {:ok, []} end
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 handle_in({"N2O," <> key = message, options}, state) do
{_,m,r,s} = :n2o_proto.stream(message,[],state)
:io.format 'N2O: ~p~n', [key]
{:reply, :ok, m, s}
end

def handle_in({"PING", options}, state) do
:io.format 'PING~n'
{:reply, :ok, {:text, "PONG"}, state}
end

def handle_in({message, options}, state) do
:io.format 'Binary: ~p~n', [message]
{x,m,r,s} = :n2o_proto.stream(message,[],state)
:io.format 'Processed: ~p~n', [{x,m,r,s}]
response({x,m,r,s})
end
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), do: response(:n2o_proto.stream(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}
def response({:reply,{:bert,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_bert.encode(rep)},s}
def response({:reply,{:json,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_json.encode(rep)},s}
def response({:reply,{:text,rep},_,s}), do: {:reply,:ok,{:text,rep},s}
def response({:reply,{:bert,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_bert.encode(rep)},s}
def response({:reply,{:json,rep},_,s}), do: {:reply,:ok,{:binary,:n2o_json.encode(rep)},s}
def response({:reply,{encoder,rep},_,s}), do: {:reply,:ok,{:binary,encoder.encode(rep)},s}

match _ do send_resp(conn, 404, "Please refer to https://n2o.dev for more information.") end
Expand Down
2 changes: 1 addition & 1 deletion priv/static/index.htm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2 id="heading">room</h2>
<script src="https://ws.n2o.dev/priv/heart.js"></script>
<script src="https://ws.n2o.dev/priv/ieee754.js"></script>
<script src="https://ws.n2o.dev/priv/n2o.js"></script>
<script>host = location.hostname; port = 8002; debug = true;</script>
<script>host = location.hostname; port = 8002; debug = true; persistent = true;</script>
<script src="https://ws.n2o.dev/priv/ftp.js"></script>
<script src="https://nitro.n2o.dev/priv/js/nitro.js"></script>
<script>$io.do = function(r) { console.log(r); };</script>
Expand Down
2 changes: 1 addition & 1 deletion priv/static/login.htm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<script src="https://ws.n2o.dev/priv/heart.js"></script>
<script src="https://ws.n2o.dev/priv/ieee754.js"></script>
<script src="https://ws.n2o.dev/priv/n2o.js"></script>
<script>host = location.hostname; port = 8002; debug = true;</script>
<script>host = location.hostname; port = 8002; debug = true; persistent = true;</script>
<script src="https://ws.n2o.dev/priv/ftp.js"></script>
<script src="https://nitro.n2o.dev/priv/js/nitro.js"></script>
<script>$io.do = function(r) { console.log(r); };</script>
Expand Down

0 comments on commit 641475c

Please sign in to comment.