WebSocket impl for Mochiweb
Dave Bryson
daveb@REDACTED
Tue Dec 15 00:33:35 CET 2009
Like many others, the WebSocket bug bit me. So I've implemented a
little add on for Mochiweb. Basically the module wraps the
mochiweb_socket_server and allows you to write WebSocket based apps
similar to how you'd write a regular web application in Mochiweb.
Here's an example from the source code:
-module(basic_websocket).
-export([start/1, stop/0, loop/1]).
start(Options) ->
Loop = fun (WebSocket) ->
?MODULE:loop(WebSocket)
end,
mochiweb_websocket:start([{name, ?MODULE}, {loop, Loop} |
Options]).
stop() ->
mochiweb_websocket:stop(?MODULE).
loop(WebSocket) ->
%% Get the data sent from the client
Data = WebSocket:get_data(),
%% This is a little echo service. The "client-connected" is just
our implementation
%% for this example. See priv/www/index.html
case Data of
"client-connected" ->
WebSocket:send("You are connected!");
%% Other messages go here
Other ->
Msg = "You Said: " ++ Other,
WebSocket:send(Msg)
end.
You can find the source here: http://github.com/davebryson/erlang_websocket
This is just a quick bit of code, but it seems to work fine for
playing around.
Dave
More information about the erlang-questions
mailing list