0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 20:30:23 +00:00

Merge branch 'websocket-example' of https://github.com/pvieytes/cowboy

This commit is contained in:
Loïc Hoguin 2013-01-30 14:50:21 +01:00
commit cc52343e9b
11 changed files with 263 additions and 1 deletions

View 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.

View 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, []}
]}.

View 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).

View 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.

View 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}}.

View 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.