summaryrefslogtreecommitdiffstats
path: root/socket.io/sio3.js
blob: 56e3cbe95156a1b89a1cca6e76178e5a0ed13567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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);
    });
});