Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50664 views
1
// Socket.io configuration for Flatiron
2
// -------------------------------------------------- //
3
4
var flatiron = require('../../lib/flatiron'),
5
fs = require("fs"),
6
app = flatiron.app;
7
8
app.use(flatiron.plugins.http, {
9
10
before: [function (req, res) {
11
12
fs.readFile(__dirname + '/index.html', function (err, data) {
13
14
if (err) {
15
res.writeHead(500);
16
return res.end('Error loading index.html');
17
}
18
19
res.writeHead(200);
20
res.end(data);
21
22
});
23
24
}]
25
});
26
27
28
// Set the server to listen on port `8080`.
29
// It is important to do this first, as app.server
30
// isn't actually created until you start()
31
app.start(8080);
32
33
34
// Socket.io
35
// -------------------------------------------------- //
36
37
var io = require('socket.io').listen(app.server);
38
39
io.sockets.on('connection', function(socket) {
40
socket.emit('news', { hello: 'world' });
41
socket.on('my other event', function(data) {
42
console.log(data);
43
});
44
});
45
46