Browse Source

n2o_stream to n4u_stream (cowboy 1.0.1 to 2.9.0)

221V 3 years ago
parent
commit
b57eca0dce
3 changed files with 43 additions and 46 deletions
  1. 1 1
      ebin/n4u.app
  2. 0 45
      src/endpoints/cowboy/n2o_stream.erl
  3. 42 0
      src/endpoints/cowboy/n4u_stream.erl

+ 1 - 1
ebin/n4u.app

@@ -2,7 +2,7 @@
   {description, "N4U WebSocket Application Server"},
   {vsn, "4.4.20"},
   {applications, [kernel, stdlib, asn1, public_key, ssl, crypto, ranch, cowboy, fs, active, sh, gproc, nitro]},
-  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n4u_xhr, n4u_cx, n4u_cowboy, n2o_multipart, n4u_static, n2o_stream, n4u_document, n4u_proto, n4u_relay, n4u_error, n4u_io, n4u_log, n4u_mq, n4u_pickle, n4u_query, n4u_secret, n4u_session, n4u_syn, n4u_client, n4u_file, n4u_heart, n4u_http, n4u_nitrogen, n4u_text, wf, wf_convert, wf_utils]},
+  {modules, [n2o, n4u_app, n4u_sup, n4u_async, n4u_xhr, n4u_cx, n4u_cowboy, n2o_multipart, n4u_static, n4u_stream, n4u_document, n4u_proto, n4u_relay, n4u_error, n4u_io, n4u_log, n4u_mq, n4u_pickle, n4u_query, n4u_secret, n4u_session, n4u_syn, n4u_client, n4u_file, n4u_heart, n4u_http, n4u_nitrogen, n4u_text, wf, wf_convert, wf_utils]},
   {registered, [n4u_sup]},
   {mod, {n4u_app, []}},
   {env, []}

+ 0 - 45
src/endpoints/cowboy/n2o_stream.erl

@@ -1,45 +0,0 @@
--module(n2o_stream).
--description('N4U Stream Bridge to WebSocket or XHR channels').
--behaviour(cowboy_http_handler).
--behaviour(cowboy_websocket_handler).
--export([init/3,handle/2,info/3,terminate/3]).
--export([websocket_init/3,websocket_handle/3,websocket_info/3,websocket_terminate/3]).
-
-% XHR
-
-init(T,R,O)                        -> upgrade(cowboy_req:header(<<"upgrade">>,R),{T,R,O}).
-websocket(_R,<<"websocket">>)       -> {upgrade, protocol, cowboy_websocket};
-websocket(R,_)                     -> down(reply([],R,501)).
-upgrade({undefined,R},{T,R,O})     -> initialize(T,R,O);
-upgrade({B,R},_) when is_binary(B) -> websocket(R,cowboy_bstr:to_lower(B)).
-
-handle(R,S)                  -> body(cowboy_req:body(R),S).
-info(M,R,S)                  -> xhr(n4u_proto:info(M,R,S)).
-terminate(_,R,S)             -> n4u_proto:terminate(R,S).
-initialize(T,R,O)            -> xhr(n4u_proto:init(T,R,[{formatter,json}|O],xhr)).
-
-body({ok,D,R2},S)            -> xhr(n4u_proto:stream({type(D),D},R2,S));
-body(R,S)                    -> {ok,R,S}.
-down(R)                      -> {shutdown,R,undefined}.
-reply(D,R,Code)              -> {ok,R2}=cowboy_req:reply(Code,[],D,R), R2.
-
-type(<<"N4U,",_/binary>>)    -> text;
-type(<<"PING">>)             -> text;
-type(_)                      -> binary.
-
-xhr({ok,R,S})                -> {ok,R,S};
-xhr({shutdown,R,S})          -> {shutdown,R,S};
-xhr({reply,D,R,S})           -> {ok,reply(D,R,200),S}.
-
-% WebSocket
-
-websocket_info(I,R,S)        -> ws(n4u_proto:info(I,R,S)).
-websocket_handle(D,R,S)      -> ws(n4u_proto:stream(D,R,S)).
-%websocket_handle(_,R,S)      -> {ok,R,S,hibernate}.
-websocket_init(T,R,O)        -> ws(n4u_proto:init(T,R,[{formatter,bert}|O],ws)).
-websocket_terminate(_,R,S)   -> n4u_proto:terminate(R,S).
-
-ws({ok,R,S})                 -> {ok,R,S,hibernate};
-ws({shutdown,R,S})           -> {shutdown,R,S};
-ws({reply,{binary,Rep},R,S}) -> {reply,{binary,Rep},R,S,hibernate};
-ws({reply,Rep,R,S})          -> {reply,{text,Rep},R,S,hibernate}.

+ 42 - 0
src/endpoints/cowboy/n4u_stream.erl

@@ -0,0 +1,42 @@
+-module(n4u_stream).
+-behaviour(cowboy_websocket).
+
+-include_lib("n4u/include/n4u.hrl").
+
+-export([init/2, websocket_init/1, websocket_handle/2, websocket_info/2, terminate/3]).
+
+
+% N4U Stream Bridge to WebSocket
+
+
+init(R, S)                                  -> upgrade(cowboy_req:header(<<"upgrade">>, R), {R, S}).
+websocket(R, <<"websocket">>, _S) ->
+  %wf:info(?MODULE, "websocket: ~p~n ~p~n", [R, S]),
+  {cowboy_websocket, R, R}; %{cowboy_websocket, R, S};
+websocket(R, _, _S)                         -> down(reply([], R, 501)).
+upgrade(undefined, {R, _S})                 -> down(reply([], R, 501));
+upgrade(B, {R, S}) when erlang:is_binary(B) -> websocket(R, cowboy_bstr:to_lower(B), S).
+
+down(R)                    -> {shutdown, R, undefined}.
+reply(D, R, Code)          -> cowboy_req:reply(Code, [], D, R). % R2.
+
+
+% WebSocket
+
+websocket_init(S)      -> ws(n4u_proto:init([], S, [{formatter, bert}], ws)).
+websocket_handle(D, S) ->
+  %wf:info(?MODULE, "websocket_handle: ~p~n~n ~p~n~n", [S#cx.req, S]),
+  ws(n4u_proto:stream(D, S#cx.req, S)).
+websocket_info(D, S)   -> ws(n4u_proto:info(D, ?REQ, S)).
+terminate(_M, R, S)    ->
+  wf:info(?MODULE, "terminate: ~p~n", [_M]),
+  n4u_proto:terminate(R, S).
+
+ws({ok, _R, S})                    -> {ok, S};
+ws({shutdown, _R, S})              -> {shutdown, S};
+ws({reply, {binary, Rep}, _R, S})  -> {reply, {binary, Rep}, S};
+ws({reply, Rep, _R, S})            -> {reply, {text, Rep}, S};
+ws(X) ->
+  wf:info(?MODULE, "ws: UNKNOWN: ~p~n", [X]),
+  {shutdown, []}.
+