-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmultiplayer_server.js
More file actions
93 lines (68 loc) · 1.99 KB
/
Copy pathmultiplayer_server.js
File metadata and controls
93 lines (68 loc) · 1.99 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
/*
* Multiplayer Engine Demo
* @author Josh Gibbs - uPaymeiFixit@gmail.com
*/ console.log("v3r0613121918 at " + new Date());
var port = 4000,
TXspeed = 1000/30,
clients = [],
cID = [],
sID = [],
io;
( function()
{
io = require( "socket.io" ).listen( port );
io.set( "log level", 1 );
// TX Loop
setInterval( function()
{
io.sockets.volatile.emit( "RX", clients );
}, TXspeed );
// Start connection
io.sockets.on( "connection", function( socket )
{
return socket.on( "firstConnect", function( client )
{
socket.broadcast.emit( "addClient", client );
socket.emit( "firstConnect", clients, cID[socket.id] = sID.push( socket.id ) - 1 );
clients.push( client );
console.log("Client with SID: " + socket.id + " and CID: " + cID[socket.id] + " has connected.");
console.log("There are now " + sID.length + " clients.");
socket.on( "TX", function( client )
{
clients[cID[socket.id]] = client;
});
socket.on( "message", function( message )
{
socket.broadcast.send( message, cID[socket.id] );
});
socket.on( "c_eval", function( string )
{
socket.broadcast.emit( "c_eval", string );
});
socket.on( "s_eval", function( string )
{
console.log('Evaluating: "' + string + '"');
eval( string ); // Screw you JSlint, eval is not always evil
});
// TODO Clean up functions that contain only single functions
socket.on( "ping", function( time )
{
socket.emit( "ping", time );
});
return socket.on( "disconnect", function()
{
console.log("Client with SID: " + socket.id + " and CID: " + cID[socket.id] + " is disconnecting.");
socket.broadcast.emit( "removeClient", clients[cID[socket.id]], cID[socket.id] );
clients.splice(cID[socket.id],1);
sID.splice(cID[socket.id],1);
var n = cID[socket.id];
delete cID[socket.id];
for ( var i = n; i < sID.length; i++ )
{
--cID[sID[i]];
}
console.log("There are now " + sID.length + " clients.");
});
});
});
}).call(this);