onMessage

The onMessage event is rarely used, just like the onMessage event on the socket. The messages should be handled by RPC or PUB/SUB. If the controller receives a message and no method is bound to the topic the onMessage event will be triggered.

The sample below will send a message to the generic controller when it is opened. Then the onMessage event will be triggered since we do not use RPC or PUB/SUB to handle the 'sayHello' topic.

<script>
    var conn = new xsockets.client('ws://localhost:4502', ['generic']);

    var generic = conn.controller('generic');
    generic.onMessage = function (m) {
        console.log('Generic onMessage fired ', m);
    };

    generic.onOpen = function () {
        console.log('Generic controller opened, send a message');
        generic.invoke('sayHello', 'Hello World!');
    };
    conn.open();
</script>

results matching ""

    No results matching ""