aboutsummaryrefslogtreecommitdiffstats
path: root/examples/websocket/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/websocket/src')
-rw-r--r--examples/websocket/src/toppage_handler.erl24
-rw-r--r--examples/websocket/src/websocket.app.src15
-rw-r--r--examples/websocket/src/websocket.erl12
-rw-r--r--examples/websocket/src/websocket_app.erl24
-rw-r--r--examples/websocket/src/websocket_sup.erl23
-rw-r--r--examples/websocket/src/ws_handler.erl29
6 files changed, 127 insertions, 0 deletions
diff --git a/examples/websocket/src/toppage_handler.erl b/examples/websocket/src/toppage_handler.erl
new file mode 100644
index 0000000..63099b0
--- /dev/null
+++ b/examples/websocket/src/toppage_handler.erl
@@ -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.
diff --git a/examples/websocket/src/websocket.app.src b/examples/websocket/src/websocket.app.src
new file mode 100644
index 0000000..4fa4f78
--- /dev/null
+++ b/examples/websocket/src/websocket.app.src
@@ -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, []}
+]}.
diff --git a/examples/websocket/src/websocket.erl b/examples/websocket/src/websocket.erl
new file mode 100644
index 0000000..da2a91c
--- /dev/null
+++ b/examples/websocket/src/websocket.erl
@@ -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).
diff --git a/examples/websocket/src/websocket_app.erl b/examples/websocket/src/websocket_app.erl
new file mode 100644
index 0000000..93598d0
--- /dev/null
+++ b/examples/websocket/src/websocket_app.erl
@@ -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.
diff --git a/examples/websocket/src/websocket_sup.erl b/examples/websocket/src/websocket_sup.erl
new file mode 100644
index 0000000..40ef8e0
--- /dev/null
+++ b/examples/websocket/src/websocket_sup.erl
@@ -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}}.
diff --git a/examples/websocket/src/ws_handler.erl b/examples/websocket/src/ws_handler.erl
new file mode 100644
index 0000000..bbbf716
--- /dev/null
+++ b/examples/websocket/src/ws_handler.erl
@@ -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.