diff --git a/client/index.html b/client/index.html index 815bb7b..3ceffd1 100644 --- a/client/index.html +++ b/client/index.html @@ -4,10 +4,12 @@ - nodePong - v0.0.1 + nodePong, a multiplayer online pong based on nodejs and socket.io + + @@ -19,10 +21,38 @@ + + + + + diff --git a/client/js/NPGClient.js b/client/js/NPGClient.js new file mode 100644 index 0000000..6649891 --- /dev/null +++ b/client/js/NPGClient.js @@ -0,0 +1,8 @@ +/** + * @file NPGClient.js + * @author frtk + */ + +var NPGClient = { 'version': '' }; + + diff --git a/client/js/socket/SocketIO.js b/client/js/socket/SocketIO.js new file mode 100644 index 0000000..81da11a --- /dev/null +++ b/client/js/socket/SocketIO.js @@ -0,0 +1,77 @@ +/** + * @file SocketIO.js + * @author frtk + */ + +NPGClient.SocketIO = { + + // Connection to server + conn_nAttempts: 0, + conn_IntervalID: 0, + socket: {}, + isConnected: false, + + // start connection loop + initConnectLoop: function() { + var self = this; + console.log(); + self.startConnectLoop(); + }, + // start connection loop + startConnectLoop: function() { + var self = this; + self.conn_IntervalID = setInterval(function() { self.connect(); }, 1500); + }, + // stop connection loop + stopConnectLoop: function() { + var self = this; + clearInterval(self.conn_IntervalID); + self.conn_IntervalID = 0; + self.conn_nAttempts = 0; + }, + // connect socket + connect: function() { + var self = this; + // check for connection + if (self.isConnected) { + return false; + } + // + if (typeof io !== 'object') { + //console.log('[NPGClient.SocketIO] io object is unknown'); + } + if ((typeof self.socket.socket === 'undefined')) { + console.log('[NPGClient.SocketIO] attempting server connection...'); + self.socket = io.connect(); + self.defineSocketMsgs(); + } else if (!self.socket.socket.connected && !self.socket.socket.connecting) { + console.log('[NPGClient.SocketIO] attempting server connection...'); + self.socket.socket.connect(); + } + }, + // Define Socket Messages + defineSocketMsgs: function() { + var self = this; + if (typeof self.socket === 'undefined') { + return false; + } + console.log('[NPGClient.SocketIO] deploying socket object features.'); + // 'connecting' + self.socket.on('connecting', function () { + console.log('[NPGClient.SocketIO] connecting ...'); + }); + // 'connect' + self.socket.on('connect', function() { + console.log('[NPGClient.SocketIO] connected to server'); + self.stopConnectLoop(); + self.isConnected = true; + }); + // 'disconnect' + self.socket.on('disconnect', function() { + console.log('[NPGClient.SocketIO] > Server: socket disconnected.'); + self.startConnectLoop(); + self.isConnected = false; + }); + } + +}; diff --git a/nodePong.js b/nodePong.js index cdfe781..c2a96e8 100644 --- a/nodePong.js +++ b/nodePong.js @@ -11,7 +11,6 @@ var fs = require('fs'); var express = require('express'); var http = require('http'); -var iosocket = require('socket.io'); //--- server app libs & params //var servP = require('./server/server_config.js'); var nPong = require('./server/server_core.js'); @@ -38,13 +37,18 @@ app.use(express.static(__dirname + '/client/')); //--- socket.io nPong.Server.log('$ # registering socket.io service on port ' + config.HTTP.port); -var io = require('socket.io').listen(httpserv, {log: false}); +var io = require('socket.io').listen(httpserv, { log: true } ); +//-- setup server socket handling features +nPong.Server.socketHandling(io); + // nPong.Server.log('$ #####'); + + /* * running nodePong server */ diff --git a/server/server_core.js b/server/server_core.js index 552255e..80d0fdd 100644 --- a/server/server_core.js +++ b/server/server_core.js @@ -37,7 +37,10 @@ var Server = { this.version = s; }, - + //--- setIO(io) + setIO: function(io) { + + }, /* * Server Messages @@ -78,7 +81,28 @@ var Server = { return self.dateToString(new Date()); }, + + /* + * Socket.io handling + */ + //--- + socketHandling: function(io) { + var self = this; + io.sockets.on('connection', function (socket) { + + // + self.log('$ User connected : id=' + socket.id); + + // 'disconnect' + socket.on('disconnect', function () { + self.log('$ User disconnected : id=' + socket.id); + connected = false; + }); + + }); + }, + }; diff --git a/version.md b/version.md index 1a0ed5b..606bf9a 100644 --- a/version.md +++ b/version.md @@ -1,5 +1,12 @@ +### **v0.0.6:** +- focus on socket.io client & server) +- Renamed SocketClient.js to SocketIO.js (files and objects) +- Added SocketIO object (NGPClient lib in /client/js/) +- Implemented socket.io client connection (with loop) => working +- Client side: Corrected bugs of multiple connection attemps and logs +- Server side: moved socket definitions to Server object ### **v0.0.5:**