-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
37 lines (29 loc) · 853 Bytes
/
server.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
36
37
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const socket = require('socket.io');
const io = socket(server);
// Configuration
// Routes
let messages = [];
var port = process.env.PORT || 5000; // Use the port that Heroku provides or default to 5000
app.listen(port, function() {
console.log('Express server listening');
});
app.get('/', (req, res, next) => {
res.send(messages);
});
let socketPool = [];
io.on('received message', function(data) {
messages.push(data);
socketPool.forEach(socket => {
socket.emit('received message', data);
});
});
io.on('connection', function(socket) {
socketPool.push(socket);
socket.emit('new connection', socket);
socket.on('message', function(data) {
io.emit('received message', data);
});
});