Permalink
Browse files

add websockets example

  • Loading branch information...
1 parent 981ea35 commit a74fbd7c2b660ccae6cdedb381302ce340d163dc @RJ committed Sep 30, 2012
@@ -0,0 +1,5 @@
+Websockets example
+==================
+
+$ rebar compile
+$ ./start.sh
@@ -0,0 +1,15 @@
+%% Feel free to use, reuse and abuse the code in this file.
+
+{application, websockets, [
+ {description, "Cowboy Websockets example."},
+ {vsn, "1"},
+ {modules, []},
+ {registered, []},
+ {applications, [
+ kernel,
+ stdlib,
+ cowboy
+ ]},
+ {mod, {websockets_app, []}},
+ {env, []}
+]}.
@@ -0,0 +1,11 @@
+-module(websockets).
+
+%% API.
+-export([start/0]).
+
+%% API.
+
+start() ->
+ ok = application:start(ranch),
+ ok = application:start(cowboy),
+ ok = application:start(websockets).
@@ -0,0 +1,23 @@
+%% @private
+-module(websockets_app).
+-behaviour(application).
+
+%% API.
+-export([start/2]).
+-export([stop/1]).
+
+%% API.
+
+start(_Type, _Args) ->
+ Dispatch = [
+ {'_', [
+ {[], websockets_handler, []}
+ ]}
+ ],
+ {ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
+ {dispatch, Dispatch}
+ ]),
+ websockets_sup:start_link().
+
+stop(_State) ->
+ ok.
@@ -0,0 +1,88 @@
+-module(websockets_handler).
+-behaviour(cowboy_http_handler).
+-behaviour(cowboy_websocket_handler).
+
+-export([init/3, handle/2, terminate/2]).
+
+-export([websocket_init/3, websocket_handle/3,
+ websocket_info/3, websocket_terminate/3]).
+
+init({_Any, http}, Req, []) ->
+ case cowboy_req:header(<<"upgrade">>, Req) of
+ {undefined, Req2} -> {ok, Req2, undefined};
+ {<<"websocket">>, _Req2} -> {upgrade, protocol, cowboy_websocket}
+ end.
+
+handle(Req, State) ->
+ {ok, Req2} = cowboy_req:reply(200, [{<<"Content-Type">>, <<"text/html">>}],
+%% HTML code taken from misultin's example file.
+<<"<html>
+<head>
+<script type=\"text/javascript\">
+function addStatus(text){
+ var date = new Date();
+ document.getElementById('status').innerHTML
+ = document.getElementById('status').innerHTML
+ + date + \": \" + text + \"<br/>\";
+}
+function ready(){
+ if (\"MozWebSocket\" in window) {
+ WebSocket = MozWebSocket;
+ }
+ if (\"WebSocket\" in window) {
+ // browser supports websockets
+ var ws = new WebSocket( (window.location.protocol == 'http:'
+ ? 'ws://' : 'wss://') +
+ window.location.host + '/');
+ ws.onopen = function() {
+ // websocket is connected
+ addStatus('websocket connected!');
+ // send hello data to server.
+ var msg = 'hello hello hello what\\'s going on \\'ere then?';
+ ws.send(msg);
+ addStatus('sent message to server: ' + msg);
+ };
+ ws.onmessage = function (evt) {
+ var receivedMsg = evt.data;
+ addStatus(\"server sent the following: '\" + receivedMsg + \"'\");
+ };
+ ws.onclose = function() {
+ // websocket was closed
+ addStatus(\"websocket was closed\");
+ };
+ } else {
+ // browser does not support websockets
+ addStatus(\"sorry, your browser does not support websockets.\");
+ }
+}
+</script>
+</head>
+<body onload=\"\">
+Hi!
+<input type=\"button\" onclick=\"ready();\" value=\"click\"/>
+<div id=\"status\"></div>
+</body>
+</html>">>, Req),
+ {ok, Req2, State}.
+
+terminate(_Req, _State) ->
+ ok.
+
+websocket_init(_Any, Req, []) ->
+ timer:send_interval(1000, tick),
+ Req2 = cowboy_req:compact(Req),
+ {ok, Req2, undefined, hibernate}.
+
+websocket_handle({text, Msg}, Req, State) ->
+ {reply, {text, << "You said: ", Msg/binary >>}, Req, State, hibernate};
+websocket_handle(_Any, Req, State) ->
+ {ok, Req, State}.
+
+%% send some repeated text to show compression at work if anyone is counting
+websocket_info(tick, Req, State) ->
+ {reply, {text, <<"badger badger badger badger badger badger badger">>}, Req, State, hibernate};
+websocket_info(_Info, Req, State) ->
+ {ok, Req, State, hibernate}.
+
+websocket_terminate(_Reason, _Req, _State) ->
+ ok.
@@ -0,0 +1,21 @@
+%% @private
+-module(websockets_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}}.
@@ -0,0 +1,3 @@
+#!/bin/sh
+erl -pa ebin ../../ebin ../../deps/*/ebin -boot start_sasl -s websockets \
+ -eval "io:format(\"Point your browser at http://localhost:8080~n\")."

0 comments on commit a74fbd7

Please sign in to comment.