Skip to content


Subversion checkout URL

You can clone with
Download ZIP
tree: f384bd4601
Fetching contributors…

Cannot retrieve contributors at this time

85 lines (76 sloc) 2.438 kb
%% Feel free to use, reuse and abuse the code in this file.
-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_http_req:header('Upgrade', Req) of
{undefined, Req2} -> {ok, Req2, undefined};
{<<"websocket">>, _Req2} -> {upgrade, protocol, cowboy_http_websocket};
{<<"WebSocket">>, _Req2} -> {upgrade, protocol, cowboy_http_websocket}
handle(Req, State) ->
{ok, Req2} = cowboy_http_req:reply(200, [{'Content-Type', <<"text/html">>}],
%% HTML code taken from misultin's example file.
<script type=\"text/javascript\">
function addStatus(text){
var date = new Date();
= 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(\"ws://localhost:8080/websocket\");
ws.onopen = function() {
// websocket is connected
addStatus(\"websocket connected!\");
// send hello data to server.
ws.send(\"hello server!\");
addStatus(\"sent message to server: 'hello server'!\");
ws.onmessage = function (evt) {
var receivedMsg =;
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.\");
<body onload=\"ready();\">
<div id=\"status\"></div>
</html>">>, Req),
{ok, Req2, State}.
terminate(_Req, _State) ->
websocket_init(_Any, Req, []) ->
timer:send_interval(1000, tick),
Req2 = cowboy_http_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}.
websocket_info(tick, Req, State) ->
{reply, {text, <<"Tick">>}, Req, State, hibernate};
websocket_info(_Info, Req, State) ->
{ok, Req, State, hibernate}.
websocket_terminate(_Reason, _Req, _State) ->
Jump to Line
Something went wrong with that request. Please try again.