Skip to content

Commit 4d46f38

Browse files
Incrementally modifying codebase to come in line with Node 0.9 and socket.io 0.9
1 parent 9af78f8 commit 4d46f38

File tree

4 files changed

+131
-103
lines changed

4 files changed

+131
-103
lines changed

DemoHelloWorld.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<!--lib-->
2727
<script src="js/lib/caat.js"></script>
2828
<script src="js/lib/Stats.js"></script>
29-
<script src="js/lib/socket.io.js"></script>
29+
<script src="node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js"></script>
3030
<script src="js/lib/SortedLookupTable.js"></script>
3131
<script src="js/lib/RequestAnimationFrame.js"></script>
3232

js/model/Constants.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,17 @@ Version:
2424

2525
SERVER_SETTING:
2626
{
27-
CLIENT_ID : 0, // If an object has a client id of zero, that means it is owned by the server
28-
SOCKET_ADDRESS: "localhost",
29-
SOCKET_PORT : 8081
27+
CLIENT_ID : 0, // If an object has a client id of zero, that means it is owned by the server
28+
SOCKET_PROTOCOL : "http",
29+
SOCKET_DOMAIN : "localhost",
30+
SOCKET_PORT : 8081,
31+
32+
/** @return {string} */
33+
GET_URI : function() {
34+
return RealtimeMultiplayerGame.Constants.SERVER_SETTING.SOCKET_PROTOCOL
35+
+ "://" + RealtimeMultiplayerGame.Constants.SERVER_SETTING.SOCKET_DOMAIN
36+
+ ":" + RealtimeMultiplayerGame.Constants.SERVER_SETTING.SOCKET_PORT;
37+
}
3038
},
3139

3240
CLIENT_SETTING:

js/network/ClientNetChannel.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,15 @@ Version:
6060

6161

