-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.js
91 lines (80 loc) · 3.09 KB
/
ws.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
try {
var ws = new WebSocket("ws://193.43.210.54:80");
$("#player_name").focus();
$('#player_name').keyup(function (e) {
if (this.value.trim() == "") {
$('#btn_start').addClass("ui-disabled");
} else {
$('#btn_start').removeClass('ui-disabled');
if (e.which == 13) send_name();
}
});
$('[name=enter_game]').click(function (btn) {
//$('#enter_game').click(function(btn){
console.log(btn.target.value);
ws.send('{"enter_game":"' + btn.target.value + '"}');
if (btn.target.value == "create_game") {
$('#enter_game').hide(1000);
} else if (btn.target.value == "join_game") {
$('#enter_game').hide(1000);
$('#players_list').show(1000);
}
});
$('#player_list').click(function (e) {
console.log(e.target.id);
ws.send('{"selected_player":"' + e.target.id + '"}');
});
function send_name() {
if ($('#player_name').val() != "") {
ws.send('{"player_name":"' + $('#player_name').val() + '"}');
$('#btn_start').hide(1000);
$('#player_name').hide(1000);
$('#enter_game').show(1000);
}
}
ws.onmessage = function (event) {
if (event.data) {
var msg = JSON.parse(event.data);
if(msg.register){
$('#msg').text(msg.register);
}
else if(msg.create_game){
$('#msg').text(msg.create_game);
}
else if(msg.players){
var player_list = $("#player_list");
var j = 0;
for (var key in msg.players) {
j++;
if (Object.keys(msg.players).length == j) {
$('#player_list').append($("<li id='" + key + "' class='ui-li ui-li-static ui-btn-up-c ui-last-child' data-theme='c'/>").text(msg.players[key]))
} else {
$('#player_list').append($("<li id='" + key + "' class='ui-li ui-li-static ui-btn-up-c' data-theme='c'/>").text(msg.players[key]))
}
}
player_list.show();
console.log(msg.players);
}
else if(msg.begin_game){
$('#msg').text(msg.begin_game);
$("#player_list").hide(1000);
}
}
};
ws.onopen = function () {
console.log('open connection');
};
ws.onclose = function (event) {
if (event.wasClean) {
console.log('was clean');
} else {
console.log('not clean');
console.log(event);
}
};
ws.onerror = function (error) {
console.log(error);
};
} catch (e) {
$('body').append(e + '<br>');
}