Game Development Reference
In-Depth Information
Storing user data on the server side
There are many cases where we need to store the user data for a session. We will lose
the data once the user disconnects. The following code uses Socket.IO API's get and
set functions of the server socket class to store and retrieve the user data:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.on('set nickname', function (name) {
socket.set('username', name, function () {
socket.emit('ready');
});
});
socket.on('msg', function () {
socket.get('username', function (err, name) {
console.log('Chat message by ', name);
});
});
});
The following code sets the username variable:
// Sets the username variable.
socket.set('username', name);
The following code retrieves the stored username variable:
socket.get('username', function (err, name) {
console.log('Chat message by ', name);
});
Implementing a multiplayer game
The objective of this section is to implement what we have learned in the previous
sections. We simply want to implement a spectator player, a player who simply
launches our game can only view it. This simple implementation is a good start for
the complete multiplayer game implementation.
Let's first understand the data that we need to communicate to our spectator client.
In our game, currently we have nearly all static assets, except camera, bullets, and
grenade. Also, the location/direction of bullet and grenade are derived from the
camera location. So, all we need to communicate to the other client is:
• Camera position, direction, left and up vectors
• The bullet fire event and the bullet index that was fired
• The grenade fire event
 
Search WWH ::




Custom Search