1234567891011121314151617181920212223242526272829303132 |
- -module(n4u_xhr).
- -behaviour(cowboy_handler).
- -include_lib("n4u/include/n4u.hrl").
- % stream bridge to XHR
- % cowboy websocket backend
- -export([init/2, handle/2, info/3, terminate/3]).
- % XHR
- init(R, O) -> xhr(n4u_proto:init([], R, [{formatter, json}|O], xhr)).
- 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).
- body({ok, D, R2}, S) -> xhr(n4u_proto:stream({type(D), D}, R2, S));
- body(R, S) -> {ok, R, S}.
- 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}.
- reply(D, R, Code) -> cowboy_req:reply(Code, [], D, R). % R2.
|