Skip to content

Instantly share code, notes, and snippets.

@majek
Created February 24, 2012 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save majek/1901073 to your computer and use it in GitHub Desktop.
Save majek/1901073 to your computer and use it in GitHub Desktop.
Possible SockJS-erlang ws bug
all:
rebar get-deps
rebar compile
erl -pa ebin deps/*/ebin -s wsbug
clean:
rm -rf deps ebin
{erl_opts, [debug_info]}.
{deps, [
{cowboy, ".*", {git, "https://github.com/extend/cowboy.git", "master"}},
{sockjs, ".*", {git, "https://github.com/sockjs/sockjs-erlang.git", "master"}}
]}.
{application, wsbug,
[
{description, ""},
{vsn, "1"},
{registered, []},
{applications, [
kernel,
stdlib
]},
{mod, { wsbug_app, []}},
{env, []}
]}.
%% Author: shizz
%% Created: Feb 22, 2012
%% Description: TODO: Add description to strata_sockjs
-module(wsbug).
-export([start/0, init/3, handle/2, terminate/2, service_echo/2]).
-define(PORT, 8081).
start() ->
application:start(cowboy),
application:start(sockjs),
io:format("Running localhost:~p~n", [?PORT]),
EchoState = sockjs_handler:init_state(
<<"/echo">>, fun service_echo/2, []),
VhostRoutes = [{[<<"echo">>, '...'], sockjs_cowboy_handler, EchoState},
{'_', ?MODULE, []}],
Routes = [{'_', VhostRoutes}], % any vhost
cowboy:start_listener(http, 100,
cowboy_tcp_transport, [{port, ?PORT}],
cowboy_http_protocol, [{dispatch, Routes}]).
init({_Any, http}, Req, []) ->
io:format("init()~n"),
{ok, Req, []}.
handle(Req, State) ->
io:format("handle()~n"),
Data = <<"<!doctype html>
<html><head>
<script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js\">
</script>
<script src=\"http://cdn.sockjs.org/sockjs-0.2.min.js\">
</script>
<style>
.box {
border: 1px dashed black;
border-radius: 4px;
-moz-border-radius: 4px;
width: 400px;
display: block;
height: 300px;
float: left;
}
#output {
border-color: grey;
overflow:auto;
}
#input {
vertical-align: text-top;
-moz-outline-style: none;
outline-style: none;
outline-width: 0px;
outline-color: -moz-use-text-color;
}
body {
background-color: #F0F0F0;
}
</style>
</head><body lang=\"en\">
<h2>SockJS-erlang Echo example</h2>
<form id=\"form\">
<input id=\"input\" autocomplete=\"off\" class=\"box\" align=top
value=\"type something here\" />
</form>
<div id=\"output\" class=\"box small\" />
<script>
function log(m) {
$('#output').append($(\"<code>\").text(m));
$('#output').append($(\"<br>\"));
$('#output').scrollTop($('#output').scrollTop()+10000);
}
var sockjs_url = '/echo';
var sockjs = new SockJS(sockjs_url);
sockjs.onopen = function() {
log(' [*] Connected (using: '+sockjs.protocol+')');
};
sockjs.onclose = function(e) {
log(' [*] Disconnected ('+e.status + ' ' + e.reason+ ')');
};
sockjs.onmessage = function(e) {
log(' [ ] received: ' + JSON.stringify(e.data));
};
$('#input').focus();
$('#form').submit(function() {
var val = $('#input').val();
$('#input').val('');
var l = ' [ ] sending: ' + JSON.stringify(val);
if (sockjs.readyState !== SockJS.OPEN) {
l += ' (error, connection not established)';
} else {
sockjs.send(val);
}
log(l);
return false;
});
</script>
</body></html>">>,
{ok, Req1} = cowboy_http_req:reply(200, [{<<"Content-Type">>, "text/html"}],
Data, Req),
{ok, Req1, State}.
terminate(_Req, _State) ->
io:format("terminate()~n"),
ok.
service_echo(Conn, {recv, Data}) ->
io:format("MESG: Conn: ~p, Data: ~p~n", [Conn, Data]),
sockjs:send(Data, Conn);
service_echo(Conn, Data) ->
io:format("MSG: Conn: ~p, Data: ~p~n", [Conn, Data]),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment