-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
150 lines (125 loc) · 4.11 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
140
141
142
143
144
145
146
147
148
149
150
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
"use strict";
require('newrelic');
// Optional. You will see this name in eg. 'ps' or 'top' command
process.title = 'node-tiles';
// Port where we'll run the websocket server
var webSocketsServerPort = process.env.PORT || 5000;
// websocket and http servers
var WebSocketServer = require('ws').Server;
var http = require('http');
var url = require("url");
var st = require('node-static');
/**
* Global variables
*/
// latest 100 messages
var history = [ ];
// list of currently connected clients (users)
var clients = [ ];
/**
* Helper function for escaping input strings
*/
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<')
.replace(/>/g, '>').replace(/"/g, '"');
}
// Letter positions array
var tiles = [];
var tile = 0;
for(var s = 0; s < 4; s++) {
for(var a = 0; a < 26; a++) {
var tileObj = {
s: s,
a: a,
x: 25 + (a * 30),
y: 25 + (s * 30),
id: tile++,
letter: 'abcdefghijklmnopqrstuvwxyz'[a]
};
tiles.push(tileObj);
}
}
// Array with some colors
var colors = [ 'red', 'green', 'blue', 'magenta', 'purple', 'plum', 'orange' ];
// ... in random order
colors.sort(function(a,b) { return Math.random() > 0.5; } );
var express = require('express');
var path = require('path');
var app = express();
// all environments
app.set('port', webSocketsServerPort);
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
/**
* HTTP server
*/
var server = http.createServer(app);
server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});
/**
* WebSocket server
*/
var wsServer = new WebSocketServer({
server: server
});
wsServer.on('connection', function(socket) {
console.log((new Date()) + ' Connection from origin ' + socket.address + '.');
var index = clients.push(socket) - 1;
var userName = false;
var userColor = false;
// user sent some message
socket.on('message', function(message) {
var inbound = JSON.parse(message);
if (inbound.type === 'logon') { // accept only text
if (userName === false) { // first message sent by user is their name
// remember user name
userName = inbound.name;
console.log((new Date()) + ' User is known as: ' + userName);
socket.send(JSON.stringify( { type: 'history', data: tiles} ));
}
}
if (inbound.type === 'dstart' || inbound.type === 'dstop') {
var broadcastData = {
u: userName,
id: inbound.id
};
// broadcast message to all connected clients
var json = JSON.stringify({ type: inbound.type, data: broadcastData });
for (var i=0; i < clients.length; i++) {
clients[i].send(json);
}
}
if (inbound.type === 'cds') {
tiles[inbound.id].x = inbound.x;
tiles[inbound.id].y = inbound.y;
var broadcastData = {
u: userName,
id: inbound.id,
x: inbound.x,
y: inbound.y
};
// broadcast message to all connected clients
var json = JSON.stringify({ type:'cds', data: broadcastData });
for (var i=0; i < clients.length; i++) {
clients[i].send(json);
}
}
});
// user disconnected
socket.on('close', function(connection) {
if (userName !== false && userColor !== false) {
console.log((new Date()) + " Peer "
+ connection.remoteAddress + " disconnected.");
// remove user from the list of connected clients
clients.splice(index, 1);
// push back user's color to be reused by another user
colors.push(userColor);
}
});
});