Skip to content

Commit

Permalink
ws bandit wip
Browse files Browse the repository at this point in the history
  • Loading branch information
5HT committed Sep 5, 2024
1 parent faad942 commit ff20b4d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ defmodule Sample.Application do

def start(_, _) do
children = [ { Bandit, scheme: :http, plug: Sample.Static, port: 8004 },
{ Bandit, scheme: :http, plug: Sample.WS, port: 8003 } ]
{ 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()}})
# :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
1 change: 1 addition & 0 deletions lib/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ defmodule Sample.Index do
chat(:nitro.render(link))
end
def event({:client, {user, message}}) do
:io.format 'client'
:nitro.wire(NITRO.jq(target: :message, method: [:focus, :select]))
:nitro.insert_top(:history, NITRO.message(body: [NITRO.author(body: user), :nitro.jse(message)]))
end
Expand Down
38 changes: 36 additions & 2 deletions lib/ws.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,39 @@
defmodule Sample.WS do

use Plug.Router
match _ do send_resp(conn, 404,
"Please refer to https://n2o.dev for information about endpoints addresses.") end
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 init(args) do {:ok, []} end

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 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,{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

end
4 changes: 2 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ defmodule Sample.Mixfile do
def application() do
[
mod: {Sample.Application, []},
extra_applications: [:ranch, :rocksdb, :mnesia, :xmerl, :cowboy, :logger, :n2o, :kvs, :syn, :nitro]
extra_applications: [:rocksdb, :mnesia, :xmerl, :logger, :n2o, :kvs, :syn, :nitro]
]
end

Expand All @@ -34,7 +34,7 @@ defmodule Sample.Mixfile do
{:ex_doc, "~> 0.29.0", only: :dev},
{:plug, "~> 1.15.3"},
{:bandit, "~> 1.0"},
{:cowboy, "~> 2.8.0"},
{:websock_adapter, "~> 0.5"},
{:rocksdb, git: "https://github.com/emqx/erlang-rocksdb", branch: "master"},
{:nitro, "~> 8.2.4"},
{:kvs, "~> 10.8.3"},
Expand Down

0 comments on commit ff20b4d

Please sign in to comment.