-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.js
More file actions
160 lines (101 loc) · 4.36 KB
/
server.js
File metadata and controls
160 lines (101 loc) · 4.36 KB
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
/*
*@autor: Rio 3D Studios
*@description: java script server that works as master server of the Basic Example of WebGL Multiplayer Kit
*/
var express = require('express');//import express NodeJS framework module
var app = express();// create an object of the express module
var http = require('http').Server(app);// create a http web server using the http library
var io = require('socket.io')(http);// import socketio communication module
app.use("/public/TemplateData",express.static(__dirname + "/public/TemplateData"));
app.use("/public/Build",express.static(__dirname + "/public/Build"));
app.use(express.static(__dirname+'/public'));
var clients = [];// to storage clients
var clientLookup = {};// clients search engine
var sockets = {};//// to storage sockets
//open a connection with the specific client
io.on('connection', function(socket){
//print a log in node.js command prompt
console.log('A user ready for connection!');
//to store current client connection
var currentUser;
var sended = false;
//create a callback fuction to listening EmitPing() method in NetworkMannager.cs unity script
socket.on('PING', function (_pack)
{
//console.log('_pack# '+_pack);
var pack = JSON.parse(_pack);
console.log('message from user# '+socket.id+": "+pack.msg);
//emit back to NetworkManager in Unity by client.js script
socket.emit('PONG', socket.id,pack.msg);
});
//create a callback fuction to listening EmitJoin() method in NetworkMannager.cs unity script
socket.on('LOGIN', function (_data)
{
console.log('[INFO] JOIN received !!! ');
var data = JSON.parse(_data);
// fills out with the information emitted by the player in the unity
currentUser = {
name:data.name,
position:data.position,
rotation:'0',
id:socket.id,//alternatively we could use socket.id
socketID:socket.id//fills out with the id of the socket that was open
};//new user in clients list
console.log('[INFO] player '+currentUser.name+': logged!');
console.log('[INFO] currentUser.position '+currentUser.position);
//add currentUser in clients list
clients.push(currentUser);
//add client in search engine
clientLookup[currentUser.id] = currentUser;
sockets[currentUser.id] = socket;//add curent user socket
console.log('[INFO] Total players: ' + clients.length);
/*********************************************************************************************/
//send to the client.js script
socket.emit("LOGIN_SUCCESS",currentUser.id,currentUser.name,currentUser.position);
//spawn all connected clients for currentUser client
clients.forEach( function(i) {
if(i.id!=currentUser.id)
{
//send to the client.js script
socket.emit('SPAWN_PLAYER',i.id,i.name,i.position);
}//END_IF
});//end_forEach
// spawn currentUser client on clients in broadcast
socket.broadcast.emit('SPAWN_PLAYER',currentUser.id,currentUser.name,currentUser.position);
});//END_SOCKET_ON
//create a callback fuction to listening EmitMoveAndRotate() method in NetworkMannager.cs unity script
socket.on('MOVE_AND_ROTATE', function (_data)
{
var data = JSON.parse(_data);
if(currentUser)
{
currentUser.position = data.position;
currentUser.rotation = data.rotation;
// send current user position and rotation in broadcast to all clients in game
socket.broadcast.emit('UPDATE_MOVE_AND_ROTATE', currentUser.id,currentUser.position,currentUser.rotation);
}
});//END_SOCKET_ON
// called when the user desconnect
socket.on('disconnect', function ()
{
if(currentUser)
{
currentUser.isDead = true;
//send to the client.js script
//updates the currentUser disconnection for all players in game
socket.broadcast.emit('USER_DISCONNECTED', currentUser.id);
for (var i = 0; i < clients.length; i++)
{
if (clients[i].name == currentUser.name && clients[i].id == currentUser.id)
{
console.log("User "+clients[i].name+" has disconnected");
clients.splice(i,1);
};
};
}
});//END_SOCKET_ON
});//END_IO.ON
http.listen(process.env.PORT ||3000, function(){
console.log('listening on *:3000');
});
console.log("------- server is running -------");