-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketexample.js
35 lines (30 loc) · 932 Bytes
/
socketexample.js
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
var http = require('http');
var socketio = require('socket.io');
var fs = require('fs');
var handler = function(req, res) {
fs.readFile(__dirname + '/index.html', function(err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
} else {
res.writeHead(200);
res.end(data);
}
});
};
var app = http.createServer(handler);
var io = socketio.listen(app);
io.sockets.on('connection', function (socket) {
setInterval(function() {
var timestamp = Date.now();
console.log('Emitted: ' + timestamp);
socket.emit('timer', timestamp);
}, 2000);
socket.on('submit', function(data) {
console.log('Submitted: ' + data);
});
});
// use process.env.PORT and process.env.IP for Cloud9
// or replace with your port and (optionally) IP as necessary
app.listen(8080, "localhost");
console.log('Server running!');