-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameClient.js
66 lines (59 loc) · 2.16 KB
/
GameClient.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
define([
'AbstractPlayer',
'AbstractGame'
], function (
/** AbstractPlayer */ AbstractPlayer,
/** AbstractGame */ AbstractGame
) {
class GameClient extends AbstractGame {
constructor(gameId, url, player, client) {
super();
this.gameId = gameId;
this.player = player;
this.client = client;
this.socket = new WebSocket("ws://" + url + "/?gameId=" + gameId + "&playerId=" + player.id);
this.client.setLeaveCallback(() => {
this.socket.close(1000); //1000 Normal closure; the connection successfully completed whatever purpose for which it was created.
});
this.socket.onclose = (e) => {
if (e.code === 1003) { // 1003 — The connection is being terminated because the endpoint received data of a type it cannot accept
this.client.notify("Невозможно войти в игру");
} else {
this.client.notify("Сервер разорвал соединение");
}
};
this.socket.onmessage = (e) => {
let message = JSON.parse(e.data);
switch(message.command) {
case 'NOTIFY':
this.client.notify(message.message, message.board);
break;
case 'MAKEMOVE':
this.client.makeMove(message.token, message.board, this.checkMove.bind(this));
break;
}
};
}
checkMove(token, x, y) {
let message = JSON.stringify({
command: 'CHECKMOVE',
token: token,
x: x,
y: y
});
this.sendMessage(message);
}
getStatus() {
let message = JSON.stringify({
command: 'GETSTATUS'
});
this.sendMessage(message);
}
sendMessage(message) {
if(this.socket.readyState === 1) {
this.socket.send(message);
}
}
}
return GameClient;
});