documentation for websocket example
Project: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/commit/ecb1eba1 Tree: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/tree/ecb1eba1 Diff: http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/diff/ecb1eba1 Branch: refs/heads/1843-feature-bigcouch Commit: ecb1eba135ce140c16cc58316b7fb20e5d1a39c8 Parents: 3961edc Author: Åukasz Lalik <[email protected]> Authored: Wed Dec 25 13:46:08 2013 +0100 Committer: Åukasz Lalik <[email protected]> Committed: Wed Dec 25 13:46:08 2013 +0100 ---------------------------------------------------------------------- examples/websocket/websocket.erl | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb-mochiweb/blob/ecb1eba1/examples/websocket/websocket.erl ---------------------------------------------------------------------- diff --git a/examples/websocket/websocket.erl b/examples/websocket/websocket.erl index b70a0de..7039e42 100644 --- a/examples/websocket/websocket.erl +++ b/examples/websocket/websocket.erl @@ -24,7 +24,28 @@ -export([start_link/0, ws_loop/3, loop/1]). +% +% Mochiweb websocket example +% +% [1]: At first you have to start HTTP server which will listen for HTTP requests +% and eventually upgrade connection to websocket +% [2]: Attempt to upgrade connection to websocket. +% Function mochiweb_websocket:upgrade_connection/2: +% * first argument is mochiweb_request +% * second is M:F which will handle further websocket messages. +% Function return two funs: +% * ReentryWs/1 - use it to enter to messages handling loop (in this example ws_loop/3) +% * ReplyChannel/1 - use to send messages to client. May be passed to other processes +% [3]: Example of sending message to client +% [4]: State that will be passed to message handling loop +% [5]: Pass controll to messages handling loop. From this moment each message received from client +% can be handled... +% [6]: ...here as Payload. State is variable intended for holiding your custom state. ReplyChannel +% is the same function as in [3]. +% [7]: Print payload received from client and send it back +% [8]: Message handling function must return new state value start_link() -> + % [1] Loop = fun (Req) -> ?MODULE:loop(Req) end, @@ -36,13 +57,24 @@ start_link() -> ]). ws_loop(Payload, State, ReplyChannel) -> + % [6] + + % [7] io:format("Received data: ~p~n", [Payload]), Received = list_to_binary(Payload), ReplyChannel(<<"Received ", Received/binary>>), + + % [8] State. loop(Req) -> + % [2] {ReentryWs, ReplyChannel} = mochiweb_websocket:upgrade_connection(Req, {?MODULE, ws_loop}), + + % [3] ReplyChannel(<<"Hello">>), + + % [4] InitialState = [], + % [5] ReentryWs(InitialState).
