-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketTest.js
More file actions
73 lines (56 loc) · 1.62 KB
/
socketTest.js
File metadata and controls
73 lines (56 loc) · 1.62 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var http = require('http');
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(process.env.C9_PORT);
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);
});
}
var mIdx = 0;
var clients = new Array();
function onEventFromClient(data)
{
console.log(data);
console.log(data.my);
}
function onSocketConnected(socket)
{
socket.emit('news', { hello: 'hello world' });
socket.on('my other event', onEventFromClient);
clients[mIdx] = socket.id;
mIdx++;
var refresh = setInterval( function() {
for (var i=0; i < mIdx; i++)
{
var msg = "<br>clients: " + clients[i];
socket.emit('news', {hello: msg});
}
}, 2000 );
socket.on('disconnect', function() {
clearInterval(refresh);
});
//getGoogle();
}
io.sockets.on('connection', onSocketConnected);
function getGoogle() {
var google = http.createClient(80, 'www.google.com');
var request = google.request('GET', '/',
{'host': 'www.google.com'});
request.end();
request.on('response', function (response) {
console.log('STATUS: ' + response.statusCode);
console.log('HEADERS: ' + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
}