Subscribing and receiving events

You can subscribe to predefined and custom events. Predefined events are "connect", "connecting", "event", "disconnect", "reconnect", "reconnecting", "reconnect_attempt", "reconnect_failed", "error". Custom events are programmer defined events that your server will send to your client. You can subscribe to an event by calling a socket’s On function:

manager.Socket.On("login", OnLogin);
manager.Socket.On("new message", OnNewMessage);

An event handler will look like this:

void OnLogin(Socket socket, Packet packet, params object[] args)
{
}

A message emitted on the server(node.js):

// send a message to the client
socket.emit('message', 'MyNick', 'Msg to the client');

can be caught by the client:

// subscribe to the "message" event
manager.Socket.On("message", OnMessage);

// event handler
void OnMessage(Socket socket, Packet packet, params object[] args)
{
    // args[0] is the nick of the sender
    // args[1] is the message
    Debug.Log(string.Format("Message from {0}: {1}", args[0], args[1]));
}

Predefined events

// The event handler will be called only once
manager.Socket.Once("connect", OnConnected);
// Removes all event-handlers
manager.Socket.Off();

// Removes event-handlers from the "connect" event
manager.Socket.Off("connect");


// Removes the OnConnected event-handler from the "connect" event
manager.Socket.Off("connect", OnConnected);