Skip to content

Commit 4a99275

Browse files
committed
Add example js
1 parent 045651b commit 4a99275

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

examples/example.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<html>
2+
<head>
3+
<script>
4+
const baseUrl = 'ws://localhost:8783/ws';
5+
6+
let agentInit = false;
7+
agentWs = new WebSocket(`${baseUrl}/agent`);
8+
agentWs.addEventListener('error', console.error);
9+
agentWs.addEventListener('close', () => console.log('agent close'));
10+
agentWs.addEventListener('message', (m) => {
11+
if (!agentInit) {
12+
agentInit = true;
13+
const agentInfo = JSON.parse(m.data);
14+
console.log('Agent connected:', agentInfo);
15+
console.log('Connect remote using:');
16+
console.log(`remoteWs.send('{ "id": "${agentInfo.id}" }');`);
17+
return;
18+
}
19+
20+
console.log('agent:', m.data);
21+
});
22+
agentWs.addEventListener('open', () => {
23+
agentWs.send(JSON.stringify({
24+
meta: {
25+
platform: 'linux',
26+
version: '1.2.3'
27+
}
28+
}));
29+
});
30+
31+
remoteWs = new WebSocket(`${baseUrl}/remote`);
32+
remoteWs.addEventListener('error', console.error);
33+
remoteWs.addEventListener('close', () => console.log('remote close'));
34+
remoteWs.addEventListener('message', (m) => console.log('remote:', m.data));
35+
</script>
36+
</head>
37+
<body></body>
38+
</html>

0 commit comments

Comments
 (0)