Some time ago I found Joe Armstrong's example on Erlang and websocket, but I couldn't get it work.
I fixed an error and a couple of warnings in the 开发者_StackOverflow社区Erlang code but with Apache I wasn't able to get a good result.
Can anybody give me some hints with a really simple example? Do I need to put the web page with the JavaScript inside the Apache directory as for normal PHP files?
Joe's websocket example is outdated and relies on an obsolete version of the protocol. Up-to-date browsers use a more recent version (draft-00)
As of today, a nice erlang implementation is available from misultin. Tested and compatible with current browsers:
https://github.com/ostinelli/misultin/blob/master/src/misultin_websocket.erl
The Yaws webserver provides a websocket implementation. I also wrote a behaviour to simplify the writing of ws-based applications. It's part of my Erlang tools (well, the first one in fact):
https://github.com/schemeway/erlang-tools
I found the SockJS-Erlang library to work wonderfully well. Best of all it supports fallback transports if websockets are not available. It uses Cowboy (although an older version) as the underlying server which is nice because it is easy to integrate with. This escript and this HTML page will give you a working demo you can play around with.
here is an annotated example:
start_link(_) ->
application:start(sockjs),
application:start(cowboy),
% generate a SockJS handler
SockjsState = sockjs_handler:init_state(
<<"/browser_socket">>, fun handle_client/3, state, []),
% build the dispatch routes for Cowboy integrating the SockJS handler
Routes = [{'_', [{[<<"echo">>, '...'],
sockjs_cowboy_handler, SockjsState}]}],
% start the cowboy server
cowboy:start_listener(http, 100,
cowboy_tcp_transport, [{port, 8081}],
cowboy_http_protocol, [{dispatch, Routes}]),
% called when a new client connects
handle_client(Conn, init, state) -> {ok, state};
% called when data is received
handle_client(Conn, {recv, Data}, state) ->
% reply to client
Conn:send(Data);
% called when connection is closed
handle_client(_Conn, closed, state) -> {ok, state}.
My advice regarding Apache would be to use HAProxy for your WebSocket connections and Apache for serving Javascript and PHP.
精彩评论