-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainController.js
63 lines (49 loc) · 1.49 KB
/
mainController.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
const NODES = ["ws://176.9.104.200:6031"];
const PONG_ADDRESS = 'iz3pongServer';
//*******************************************************
const MESSAGES = {handshake: 'HDSHK', left: 'LFT', right: 'RGT', playerName: 'NM', win:'WIN', lose:'LOSE'};
let leftDown = false, rightDown = false;
$('#left').on('mousedown touchstart',function (e) {
console.log(e);
e.preventDefault();
leftDown = true;
});
$('#left').on('mouseup touchend',function (e) {
e.preventDefault();
leftDown = false;
});
$('#right').on('mousedown touchstart',function (e) {
e.preventDefault();
rightDown = true;
});
$('#right').on('mouseup touchend',function (e) {
e.preventDefault();
rightDown = false;
});
/**
* Send message
* @param {string} to
* @param {object} message
* @param {string} messageId
*/
function sendMessage(to, message, messageId) {
let msg = candy.starwave.createMessage(message, to, undefined, messageId);
candy.starwave.sendMessage(msg);
}
let candy = new Candy(NODES).start();
candy.onready = function () {
setTimeout(function () {
sendMessage(PONG_ADDRESS, {hello: 'Hello'}, MESSAGES.handshake);
}, 1000);
};
candy.starwave.registerMessageHandler(MESSAGES.playerName, function (message) {
document.getElementById('playerNo').innerText = message.data.name;
});
setInterval(function () {
if(leftDown) {
sendMessage(PONG_ADDRESS, {}, MESSAGES.left);
}
if(rightDown) {
sendMessage(PONG_ADDRESS, {}, MESSAGES.right);
}
}, 10);