-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimpsock.js
56 lines (50 loc) · 1.42 KB
/
impsock.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
ig.module(
'plugins.impsock'
)
.requires(
'impact.impact'
)
.defines(function(){
ig.Impsock = ig.Class.extend({
init: function(game) {
this.host = null;
this.port = 8080;
this.game = game;
this.sessionID = null;
this.socket = null;
this.clients = null;
this.otherClients = null;
this.disconnectedClients = [];
this.establishSocket();
},
establishSocket: function() {
var game = this.game;
var self = this;
this.socket = new io.Socket(this.host,{rememberTransport: false, port: this.port, secure: false});
this.socket.on('connect', function(){
self.sessionID = self.socket.transport.sessionid;
});
this.socket.on('message', function(messageFromServer){
self.receive(messageFromServer);
});
this.socket.connect();
},
broadcast: function(e) {
var properties = e.syncableProperties();
properties["sessionId"] = e.name;
var message = {"action": "broadcast", "entity": properties};
this.socket.send(message);
},
receive: function(messageFromServer) {
if (messageFromServer.spawn) {
this.game.joinHandler(messageFromServer);
return;
}
if (messageFromServer.remove) {
this.game.disconnectHandler(messageFromServer);
return;
}
this.game.broadcastHandler(messageFromServer);
}
});
});