6262
setupSocketIO: function() {
63-
this.socketio = new io.Socket(RealtimeMultiplayerGame.Constants.SERVER_SETTING.SOCKET_ADDRESS, {port: RealtimeMultiplayerGame.Constants.SERVER_SETTING.SOCKET_PORT, transports:['websocket', 'xhr-polling', 'jsonp-polling'], reconnect: false, rememberTransport: false});
64-
this.socketio.connect();
63+
var socket = io.connect('http://localhost');
64+
socket.on('news', function (data) {
65+
console.log(data);
66+
socket.emit('my other event', { my: 'data' });
67+
});
68+
69+
70+
debugger;
71+
this.socketio = new io.connect( RealtimeMultiplayerGame.Constants.SERVER_SETTING.GET_URI(), {transports:['websocket', 'xhr-polling', 'jsonp-polling'], reconnect: false, rememberTransport: false});
6572

6673
var that = this;
6774
this.socketio.on('connect', function(){ that.onSocketConnect() });

js/network/ServerNetChannel.js

Lines changed: 110 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,36 @@
11
/**
2-
File:
3-
ServerNetChannel.js
4-
Created By:
5-
Mario Gonzalez
6-
Project:
7-
RealtimeMultiplayerNodeJS
8-
Abstract:
9-
This class is responsible for managing the socket connection for each client
10-
11-
-> ClientNetChannel talks to this object
12-
<--> ServerNetChannel talks to it's GameController via delegation
13-
<-- ServerNetChannel broadcast the message to all clients
14-
15-
Basic Usage:
2+
File:
3+
ServerNetChannel.js
4+
Created By:
5+
Mario Gonzalez
6+
Project:
7+
RealtimeMultiplayerNodeJS
8+
Abstract:
9+
This class is responsible for managing the socket connection for each client
10+
11+
-> ClientNetChannel talks to this object
12+
<--> ServerNetChannel talks to it's GameController via delegation
13+
<-- ServerNetChannel broadcast the message to all clients
14+
15+
Basic Usage:
1616
TODO: UPDATE USAGE
17-
Version:
18-
1.0
19-
*/
20-
(function(){
21-
// Node.JS Imports
22-
var http = require('http');
23-
var url = require('url');
24-
var fs = require('fs');
25-
var sys = require(process.binding('natives').util ? 'util' : 'sys');
26-
var io = require('../lib/Socket.IO-node');
27-
28-
// Local variables for private things in this class
17+
Version:
18+
1.1.0
19+
*/
20+
(function () {
21+
/**
22+
*
23+
* The auto-incrimented ID to use for the next client
24+
*/
2925
var nextClientID = RealtimeMultiplayerGame.Constants.SERVER_SETTING.CLIENT_ID;
3026

31-
// Retrieve the namespace
32-
RealtimeMultiplayerGame.namespace("RealtimeMultiplayerGame.network");
27+
RealtimeMultiplayerGame.namespace( "RealtimeMultiplayerGame.network" );
3328

3429
/**
3530
* Creates a new ServerNetChannel instance
3631
* @param {RealtimeMultiplayerGame.network.ServerNetChannelDelegateProtocol} aDelegate A delegate that conforms to RealtimeMultiplayerGame.network.ServerNetChannelDelegateProtocol
3732
*/
38-
RealtimeMultiplayerGame.network.ServerNetChannel = function( aDelegate ) {
33+
RealtimeMultiplayerGame.network.ServerNetChannel = function ( aDelegate ) {
3934
this.clients = new SortedLookupTable();
4035

4136
this.setDelegate( aDelegate );
@@ -46,35 +41,42 @@ Version:
4641
};
4742

4843
RealtimeMultiplayerGame.network.ServerNetChannel.prototype = {
49-
httpserver : null, // A minimal HTTP server which socket.io can listen on
50-
socketio : null, // Socket.IO server
44+
socketio : null, // Socket.IO server
5145
clients : null, // SortedLookupTable
5246
delegate : null, // Should conform to ServerNetChannel delegate
5347
outgoingSequenceNumber : 0, // A unique ID for each message
5448
cmdMap : {}, // Map the CMD constants to functions
55-
// Methods
49+
50+
// Methods
5651
/**
5752
* Initializes socket.io
5853
*/
59-
setupSocketIO: function() {
60-
// Create a minimal http server to listen
61-
this.httpserver = http.createServer(function(req, res) {});
62-
this.httpserver.listen( RealtimeMultiplayerGame.Constants.SERVER_SETTING.SOCKET_PORT );
63-
// Start socket.io
64-
this.socketio = io.listen(this.httpserver);
65-
54+
setupSocketIO: function () {
55+
var server = require( 'http' ).createServer( function ( req, res ) {
56+
} );
57+
server.listen( RealtimeMultiplayerGame.Constants.SERVER_SETTING.SOCKET_PORT );
58+
this.socketio = require( 'socket.io' ).listen( server );
6659

6760
var that = this;
68-
this.socketio.on('connection', function(client){ that.onSocketConnection(client) });
69-
this.socketio.on('clientMessage', function(data, client){ that.onSocketMessage( data, client ) });
70-
this.socketio.on('clientDisconnect', function(client){ that.onSocketClosed(client) });
61+
this.socketio.on( 'connection', function ( client ) {
62+
console.log(client);
63+
that.onSocketConnection( client )
64+
} );
65+
this.socketio.on( 'clientMessage', function ( data, client ) {
66+
console.log(data)
67+
that.onSocketMessage( data, client )
68+
} );
69+
this.socketio.on( 'clientDisconnect', function ( client ) {
70+
console.log(client)
71+
that.onSocketClosed( client )
72+
} );
7173
},
7274

73-
setupWSServer: function() {
75+
setupWSServer: function () {
7476

75-
var profiler = require('v8-profiler');
76-
var util = require('util');
77-
var ws = require("../lib/bonsai-ws/ws.js");
77+
var profiler = require( 'v8-profiler' );
78+
var util = require( 'util' );
79+
var ws = require( "../lib/bonsai-ws/ws.js" );
7880

7981
this.clientCount = 0;
8082
this.maxClients = 8;
@@ -83,29 +85,29 @@ Version:
8385
var that = this;
8486

8587
this.$ = new ws.Server( false );
86-
this.$.onConnect = function(conn) {
88+
this.$.onConnect = function ( conn ) {
8789
var aClient = new RealtimeMultiplayerGame.network.Client( conn, that.getNextClientID() );
8890

8991
// Send the first message back to the client, which gives them a clientid
90-
var connectMessage = new RealtimeMultiplayerGame.model.NetChannelMessage( ++this.outgoingSequenceNumber, aClient.getClientid(), true, RealtimeMultiplayerGame.Constants.CMDS.SERVER_CONNECT, { gameClock: that.delegate.getGameClock() });
92+
var connectMessage = new RealtimeMultiplayerGame.model.NetChannelMessage( ++this.outgoingSequenceNumber, aClient.getClientid(), true, RealtimeMultiplayerGame.Constants.CMDS.SERVER_CONNECT, { gameClock: that.delegate.getGameClock() } );
9193
connectMessage.messageTime = that.delegate.getGameClock();
92-
aClient.getConnection().send( RealtimeMultiplayerGame.modules.bison.encode(connectMessage) );
94+
aClient.getConnection().send( RealtimeMultiplayerGame.modules.bison.encode( connectMessage ) );
9395

9496
// Add to our list of connected users
9597
that.clients.setObjectForKey( aClient, aClient.getSessionId() );
9698
};
9799

98-
this.$.onMessage = function(conn, msg) {
99-
console.log("MESSAGE RECEIVED", msg);
100+
this.$.onMessage = function ( conn, msg ) {
101+
console.log( "MESSAGE RECEIVED", msg );
100102
};
101103

102-
this.$.onClose = function(conn) {
103-
that.removeClient(conn.$clientID);
104-
console.log("Disconnected!");
104+
this.$.onClose = function ( conn ) {
105+
that.removeClient( conn.$clientID );
106+
console.log( "Disconnected!" );
105107
};
106108

107-
this.removeClient = function(id) {
108-
if (this.socketClients[id]) {
109+
this.removeClient = function ( id ) {
110+
if ( this.socketClients[id] ) {
109111
this.clientCount--;
110112
this.socketClients[id].remove();
111113
delete this.socketClients[id];
@@ -118,19 +120,18 @@ Version:
118120
/**
119121
* Map RealtimeMultiplayerGame.Constants.CMDS to functions
120122
*/
121-
setupCmdMap: function() {
123+
setupCmdMap: function () {
122124
this.cmdMap = {};
123125
this.cmdMap[RealtimeMultiplayerGame.Constants.CMDS.PLAYER_JOINED] = this.onPlayerJoined;
124126
},
125127

126128
/**
127129
* Checks all the clients to see if its ready for a new message.
128130
* If they are, have the client perform delta-compression on the worldDescription and send it off.
129-
* @param gameClock The current (zero-based) game clock
131+
* @param gameClock The current (zero-based) game clock
130132
* @param worldDescription A description of all the entities currently in the world
131133
*/
132-
tick: function( gameClock, worldDescription )
133-
{
134+
tick: function ( gameClock, worldDescription ) {
134135
var worldEntityDescriptionString = worldDescription.getEntityDescriptionAsString();
135136
var entityDescriptionObject = {
136137
entities: worldEntityDescriptionString,
@@ -139,30 +140,29 @@ Version:
139140
};
140141

141142
// Send client the current world info
142-
this.clients.forEach( function(key, client)
143-
{
143+
this.clients.forEach( function ( key, client ) {
144144
// Collapse delta - store the world state
145145
client.entityDescriptionBuffer.push( entityDescriptionObject );
146146

147147
// Ask if enough time passed, and send a new world update
148-
if ( client.canSendMessage(gameClock) ) {
149-
client.sendQueuedCommands(gameClock);
148+
if ( client.canSendMessage( gameClock ) ) {
149+
client.sendQueuedCommands( gameClock );
150150
}
151151

152152
}, this );
153153
},
154154

155-
// Socket.IO callbacks
155+
// Socket.IO callbacks
156156
/**
157157
* Callback from socket.io when a client has connected
158158
* @param clientConnection
159159
*/
160-
onSocketConnection: function( clientConnection ) {
160+
onSocketConnection: function ( clientConnection ) {
161161

162162
var aClient = new RealtimeMultiplayerGame.network.Client( clientConnection, this.getNextClientID() );
163163

164164
// Send the first message back to the client, which gives them a clientid
165-
var connectMessage = new RealtimeMultiplayerGame.model.NetChannelMessage( ++this.outgoingSequenceNumber, aClient.getClientid(), true, RealtimeMultiplayerGame.Constants.CMDS.SERVER_CONNECT, { gameClock: this.delegate.getGameClock() });
165+
var connectMessage = new RealtimeMultiplayerGame.model.NetChannelMessage( ++this.outgoingSequenceNumber, aClient.getClientid(), true, RealtimeMultiplayerGame.Constants.CMDS.SERVER_CONNECT, { gameClock: this.delegate.getGameClock() } );
166166
connectMessage.messageTime = this.delegate.getGameClock();
167167
aClient.getConnection().send( connectMessage );
168168

@@ -174,10 +174,10 @@ Version:
174174
* Callback from socket.io when a client has disconnected
175175
* @param client
176176
*/
177-
onSocketClosed: function( clientConnection ) {
177+
onSocketClosed: function ( clientConnection ) {
178178
var client = this.clients.objectForKey( clientConnection.sessionId );
179-
if(!client) {
180-
console.warn("(ServerNetChannel)::onSocketClosed - ERROR - Attempting to remove client that was not found in our list! ");
179+
if ( !client ) {
180+
console.warn( "(ServerNetChannel)::onSocketClosed - ERROR - Attempting to remove client that was not found in our list! " );
181181
return;
182182
}
183183

@@ -192,54 +192,60 @@ Version:
192192
* @param data
193193
* @param connection
194194
*/
195-
onSocketMessage: function( data, connection )
196-
{
197-
var client = this.clients.objectForKey(connection.sessionId);
195+
onSocketMessage: function ( data, connection ) {
196+
var client = this.clients.objectForKey( connection.sessionId );
198197
//that.CMD_TO_FUNCTION[decodedMessage.cmds.cmd].apply(that, [connection, decodedMessage]);
199198

200199
// Allow the client to track that data was received
201-
if(client) {
200+
if ( client ) {
202201
client.onMessage( data );
203202
} else {
204-
console.log("(NetChannel)::onSocketMessage - no such client!");
203+
console.log( "(NetChannel)::onSocketMessage - no such client!" );
205204
return;
206205
}
207-
206+
208207
//// Call the mapped function, always pass the connection. Also pass data if available
209-
if( this.cmdMap[data.cmd] ) {
210-
this.cmdMap[data.cmd].call(this, client, data);
211-
} else if (this.delegate.cmdMap[data.cmd]) { // See if delegate has function mapped
212-
this.delegate.cmdMap[data.cmd].call(this.delegate, client, data);
208+
if ( this.cmdMap[data.cmd] ) {
209+
this.cmdMap[data.cmd].call( this, client, data );
210+
} else if ( this.delegate.cmdMap[data.cmd] ) { // See if delegate has function mapped
211+
this.delegate.cmdMap[data.cmd].call( this.delegate, client, data );
213212
} else { // Display error
214-
console.log("(NetChannel)::onSocketMessage could not map '" + data.cmd + "' to function!");
213+
console.log( "(NetChannel)::onSocketMessage could not map '" + data.cmd + "' to function!" );
215214
}
216215
},
217216

218-
////// Game callbacks
217+
////// Game callbacks
219218
/**
220219
* Callback for when a player has joined the match.
221220
* Note that joining the match, happens after connecting.
222221
* For example a player might be able to connect to the match, and watch the game for a while then want to join the match
223222
* @param client
224223
* @param data
225224
*/
226-
onPlayerJoined: function( client, data ) {
227-
console.log( client.getClientid() + " joined the game!");
228-
this.delegate.shouldAddPlayer( client.getClientid(), data);
225+
onPlayerJoined: function ( client, data ) {
226+
console.log( client.getClientid() + " joined the game!" );
227+
this.delegate.shouldAddPlayer( client.getClientid(), data );
229228
client.getConnection().send( data );
230229
},
231230

232-
// Accessors
233-
getNextClientID: function() { return ++nextClientID },
231+
/*************
232+
* ACCESSORS *
233+
*************/
234+
235+
236+
getNextClientID: function () {
237+
return ++nextClientID
238+
},
239+
234240
/**
235241
* Checks that an object contains the required methods and sets it as the delegate for this ServerNetChannel instance
236242
* @param {RealtimeMultiplayerGame.network.ServerNetChannelDelegateProtocol} aDelegate A delegate that conforms to RealtimeMultiplayerGame.network.ServerNetChannelDelegateProtocol
237243
*/
238-
setDelegate: function( aDelegate ) {
244+
setDelegate: function ( aDelegate ) {
239245
var theInterface = RealtimeMultiplayerGame.network.ServerNetChannelDelegateProtocol;
240-
for (var member in theInterface) {
246+
for ( var member in theInterface ) {
241247
if ( (typeof aDelegate[member] != typeof theInterface[member]) ) {
242-
console.log("object failed to implement interface member " + member);
248+
console.log( "object failed to implement interface member " + member );
243249
return false;
244250
}
245251
}
@@ -253,12 +259,19 @@ Version:
253259
* Required methods for the ServerNetChannel delegate
254260
*/
255261
RealtimeMultiplayerGame.network.ServerNetChannelDelegateProtocol = {
256-
setupCmdMap: function() {},
257-
shouldUpdatePlayer: function( clientID, data ) {},
258-
shouldAddPlayer: function( clientID, data ) {},
259-
shouldRemovePlayer: function( clientID ) {},
260-
getNextEntityID: function() {},
261-
getGameClock: function() {},
262-
log: function(){}
262+
setupCmdMap: function () {
263+
},
264+
shouldUpdatePlayer: function ( clientID, data ) {
265+
},
266+
shouldAddPlayer: function ( clientID, data ) {
267+
},
268+
shouldRemovePlayer: function ( clientID ) {
269+
},
270+
getNextEntityID: function () {
271+
},
272+
getGameClock: function () {
273+
},
274+
log: function () {
275+
}
263276
}
264277
})();

0 commit comments

Comments
 (0)