-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclock.html
151 lines (126 loc) · 4.42 KB
/
clock.html
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebSocket Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button id="connectButton">Connect</button>
<button id="createEntityButton" disabled>Create Entity</button>
<button id="moveEntityButton" disabled>Move Entity</button>
<button id="getEntityButton" disabled>Get Entity</button>
<button id="getSnapshotButton" disabled>Get Snapshot</button>
<button id="getEntitiesButton" disabled>Get Entities</button>
<button id="clearOutputButton">Clear Output</button>
<div id="output"></div>
<style>
#output {
max-height: 1200px;
/* adjust this value as per your preference */
overflow-y: auto;
border: 1px solid #ccc;
padding: 10px;
margin-top: 20px;
}
</style>
<script type="text/javascript">
let hzMS = 40;
var socket;
function randomId() {
return Math.random().toString(36).substr(2, 9);
}
let host = true;
let testingInputs = false;
let entityName = randomId();
document.getElementById('connectButton').addEventListener('click', function () {
socket = new WebSocket('ws://192.168.1.80:8787/websocket');
//socket = new WebSocket('ws://192.168.1.80:8888/websocket');
//socket = new WebSocket('wss://ayyo.cloudflare1973.workers.dev/websocket');
socket.onopen = function (event) {
console.log('WebSocket is connected:', event);
['createEntityButton', 'moveEntityButton', 'getEntityButton', 'getSnapshotButton', 'getEntitiesButton'].forEach(function (id) {
document.getElementById(id).disabled = false;
});
if (testingInputs) {
setInterval(() => {
let controls = {
UP: true
};
socket.send(JSON.stringify({ action: 'player_input', controls: controls }));
}, 16);
}
if (host) {
console.log("CREATING GAMETICK INTERVAL")
// Send gameTick signal every 16ms (assuming 60 FPS)
setInterval(() => {
socket.send(JSON.stringify({ action: 'gameTick' }));
}, hzMS);
}
};
let count = 0;
// Modify the socket.onmessage function to use the helper function
socket.onmessage = function (event) {
count++;
let data = JSON.parse(event.data);
if (data.action !== 'GAMETICK') {
console.log('WebSocket message received:', event.data);
$('#output').append('<p>' + event.data + '</p>');
} else {
if (count % 60 === 0) {
}
console.log('WebSocket message received:', event.data);
$('#output').append('<p>' + event.data + '</p>');
}
//scrollToBottom();
};
socket.onclose = function (event) {
console.log('WebSocket is closed:', event);
};
socket.onerror = function (error) {
console.error('WebSocket error:', error);
};
});
function sendMessage(action, data) {
var message = JSON.stringify(Object.assign({ action: action }, data));
socket.send(message);
}
// Clear the output div
document.getElementById('clearOutputButton').addEventListener('click', function () {
$('#output').html('');
});
// A helper function to scroll the output div to the bottom
function scrollToBottom() {
var output = document.getElementById("output");
output.scrollTop = output.scrollHeight;
}
document.getElementById('createEntityButton').addEventListener('click', function () {
sendMessage('createEntity', {
entityId: entityName,
type: 'player',
x: 0,
y: 0
});
});
document.getElementById('moveEntityButton').addEventListener('click', function () {
sendMessage('moveEntity', {
entityId: entityName,
dx: 1,
dy: 1
});
});
document.getElementById('getEntityButton').addEventListener('click', function () {
sendMessage('getEntity', {
entityId: entityName
});
});
document.getElementById('getSnapshotButton').addEventListener('click', function () {
sendMessage('getSnapshot', {});
});
document.getElementById('getEntitiesButton').addEventListener('click', function () {
sendMessage('getEntities', {});
});
</script>
</body>
</html>