-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
56 lines (49 loc) · 1.59 KB
/
app.js
File metadata and controls
56 lines (49 loc) · 1.59 KB
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
// Conflict between express.js 3.x and socket.io:
// var express = require('express');
// var expressApp = express();
//
// var app = require('http').createServer(expressApp)
// ,io = require('socket.io').listen(app)
//
// wrap an another object to work-around
//
//
// external packages:
// socket.server
// cluster.io => graphically show benchmark(presure testing) of socket.io on current server
var app = require('http').createServer(handler)
,io = require('socket.io').listen(app)
,fs = require('fs');
app.listen(3333);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
// trigger event when
io.sockets.on('connection', function (socket) {
console.log('User ' + socket.id + ' connected!');
// socket.io configuration
io.configure(fucntion() {
// set data into socalled 'handshake'
io.set('authorization', function(handshakeData) {
handshakeData.hello = 'word';
callback(null, true); // error first callback
})
});
// if client trigger say event to server, server proxy the message to other user.
socket.on('say', function(data) {
// retrieve previos setted cookie data
console.log(sockets.handshake.hello);
io.sockets.emit('sayHandler', {
id: socket.id,
text: data.text
});
});
});