mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
add websocket example
This commit is contained in:
parent
d9b3727a62
commit
b8a0a8963b
11 changed files with 263 additions and 0 deletions
24
examples/websocket/src/toppage_handler.erl
Normal file
24
examples/websocket/src/toppage_handler.erl
Normal file
|
@ -0,0 +1,24 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
-module(toppage_handler).
|
||||
|
||||
-export([init/3]).
|
||||
-export([handle/2]).
|
||||
-export([terminate/3]).
|
||||
|
||||
init(_Transport, Req, []) ->
|
||||
{ok, Req, undefined}.
|
||||
|
||||
handle(Req, State) ->
|
||||
Html = get_html(),
|
||||
{ok, Req2} = cowboy_req:reply(200, [], Html, Req),
|
||||
{ok, Req2, State}.
|
||||
|
||||
terminate(_Reason, _Req, _State) ->
|
||||
ok.
|
||||
|
||||
get_html() ->
|
||||
{ok, Cwd} = file:get_cwd(),
|
||||
Filename =filename:join([Cwd, "priv", "html_ws_client.html"]),
|
||||
{ok, Binary} = file:read_file(Filename),
|
||||
Binary.
|
15
examples/websocket/src/websocket.app.src
Normal file
15
examples/websocket/src/websocket.app.src
Normal file
|
@ -0,0 +1,15 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
{application, websocket, [
|
||||
{description, "Cowboy websocket example."},
|
||||
{vsn, "1"},
|
||||
{modules, []},
|
||||
{registered, []},
|
||||
{applications, [
|
||||
kernel,
|
||||
stdlib,
|
||||
cowboy
|
||||
]},
|
||||
{mod, {websocket_app, []}},
|
||||
{env, []}
|
||||
]}.
|
12
examples/websocket/src/websocket.erl
Normal file
12
examples/websocket/src/websocket.erl
Normal file
|
@ -0,0 +1,12 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
-module(websocket).
|
||||
|
||||
%% API.
|
||||
-export([start/0]).
|
||||
|
||||
start() ->
|
||||
ok = application:start(crypto),
|
||||
ok = application:start(ranch),
|
||||
ok = application:start(cowboy),
|
||||
ok = application:start(websocket).
|
24
examples/websocket/src/websocket_app.erl
Normal file
24
examples/websocket/src/websocket_app.erl
Normal file
|
@ -0,0 +1,24 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
%% @private
|
||||
-module(websocket_app).
|
||||
-behaviour(application).
|
||||
|
||||
%% API.
|
||||
-export([start/2]).
|
||||
-export([stop/1]).
|
||||
|
||||
%% API.
|
||||
start(_Type, _Args) ->
|
||||
Dispatch = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/", toppage_handler, []},
|
||||
{"/websocket", ws_handler, []}
|
||||
]}
|
||||
]),
|
||||
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}],
|
||||
[{env, [{dispatch, Dispatch}]}]),
|
||||
websocket_sup:start_link().
|
||||
|
||||
stop(_State) ->
|
||||
ok.
|
23
examples/websocket/src/websocket_sup.erl
Normal file
23
examples/websocket/src/websocket_sup.erl
Normal file
|
@ -0,0 +1,23 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
%% @private
|
||||
-module(websocket_sup).
|
||||
-behaviour(supervisor).
|
||||
|
||||
%% API.
|
||||
-export([start_link/0]).
|
||||
|
||||
%% supervisor.
|
||||
-export([init/1]).
|
||||
|
||||
%% API.
|
||||
|
||||
-spec start_link() -> {ok, pid()}.
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
%% supervisor.
|
||||
|
||||
init([]) ->
|
||||
Procs = [],
|
||||
{ok, {{one_for_one, 10, 10}, Procs}}.
|
29
examples/websocket/src/ws_handler.erl
Normal file
29
examples/websocket/src/ws_handler.erl
Normal file
|
@ -0,0 +1,29 @@
|
|||
-module(ws_handler).
|
||||
-behaviour(cowboy_websocket_handler).
|
||||
|
||||
-export([init/3]).
|
||||
-export([websocket_init/3]).
|
||||
-export([websocket_handle/3]).
|
||||
-export([websocket_info/3]).
|
||||
-export([websocket_terminate/3]).
|
||||
|
||||
init({tcp, http}, _Req, _Opts) ->
|
||||
{upgrade, protocol, cowboy_websocket}.
|
||||
|
||||
websocket_init(_TransportName, Req, _Opts) ->
|
||||
erlang:start_timer(1000, self(), <<"Hello!">>),
|
||||
{ok, Req, undefined_state}.
|
||||
|
||||
websocket_handle({text, Msg}, Req, State) ->
|
||||
{reply, {text, << "That's what she said! ", Msg/binary >>}, Req, State};
|
||||
websocket_handle(_Data, Req, State) ->
|
||||
{ok, Req, State}.
|
||||
|
||||
websocket_info({timeout, _Ref, Msg}, Req, State) ->
|
||||
erlang:start_timer(1000, self(), <<"How' you doin'?">>),
|
||||
{reply, {text, Msg}, Req, State};
|
||||
websocket_info(_Info, Req, State) ->
|
||||
{ok, Req, State}.
|
||||
|
||||
websocket_terminate(_Reason, _Req, _State) ->
|
||||
ok.
|
Loading…
Add table
Add a link
Reference in a new issue