Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

63 lines (53 sloc) 1.826 kb
-module(websockets_passive).
-author('author <rj@metabrew.com>').
-export([start/0, start/1, stop/0, loop/2, wsloop/1]).
start() -> start([{port, 8080}, {docroot, "."}]).
start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Loop = fun (Req) -> ?MODULE:loop(Req, DocRoot) end,
% How we validate origin for cross-domain checks:
OriginValidator = fun(Origin) ->
io:format("Origin '~s' -> OK~n", [Origin]),
true
end,
% websockets options:
WsOpts = [ {active, false},
{origin_validator, OriginValidator},
{loop, {?MODULE, wsloop}} ],
mochiweb_http:start([{name, ?MODULE},
{loop, Loop},
{websocket_opts, WsOpts} | Options1]).
stop() ->
mochiweb_http:stop(?MODULE).
wsloop(Ws) ->
Ws:send("WELCOME MSG FROM THE SERVER part 1/2"),
Ws:send("WELCOME MSG FROM THE SERVER part 2/2"),
wsloop0(Ws).
wsloop0(Ws) ->
case Ws:get_data() of
closed -> ok;
closing -> ok;
timeout -> timeout;
{frame, Body} ->
Ws:send(["Dear client, thanks for sending this message: ", Body]),
wsloop0(Ws)
end.
loop(Req, DocRoot) ->
"/" ++ Path = Req:get(path),
case Req:get(method) of
Method when Method =:= 'GET'; Method =:= 'HEAD' ->
case Path of
_ ->
Req:serve_file(Path, DocRoot)
end;
'POST' ->
case Path of
_ ->
Req:not_found()
end;
_ ->
Req:respond({501, [], []})
end.
%% Internal API
get_option(Option, Options) ->
{proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
Jump to Line
Something went wrong with that request. Please try again.