onMessage

In the browsers websocket API the onMessage event is fired as soon as the browser receives data on the socket. This can be both binary or text and when using onMessage you have to check tha actual data to see what you got. In the XSockets client this event is rarely used. The only time the event is fired is when no controller is found to dispatch the message to, and that should not happen.

To trigger the event we will send data to the server without using a controller. When the data comes back the onMessage event will fire since there is no controller defined on the client.

Note: This is not the normal way to use the API. This is just to trigger the onMessage event and show that the event will fire if no controller is available.

<script>
    var conn = new xsockets.client('ws://localhost:4502');
    conn.onOpen = function (e) {
        console.log('Connection Open, send fake data', e);
        var m = new xsockets.message('generic', 'foo', 'bar');
        conn.sendtext(m);
    }
    conn.onMessage = function(e) {
        console.log('data arrived', e);
    };
    conn.open();
</script>

results matching ""

    No results matching ""