diff options
Diffstat (limited to 'socket.io/sio3.js')
-rw-r--r-- | socket.io/sio3.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/socket.io/sio3.js b/socket.io/sio3.js new file mode 100644 index 0000000..56e3cbe --- /dev/null +++ b/socket.io/sio3.js @@ -0,0 +1,55 @@ +/* probably want to use this approach for socket.io */ + +var express = require('express'); +var http = require('http'); +var io = require('socket.io'); +var path = require('path'); +var util = require('util'); + +var app = express(); +var server = http.createServer(app).listen(8081); +var sio = io.listen(server); + +app.use(express.logger('dev')); +app.use(express.favicon()); + +app.all('*', function(req, res, next) { + util.log('trace, url: ' + req.url); + console.log('you are %s:%s and your socket is %s', res.connection.myip, res.connection.myport, res.connection.mytag); + next(); +}); + +app.get('/hello', function (req, res) { + /* console.log(util.inspect(req, false, 7)); */ + res.send('hello, world!'); +}); + +app.use(express.static(path.join(__dirname, 'public'))); + +/* teh fuck why this also gets invoked, fucking nodejs and js, clusterfuck god damn it */ +server.on('connection', function(socket) { + /* socket.myid = crypto.createHash('md5').update(socket.remoteAddress + ':' + socket.remotePort).digest('hex'); */ + util.log('[server] new node connection from ' + socket.remoteAddress + ':' + socket.remotePort); + socket.myip = socket.remoteAddress; + socket.myport = socket.remotePort; + socket.mytag = 'node socket'; + socket.on('close', function() { + /* delete connected_clients */ + util.log('[server] client ' + this.myip + ':' + this.myport + ' closed node connection\n'); + /* this.port2? why this keyword? above callback gets executed in socket object, that's why this works? */ + }); +}) + +sio.sockets.on('connection', function (socket) { + var endpoint = socket.manager.handshaken[socket.id].address; + util.log('[server] new socket.io connection from ' + endpoint.address + ':' + endpoint.port); + socket.myip = endpoint.address; + socket.myport = endpoint.port; + socket.mytag = 'socket.io socket'; + /* console.log(util.inspect(socket, false, 7)); find ip here */ + socket.emit('news', 'hello there stranger'); + socket.on('my other event', function (data) { + console.log(data); + }); +}); + |