This commit is contained in:
frtk 2016-02-10 18:48:47 +01:00
parent f4bc521bb3
commit d27f957f82
6 changed files with 156 additions and 6 deletions

View File

@ -4,10 +4,12 @@
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<title>nodePong - v0.0.1</title> <title>nodePong, a multiplayer online pong based on nodejs and socket.io</title>
<link rel="stylesheet" type="text/css" media="all" href="./css/main.css"> <link rel="stylesheet" type="text/css" media="all" href="./css/main.css">
<script type="text/javascript" src="http://127.0.0.1:8042/socket.io/socket.io.js"></script> <script type="text/javascript" src="http://127.0.0.1:8042/socket.io/socket.io.js"></script>
<script type="text/javascript" src="./src/jquery-2.1.3.min.js"></script> <script type="text/javascript" src="./src/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="./js/NPGClient.js"></script>
<script type="text/javascript" src="./js/socket/SocketIO.js"></script>
</head> </head>
<body> <body>
@ -19,10 +21,38 @@
</html> </html>
<!--
<script type="text/javascript">
//console.log(io);
var socket = {};
if (typeof io !== 'object') {
console.log('Client: io object is unknown');
}
if ((typeof self.socket.socket === 'undefined')) {
console.log('[#SocketClient] attempting server connection...');
socket = io.connect(); // socket connection
console.log('[#SocketClient] attempting server connection...');
//socket.socket.connect();
}
socket.emit('data', 'yo');
</script>
-->
<!--
<script type="text/javascript">
console.log(io);
// NPGClient.io = io;
// NPGClient.SocketClient.setIO(io);
NPGClient.SocketClient.connect();
NPGClient.SocketClient.socket.emit('data','yo');
</script>
-->
<script type="text/javascript"> <script type="text/javascript">
$(document).ready(function() { $(document).ready(function() {
//GT4UI.SocketClient.connectSocket(); NPGClient.SocketIO.startConnectLoop();;
//GT4UI.Status.init("status"); if (NPGClient.SocketIO.isConnected) NPGClient.SocketIO.socket.emit('data','yo');
}); });
</script> </script>

8
client/js/NPGClient.js Normal file
View File

@ -0,0 +1,8 @@
/**
* @file NPGClient.js
* @author frtk
*/
var NPGClient = { 'version': '' };

View File

@ -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;
});
}
};

View File

@ -11,7 +11,6 @@
var fs = require('fs'); var fs = require('fs');
var express = require('express'); var express = require('express');
var http = require('http'); var http = require('http');
var iosocket = require('socket.io');
//--- server app libs & params //--- server app libs & params
//var servP = require('./server/server_config.js'); //var servP = require('./server/server_config.js');
var nPong = require('./server/server_core.js'); var nPong = require('./server/server_core.js');
@ -38,13 +37,18 @@ app.use(express.static(__dirname + '/client/'));
//--- socket.io //--- socket.io
nPong.Server.log('$ # registering socket.io service on port ' + config.HTTP.port); 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('$ #####'); nPong.Server.log('$ #####');
/* /*
* running nodePong server * running nodePong server
*/ */

View File

@ -37,7 +37,10 @@ var Server = {
this.version = s; this.version = s;
}, },
//--- setIO(io)
setIO: function(io) {
},
/* /*
* Server Messages * Server Messages
@ -78,7 +81,28 @@ var Server = {
return self.dateToString(new Date()); 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;
});
});
},
}; };

View File

@ -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:** ### **v0.0.5:**