Game Development Reference
In-Depth Information
socket.emit('clientData, inputElement.value);
inputElement.value = '';
return false;
} else {
return true;
}
};
The corresponding server handler function for the client event, clientData , on
invocation, sends the data back to the client and broadcasts the message. Then, the
code line sends the data back to the client and invokes its serverData event. This
code broadcasts the message to all the clients connected to the socket server except
the socket it is fired on. As we can see in the following code listing, we first emit
the serverData event on the calling socket and then broadcast serverMessage to
all sockets:
io.sockets.on('connection', function (socket) {
socket.on('clientData', function(data) {
socket.emit('serverData', data);
socket.broadcast.emit('serverMessage', socket.id +content);
});
});
As you can see in the preceding code, the client and server both use the emit
function to invoke each other's events. The other important aspect is socket.id ,
which is a unique key that identifies each client. The complete implementation of
socket.id is managed by the Socket.IO library.
Learning the Socket.IO API
The Socket.IO API consists of server-side functions and client-side functions. The
API has a list of reserved events. The API defines two objects on the server side:
io.sockets and socket . They are explained as follows:
io.sockets : This has only one reserved event connection.
io.sockets.on('connection', function(socket) {})
The preceding event is generated when a new connection is requested from
the client side.
socket : This offers two reserved events, message and disconnect .
The message event is fired when the send function of the socket object is
used to emit events.
 
Search WWH ::




Custom Search