-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
194 lines (127 loc) · 3.37 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Module dependencies.
*/
// Command Constants
var IDLE = 0, LOGIN = 1, USERID = 2, LOGOUT = 3, SYNCUSER = 4,NEW = 5,DELETE = 6;
var STARTSYNCCUBES = 99 , ENDSYNCCUBES = 100;
var express = require('express');
var ws = require("websocket-server")
var app = module.exports = express.createServer();
var userGrounp = {};
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', function(req, res){
res.render('Editor', {
title: '3D Editor',
layout: false
});
});
app.listen(3000);
var wsserver = ws.createServer();
wsserver.addListener("connection", function(connection) {
connection.addListener("message", function(msg) {
wsOnMessage(connection, msg);
});
});
function User() {
this.userID = undefined;
this.nickname = undefined;
this.pos = function(){
this.x = undefined;
this.y = undefined;
this.z = undefined;
};
this.color = undefined;
this.offline = true;
}
function Cube(){
this.x = undefined;
this.y = undefined;
this.z = undefined;
this.color = undefined;
}
var Cubes = [];
function wsOnMessage(con, msg) {
console.log("got message: " + msg);
var cmd = msg.split('|');
switch (parseInt(cmd[0]))
{
case LOGIN:
onLogin(con);
break;
case NEW:
for(var i = 0;i<Cubes.length;i++){
if(!Cubes[i]) break;
}
var ___cube = new Cube();
___cube.x = cmd[2];
___cube.y = cmd[3];
___cube.z = cmd[4];
___cube.color = cmd[5];
Cubes[i] = ___cube;
con.broadcast(msg);
break;
case DELETE:
for(var i= 0;i<Cubes.length;i++){
if(Cubes[i]) {
if(Cubes[i].x == cmd[2] && Cubes[i].y == cmd[3] && Cubes[i].z == cmd[4]) Cubes[i] = undefined;
}
}
con.broadcast(msg);
break;
case LOGOUT:
userGrounp[parseInt(cmd[1])].offline = true;
userGrounp[parseInt(cmd[1])] = undefined;
}
}
function onLogin(con) {
for(var id = 0;id<99;id++){
if(!userGrounp[id] || userGrounp[id].offline == true){
break;
}
}
console.log("Sending UID:" + id);
var LoginUser = new User();
LoginUser.userID = id;
LoginUser.offline = false;
userGrounp[id] = LoginUser;
var str = [USERID,id].join('|');
con.send(str);
// let other client know a new user has joined
str = [LOGIN,id].join('|');
con.broadcast(str);
// synchronize other clients that joined before this connection
for (var k in userGrounp) {
if (k != id && userGrounp.hasOwnProperty(k) && userGrounp[k].offline == false) {
var u = userGrounp[k];
var msg = [SYNCUSER, u.userID].join('|');
con.send(msg);
}
}
var str = [STARTSYNCCUBES].join('|');
con.send(str);
for (var i= 0;i<Cubes.length;i++){
if(Cubes[i]){
var str = [NEW,id,Cubes[i].x,Cubes[i].y,Cubes[i].z,Cubes[i].color].join('|');
con.send(str);
}
}
var str = [ENDSYNCCUBES].join('|');
con.send(str);
}
wsserver.listen(8080);
console.log("Express server listening on port %d", app.address().port);