-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSubscription.html
71 lines (56 loc) · 2.27 KB
/
getSubscription.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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Client</title>
</head>
<body>
<h1>WebSocket Client</h1>
<div id="output"></div>
<script>
const socket = new WebSocket('ws://localhost:8765');
socket.onopen = function () {
console.log('WebSocket connection opened.');
let message = { action: 'subscribe', type: 'operation', name: 'get' };
socket.send(JSON.stringify(message));
// message = { action: 'subscribe', type: 'operation', name: 'put' };
// socket.send(JSON.stringify(message));
// message = { action: 'subscribe', type: 'key', name: 'myKey' };
// socket.send(JSON.stringify(message));
};
// Define different handlers for different operations or keys
const operationHandlers = {
'get': function(data) {
console.log(`Received a 'get' operation with data:`, JSON.stringify(data));
// ...do something with the data...
},
'put': function(data) {
console.log(`Received a 'put' operation with data:`, data);
// ...do something with the data...
}
};
const keyHandlers = {
'myKey': function(data) {
console.log(`Received data for 'myKey':`, data);
// ...do something with the data...
},
// Add more handlers as needed...
};
socket.onmessage = function (event) {
const data = JSON.parse(event.data);
// Call the appropriate handler based on the operation or key
if (data.type === "key" && data.key && keyHandlers[data.key]) {
console.log("here")
keyHandlers[data.key](data);
} else if (data.type === "operation" && data.operation && operationHandlers[data.operation]) {
console.log("oeprationn")
operationHandlers[data.operation](data);
} else {
console.warn(`No handler found for operation "${data.operation}" or key "${data.key}"`);
}
};
socket.onclose = function (event) {
console.log('WebSocket connection closed:', event);
};
</script>
</body>
</html>