This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
58 lines (47 loc) · 1.83 KB
/
app.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
ent = require('ent'),
express = require("express"),
fs = require('fs');
app.use('/', express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', function (socket, username) {
socket.on('new_client', function(message) {
if (message.username == null || message.room == null)
{
message.username = "Anonyme";
message.room = "1984";
}
else
{
socket.username = ent.encode(message.username);
socket.room = ent.encode(message.room);
}
socket.join(socket.room);
socket.to(socket.room).emit('new_user', {username: socket.username});
});
socket.on('roll_dices', function(message) {
socket.broadcast.to(socket.room).emit('roll_dices', {username: socket.username});
});
socket.on('remove_dice', function(message) {
socket.broadcast.to(socket.room).emit('remove_dice', {username: socket.username});
console.log(socket.username + ' removed');
});
socket.on('add_dice', function(message) {
socket.broadcast.to(socket.room).emit('add_dice', {username: socket.username});
});
socket.on('change_room', function(message) {
socket.broadcast.to(socket.room).emit('leave_room', {username: socket.username});
socket.leave(socket.room);
socket.room = ent.encode(message.room);
socket.join(socket.room);
socket.to(socket.room).emit('new_user', {username: socket.username});
});
socket.on('disconnect', function() {
socket.broadcast.to(socket.room).emit('user_leave', {username: socket.username});
});
});
server.listen(process.env.PORT || 8080);