-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (113 loc) · 3.25 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var fs = require('fs');
var express = require('express');
var app = express();
var Moniker = require('moniker');
var path = require('path');
var elastical = require('elastical');
const { Client } = require('@elastic/elasticsearch')
var vnc = require('./src/server.js');
var port = process.env.PORT || 5000;
var server = new vnc.Server();
app.engine('ejs', require('express-ejs-extend')); // add this line
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, '/views'));
app.use(express.static(__dirname + '/public'));
app.get("/room/:id", function(req, res){
var id = req.params.id;
var title = 'room'
res.render('room', {
room: id,
title: title
});
});
app.get("/", function(req, res){
var title = 'Home'
res.render('home', {
title: title
});
});
var http = require('http').createServer(app);
var io = require('socket.io')(http);
setTimeout(function() { io.sockets.emit('restart') }, 5000);
// socket.io
io.on('connection', function (socket) {
var user = addUser(socket.handshake.address.address);
var board, room;
socket.emit("welcome", user);
socket.on('join', function(data) {
console.log({data});
room = data.room;
socket.join(room);
user.room = room;
updateUsers();
server.join(user.name, room);
board = server.boards[room];
socket.emit("board", board);
});
socket.on('disconnect', function() {
removeUser(user);
server.unjoin(user.name);
});
socket.on('chat', function(data) {
var m1 = '<strong>' + data.username + ': <em>' + data.message + '</em></strong>';
var m2 = data.username + ': ' + data.message;
socket.emit("updateChat", { message: m1 });
socket.broadcast.to(room).emit("updateChat", { message: m2 });
});
socket.on('send', function(data) {
try {
io.sockets.in(room).emit("board", vnc.Board.prototype.move.call(board, data.message));
} catch (e) {
handleException(e);
}
});
socket.on('new', function() {
try {
io.sockets.in(room).emit("board", vnc.Board.prototype.newGame.call(board));
} catch (e) {
handleException(e);
}
});
socket.on('loadgame', function(data) {
try {
io.sockets.in(room).emit("board", vnc.Board.prototype.loadGame.call());
} catch (e) {
handleException(e);
}
});
var handleException = function(e) {
console.log(e);
};
});
var users = [];
var addUser = function(address) {
var user = {
name: Moniker.choose(),
ip: address
}
users.push(user);
updateUsers();
return user;
}
var removeUser = function(user) {
for(var i=0; i<users.length; i++) {
if(user.name === users[i].name) {
users.splice(i, 1);
updateUsers();
return;
}
}
}
var updateUsers = function() {
io.sockets.emit("users", { users: users, count: users.length });
}
var findUser = function(username) {
for(var i=0; i<users.length; i++) {
if(username === users[i].name) {
return users[i];
}
}
}
http.listen(port, function(){
console.log('listening on *:'+port);
});