-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.js
More file actions
37 lines (31 loc) · 911 Bytes
/
client.js
File metadata and controls
37 lines (31 loc) · 911 Bytes
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
'use strict';
const socket = new WebSocket('ws://127.0.0.1:8000/');
const buildAPI = (methods) => {
const api = {};
for (const method of methods) {
api[method] = (...args) =>
new Promise((resolve) => {
socket.send(JSON.stringify({ method, args }));
socket.onmessage = (event) => {
const data = JSON.parse(event.data);
resolve(data);
};
});
}
return api;
};
const api = buildAPI(['rect', 'move', 'rotate', 'read', 'render', 'resize']);
const show = async () => {
const svg = await api.render('Rect1');
const output = document.getElementById('output');
output.innerHTML = svg;
};
const scenario = async () => {
await api.rect('Rect1', -10, 10, 10, -10);
await api.move('Rect1', 5, 5);
await api.rotate('Rect1', 5);
const data = await api.read('Rect1');
console.dir({ data });
await show();
};
setTimeout(scenario, 1000);