Class: On

(private) wsUtil.SocketEmitter~On(id) → {OnInterface}

(private) new On(id) → {OnInterface}

The emit event handlers acts on emit events from the clients

Parameters:
Name Type Description
id string

The id of the connection

Since:
  • 0.0.1
Source:
Returns:

Binds a handler to an emit event

Type
OnInterface

Type Definitions

OnInterface(event, handler)

Returned function from the On class instance. This function takes an event and a handler. The handler is being called when a client emits the event with or without parameters

Parameters:
Name Type Description
event string

The name of the emitted event from the client

handler function

The emitted event handler

Source:
Example
socketEmitter.connect(function(socket) {
 // example of handler without parameters
 socket.on('What is the meaning of life, the universe and everything?', function() {
     socket.emit('answer', calculateTheMeaningOfLifeTheUniverseAndEverything());
 });
 // example of handler with parameters
 socket.on('isPrime?', function(number) {
     function isPrime(num, iter) {
       if (!iter) iter = Math.floor(Math.sqrt(num));
       if (iter > 1) return isPrime(num, iter-1) ? num % iter !== 0 : false;
        return true
      }
      socket.emit('isPrime!', isPrime(number));
 });
})