off
The off
method will dispose of a message handler. You might still get messages over the wire since it is RPC, but instead they will end up in the onMessage
handler if you have such a handler.
The sample below will send messages on the topic 'foo' every 3 seconds when connected. The message handler for 'foo' will unregister after 3 messages with the method off('foo');
. After the dispose of the message handler for foo, the messages will end up in the onMessage
handler
<script>
var conn = new xsockets.client('ws://localhost:4502', ['generic']);
var generic = conn.controller('generic');
// For showing that messages end up here if there is no handler for the topic.
generic.onMessage = function (d) {
console.log('OnMessage MessageHandler', d);
};
var maxFooAccpeted = 3;
generic.on('foo', function(data) {
maxFooAccpeted--;
if (maxFooAccpeted === 0)
generic.off('foo');
console.log('Foo MessageHandler', data);
});
// Just for sending some data
generic.onOpen = function(ci) {
setInterval(function() {
generic.invoke('foo', 'Hello world');
}, 3000);
};
conn.open();
</script>