Websocket

WebSocket is an HTML5 technology used for two-way messaging from inside a web page.
WebSocket is a protocol providing full-duplex communication channels over a single TCP connection.
WebSocket

ChicagoBoss WebSockets

###Server Code

The server WebSocket API is based around Erlang message-passing;
to send a message to a particular client, simply send it a message like:

WebSocket ! {text, <<"some text">>}

Furthermore, you can broadcast a message that will be handled by your handle_broadcast/2 function by
using the module name of your websocket module and the proper method of the boss_service_worker module:

boss_service_worker:broadcast(myapp_myservice_websocket, MyMessage).

To handle incoming messages from clients, you’ll need to create WebSocket controllers in your project’s
src/websocket directory. Each controller module should have a name of the form
<app name>_<service name>_websocket.erl and be a parameterized module with parameters for
Req and SessionId, e.g.:

-module(myapp_myservice_websocket, [Req, SessionId]).

The module should implement the boss_service_handler behavior.

-module(ab_status_websocket, [Req, SessionId]).
-behaviour(boss_service_handler).
-compile(export_all).

init() -> {ok, []}.

handle_join(_Url, WebsocketPid, State) ->
    log:d("~p joined!", [WebsocketPid]),
    WebsocketPid ! {text, "Hello WebSocket!"},
    NewState = [WebsocketPid | State],
    {noreply, NewState}.

handle_close(_Reason, _Url, WebsocketPid, State) ->
    log:d("~p closed!", [WebsocketPid]),
    NewState = lists:delete(WebsocketPid, State),
    {noreply, NewState}.

handle_incoming(_Url, _WebsocketPid, _Message, State) ->
    {noreply, State}.

handle_broadcast(Message, State) ->
    broadcast_message(Message, State),
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) -> ok.

broadcast_message(Message, [H|R]) ->
    log:d("send ~p to ~p ...", [Message, H]),
    H ! {text, Message},
    broadcast_message(Message, R);
broadcast_message(_Message, []) -> ok.

###Client code

WebSocket URLs are automatically generated from the WebSocket controllers in your project’s
src/websocket directory. For example, if you have myapp_foobar_websocket.erl, then to create
a new WebSocket on the client:

<head>
    <!--<meta http-equiv="refresh" content="5">-->
    <script>
        function createWebsocket()
        {
            ws = new WebSocket("ws://localhost:8001/websocket/status");
            ws.onmessage = function(e){
                <!--alert(e.data);-->
                document.getElementById("status").innerHTML = e.data;
                scrollToBottom();
            }
        }
        function scrollToBottom()
        {
            window.scrollTo(0, document.body.scrollHeight);
        }
        function reloadPage()
        {
            window.location.reload();
        }
        function load()
        {
            scrollToBottom();
            createWebsocket();
        }
    </script>
</head>
<!--<body bgcolor="#000000" onclick="reloadPage()" onload="load()">-->
<body bgcolor="#000000" onload="load()">
<div>
    <pre style="color:#00ff00" id="status">
    
    </pre>
</div>
</body>

到此AutoBuild初步完成状态通知。


乌托邦

xl

Stay hungry, Stay foolish.