From ff20b4d2c105a00ef8b669f88b4471de76d07d15 Mon Sep 17 00:00:00 2001 From: 5HT Date: Thu, 5 Sep 2024 15:04:10 +0300 Subject: [PATCH] ws bandit wip --- lib/application.ex | 4 ++-- lib/index.ex | 1 + lib/ws.ex | 38 ++++++++++++++++++++++++++++++++++++-- mix.exs | 4 ++-- 4 files changed, 41 insertions(+), 6 deletions(-) diff --git a/lib/application.ex b/lib/application.ex index 7298171..17b50c8 100644 --- a/lib/application.ex +++ b/lib/application.ex @@ -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 diff --git a/lib/index.ex b/lib/index.ex index e9c5d1a..4e89ef0 100644 --- a/lib/index.ex +++ b/lib/index.ex @@ -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 diff --git a/lib/ws.ex b/lib/ws.ex index 25ba17f..c9a13d0 100644 --- a/lib/ws.ex +++ b/lib/ws.ex @@ -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 diff --git a/mix.exs b/mix.exs index 71b818f..5fe4499 100644 --- a/mix.exs +++ b/mix.exs @@ -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 @@ -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"},