Game Development Reference
In-Depth Information
The client code is as follows:
<script>
var socket = io.connect('http://localhost/');
socket.on('connect', function () {
socket.send('hi');
socket.on('message', function (msg) {
});
});
</script>
The server code is as follows:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('message', function () { });
socket.on('disconnect', function () { });
});
In the preceding server code, we handle two events, message and
disconnect . The disconnect event is fired when the client-server
connection is closed.
The client socket's reserved events
The client socket has many reserved events defined. Refer to the following list:
connect : This is emitted when the socket connection is successful.
connecting : This is emitted when the socket is attempting to connect
with the server.
disconnect : This is emitted when the socket is disconnected.
connect_failed : This is emitted when Socket.IO fails to establish a
connection to the server and has no other transport to fallback on.
error : This is emitted when an error occurs and it cannot be handled by the
other event types.
message : This is emitted when a message sent with the socket.send
function is received.
reconnect_failed : This is emitted when Socket.IO fails to reestablish a
working connection after the connection was dropped.
reconnect : This is emitted when Socket.IO has successfully reconnected to
the server.
reconnecting : This is emitted when the socket is attempting to reconnect
with the server.
 
Search WWH ::




Custom Search