-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·139 lines (122 loc) · 3.61 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
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 http = require("http");
var url = require("url");
var io = require("socket.io");
var counts = 0;
var clients = []
var clientSockets = [];
/*
http.createServer(function (request, response) {
console.log('Req received! ');
response.writeHead(200, {"Content-Type:": "text/plain"});
response.write("Hello world!!!!!");
response.end();
}).listen(8888);
*/
function start(route, handle) {
function onRequest(request, response) {
var postData = "";
var pathname = url.parse(request.url).pathname;
console.log("Request received for " + pathname + " received.");
route(handle, pathname, response, request, counts);
}
var server = http.createServer(onRequest).listen(process.env.PORT || 5000);
var iol = io.listen(server);
iol.set('log level', 1);
iol.configure('production', function(){
iol.enable('browser client etag');
iol.set('log level', 1);
iol.set('transports', [
'websocket'
, 'flashsocket'
, 'htmlfile'
, 'xhr-polling'
, 'jsonp-polling'
]);
});
iol.on('connection', function(socket){
socket.set('log level', 1);
clientSockets.push(socket);
counts++;
emitToAll(counts);
var new_player = {
id: Math.floor((Math.random()*1000)+1),
x:4361,
y:40,
z:960,
position:{},
quaternion:
{
x:0,
y:0,
z:0,
w:0,
},
angle:0,
//socket:socket;
}
clients.push(new_player);
socket.emit('connected', {'uniqueID':new_player.id});
console.log('Client connected... id: '+new_player.id);
//send data to client
setInterval(function(){
socket.broadcast.emit('allPlayerPositions', clients);
}, 500);
socket.on('disconnect', function () {
console.log('Client disconnected!');
counts--;
socket.broadcast.emit('count', {'count':counts});
socket.broadcast.emit('remove', {'removeID':new_player.id});
var dc = clientSockets.indexOf(socket);
var re = clients.indexOf(new_player);
clients.splice(re,1);
delete clientSockets[dc];
clientSockets.splice(dc,1);
});
socket.on('position', function (data){
var pl = clients.indexOf(new_player);
new_player.x = data.x;
new_player.y = data.y;
new_player.z = data.z;
new_player.position = data.position;
new_player.quaternion = data.quaternion;
new_player.id = data.id;
//console.log('data received: '+data.x);
var pl = clients.indexOf(new_player);
//console.log('changing lient coord: '+pl);
clients[pl].x = data.x;
clients[pl].y = data.y;
clients[pl].position = data.position;
clients[pl].quaternion = data.quaternion;
clients[pl].z = data.z;
//process.stdout.write(data.letter);
});
function emitPositionToAll(counts){
//io.sockets.emit('count', {'count':counts});
socket.emit('count', {'count':counts});
socket.broadcast.emit('count', {'count':counts});
//socket.emit('count', {'count':counts});
}
function emitToAll(counts){
//io.sockets.emit('count', {'count':counts});
socket.emit('count', {'count':counts});
socket.broadcast.emit('count', {'count':counts});
//socket.emit('count', {'count':counts});
}
});
//io.set('log level', 1);
//var io = io.listen(http.createServer(onRequest).listen(process.env.PORT || 5000););
//io.listen(server);
/*
io.sockets.on('connection', function(socket){
socket.emit('message', {'message': 'hello world'});
});
io.sockets.on('connection', function(socket){
//send data to client
setInterval(function(){
socket.emit('date', {'date': new Date()});
}, 1000);
});
*/
console.log("Server has started.");
}
exports.start = start;