-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllSubscription.html
132 lines (109 loc) · 4.78 KB
/
AllSubscription.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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Client</title>
</head>
<body>
<h1>WebSocket Client</h1>
<div id="output"></div>
<button id="dataButton">Get Data</button>
<script>
const socket = new WebSocket('ws://localhost:8101');
const ecsSocket = new WebSocket('ws://localhost:8100');
ecsSocket.onopen = function () {
console.log('ECS WebSocket connection opened.');
let message = { action: 'subscribe', type: 'operation', name: 'keyrange' };
ecsSocket.send(JSON.stringify(message));
};
// Define different handlers for different operations or keys
const ecsHandlers = {
'keyrange': function (data) {
console.log(`Received a 'keyrange' operation with data:`);
console.log(data)
console.log(data.data)
const parsedData = JSON.parse(data.data);
console.log(parsedData);
// console.log(data.data)
// for (curData in data.data) {
// console.log(curData);
// console.log(JSON.parse(curData));
// }
// ...do something with the data...
}
};
ecsSocket.onmessage = function (event) {
const data = JSON.parse(event.data);
if (data.type === "operation" && data.operation && ecsHandlers[data.operation]) {
console.log("ecs operation")
ecsHandlers[data.operation](data);
} else {
console.warn(`No ECS handler found for operation "${data.operation}" or key "${data.key}"`);
}
};
ecsSocket.onclose = function (event) {
console.log('ECS WebSocket connection closed:', event);
};
socket.onopen = function () {
console.log('WebSocket connection opened.');
let message = { action: 'subscribe', type: 'operation', name: 'get', get_old_data: 'true' };
socket.send(JSON.stringify(message));
message = { action: 'subscribe', type: 'operation', name: 'put', get_old_data: 'true' };
socket.send(JSON.stringify(message));
message = { action: 'subscribe', type: 'operation', name: 'delete', get_old_data: 'true' };
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:`, data);
// ...do something with the data...
},
'put': function (data) {
console.log(`Received a 'put' operation with data:`, data);
// ...do something with the data...
},
'delete': function (data) {
console.log(`Received a 'delete' 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 if (data.type === "data" && data.data) {
console.log("data")
console.log(data.data)
console.log(JSON.parse(data.data))
console.log(JSON.parse(data.data))
console.log((JSON.parse(data.data))["localhost:8001"])
} else {
console.log(data)
console.warn(`No handler found for operation "${data.operation}" or key "${data.key}"`);
}
};
socket.onclose = function (event) {
console.log('WebSocket connection closed:', event);
};
document.getElementById("dataButton").addEventListener("click", function () {
console.log("Data button was clicked!");
const message = { action: 'data' };
socket.send(JSON.stringify(message));
});
</script>
</body>
</html>