davebryson/erlang_websocket

WebSocket Server and Client implementation in Erlang/Mochiweb

Erlang

83

7 commits

updated Mar 31, 2011

See the code

README

h2. Erlang WebSocket Server and Client


h3. WebSocket Server

The server is a simple wrapper around the mochiweb_socket_server to allow "WebSocket":http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-66 connections.  With this wrapper, you can write WebSocket connection loops just like you'd write a normal mochiweb application.  Here's an example (included with the source code):

<pre>
 <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(),
    
    %% For this example...
    %% Of course you could handle JSON or another format of your choice
    case Data of
	%% On initial connect we get this message
	"client-connected" ->
	    WebSocket:send("You are connected!");
	%% Other messages go here
	Other ->
	    Msg = "You Said: " ++ Other,
	    WebSocket:send(Msg)
    end.
 </code>
</pre> 

h3. WebSocket Client

This is a pure Erlang WebSocket client you can use to call a WebSocket Server from Erlang code. For simplicity, the code defines an Erlang Behaviour you implement for your client. The Behaviour defines the functions outlined in the WebSocket API spec.  Here's an example of a simple client:

<pre>
 <code>

-module(sample).
-behaviour(websocket_client).
-export([start/0]).
%% websocket specific callbacks
-export([onmessage/1,onopen/0,onclose/0,close/0,send/1]).

send(Data) ->
    websocket_client:write(Data).

start() ->
    websocket_client:start("localhost",8002,?MODULE).

%% Handle incoming messages here
onmessage(Data) ->
    io:format("Got some data:: ~p~n",[Data]).

onclose() ->
    io:format("Connection closed~n").

onopen() ->
    io:format("Connection open~n"),
    send("client-connected").

close() ->
    websocket_client:close().

 </code>
</pre>

Here's an example of using the client from an erl shell:

<pre>
 <code>
> sample:start().
> sample:send("Hello there").
> sample:close().
 </code>
</pre>

Check out the examples directory. 

h3. Requirements:
 
 # Erlang 12.5 or greater
 # Google Chrome Browser developer channel release 4.0.249.0


h3. Getting Started:
 
 # download the code
 # CD into the erlang_websocket directory
 # run make

h3. Run the example:

 # CD into the examples/basic directory
 # run the 'start.sh' script
 # Point the Chrome Browser to 'http://localhost:8000'


    
 

Not written in Markdown, so it's shown here as plain text — view it formatted on GitHub.

Contributors

davebryson

6 commits

leandrosilva

1 commits

davebryson/erlang_websocket

WebSocket Server and Client implementation in Erlang/Mochiweb

Erlang

83

7 commits

updated Mar 31, 2011

See the code

README

h2. Erlang WebSocket Server and Client


h3. WebSocket Server

The server is a simple wrapper around the mochiweb_socket_server to allow "WebSocket":http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-66 connections.  With this wrapper, you can write WebSocket connection loops just like you'd write a normal mochiweb application.  Here's an example (included with the source code):

<pre>
 <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(),
    
    %% For this example...
    %% Of course you could handle JSON or another format of your choice
    case Data of
	%% On initial connect we get this message
	"client-connected" ->
	    WebSocket:send("You are connected!");
	%% Other messages go here
	Other ->
	    Msg = "You Said: " ++ Other,
	    WebSocket:send(Msg)
    end.
 </code>
</pre> 

h3. WebSocket Client

This is a pure Erlang WebSocket client you can use to call a WebSocket Server from Erlang code. For simplicity, the code defines an Erlang Behaviour you implement for your client. The Behaviour defines the functions outlined in the WebSocket API spec.  Here's an example of a simple client:

<pre>
 <code>

-module(sample).
-behaviour(websocket_client).
-export([start/0]).
%% websocket specific callbacks
-export([onmessage/1,onopen/0,onclose/0,close/0,send/1]).

send(Data) ->
    websocket_client:write(Data).

start() ->
    websocket_client:start("localhost",8002,?MODULE).

%% Handle incoming messages here
onmessage(Data) ->
    io:format("Got some data:: ~p~n",[Data]).

onclose() ->
    io:format("Connection closed~n").

onopen() ->
    io:format("Connection open~n"),
    send("client-connected").

close() ->
    websocket_client:close().

 </code>
</pre>

Here's an example of using the client from an erl shell:

<pre>
 <code>
> sample:start().
> sample:send("Hello there").
> sample:close().
 </code>
</pre>

Check out the examples directory. 

h3. Requirements:
 
 # Erlang 12.5 or greater
 # Google Chrome Browser developer channel release 4.0.249.0


h3. Getting Started:
 
 # download the code
 # CD into the erlang_websocket directory
 # run make

h3. Run the example:

 # CD into the examples/basic directory
 # run the 'start.sh' script
 # Point the Chrome Browser to 'http://localhost:8000'


    
 

Not written in Markdown, so it's shown here as plain text — view it formatted on GitHub.

Contributors

davebryson

6 commits

leandrosilva

1 commits

Languages

Erlang

100.0%