[erlang-questions] WebSocket impl for Mochiweb

20 views
Skip to first unread message

Dave Bryson

unread,
Dec 14, 2009, 6:33:35 PM12/14/09
to erlang-q...@erlang.org
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

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Joe Armstrong

unread,
Dec 15, 2009, 3:50:24 AM12/15/09
to Dave Bryson, erlang-q...@erlang.org
Sweet. You have a nice way of encapsulating the interface in a Fun.

Now all we need is a javascript IRC client that talks web-sockets
and a server that understands the IRC protocol, and a backend based on a DHT
(like riak) and we have a high scalable robust p2p architecture for
chat/whatever !!!

I can contribute a IRC server that is pretty basic (it's almost done)

Just need riak integration and a javascript client.

/Joe

Reply all
Reply to author
Forward
0 new messages