-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
44 lines (34 loc) · 1.32 KB
/
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
38
39
40
41
42
43
44
require('dotenv').config();
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var uuid = require('node-uuid');
var port = process.env.PORT || 3000;
var debug_env_val = process.env.DEBUG || false;
var debug = (debug_env_val === 'true');
app.get('/', function(req, res) {
res.redirect(301, '/' + uuid.v4());
});
app.get('/:room', function(req, res) {
console.log('Room requested: ' + req.params.room);
res.sendfile('index.html');
});
app.use(express.static('public'));
io.on('connection', function(socket) {
var ROOM_ID = (typeof socket.handshake.headers.referer === 'undefined')? 'no-room' : socket.handshake.headers.referer.replace(/^.*?([^\/]*?)\/{0,1}$/g,"$1");
socket.on('sock.message', function(msg) {
io.emit('sock.broadcast.' + ROOM_ID + '.' + msg.sender.id, msg);
});
socket.on('sock.join.' + ROOM_ID, function(user){
io.emit('sock.enter.' + ROOM_ID, user);
if (debug) console.log('User (' + user.id + ') connected to room: ' + ROOM_ID);
});
socket.on('sock.ack.' + ROOM_ID, function(ack) {
io.emit('sock.ack.' + ROOM_ID + '.' + ack.to, ack.from);
if (debug) console.log('User (' + ack.from.id + ') acknowledged: ' + ack.to);
});
});
http.listen(port, function() {
console.log('listening on *:' + port);
});