-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocket.flowext.ts
More file actions
249 lines (233 loc) · 9.57 KB
/
WebSocket.flowext.ts
File metadata and controls
249 lines (233 loc) · 9.57 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import type { BlockCategory } from '../types.js';
const COLOR = '#7c3aed'; // violet
const WS_VAR = '_ws';
const WS_PAYLOAD = '_ws_payload';
const WS_CLIENT = '_ws_client_id';
const websocketExtension: BlockCategory = {
id: 'websocket',
name: 'WebSocket',
blocks: [
// ─── Server Begin ─────────────────────────────────────────────────────
{
id: 'ws_begin',
name: 'WS Server Begin',
color: COLOR,
icon: '🔌',
category: 'WebSocket',
description: 'เริ่มต้น WebSocket Server\nClientเชื่อมได้ที่ ws://<ip>:<port>',
inputs: [{ id: 'in', type: 'input', label: '➜', dataType: 'any' }],
outputs: [{ id: 'out', type: 'output', label: '➜', dataType: 'void' }],
params: [
{
id: 'port', type: 'number', label: 'Port', default: '81',
description: 'Port ของ WebSocket Server (ต้องต่างจาก HTTP port)',
validation: (n: number) => Math.trunc(n),
},
],
toCode({ pad, params, registerPreprocessor, registerGlobal, registerFunction, registerPollingCode }) {
registerPreprocessor('#include <WebSocketsServer.h>');
const port = params.port ?? '81';
registerGlobal(`WebSocketsServer ${WS_VAR}(${port});`);
registerGlobal(`String ${WS_PAYLOAD} = "";`);
registerGlobal(`uint8_t ${WS_CLIENT} = 0;`);
registerFunction(
`void _ws_event(uint8_t num, WStype_t type, uint8_t* payload, size_t length)`,
[
` ${WS_CLIENT} = num;`,
` if (type == WStype_CONNECTED) {`,
`#ifdef WS_ON_CONNECTED_CB`,
` WS_ON_CONNECTED_CB();`,
`#endif`,
` } else if (type == WStype_DISCONNECTED) {`,
`#ifdef WS_ON_DISCONNECTED_CB`,
` WS_ON_DISCONNECTED_CB();`,
`#endif`,
` } else if (type == WStype_TEXT) {`,
` ${WS_PAYLOAD} = String((char*)payload);`,
`#ifdef WS_ON_MESSAGE_CB`,
` WS_ON_MESSAGE_CB();`,
`#endif`,
` }`,
].join('\n'),
`void _ws_event(uint8_t, WStype_t, uint8_t*, size_t);`
);
registerPollingCode(`${WS_VAR}.loop();`);
return {
parts: [
[
`${pad}${WS_VAR}.begin();`,
`${pad}${WS_VAR}.onEvent(_ws_event);`,
],
{ portId: 'out', depthDelta: 0 },
]
};
}
},
// ─── On Connected ────────────────────────────────────────────────────
{
id: 'ws_on_connected',
name: 'WS On Connected',
trigger: true,
color: COLOR,
icon: '🟢',
category: 'WebSocket',
description: 'เรียกเมื่อ WebSocket client เชื่อมต่อเข้ามา\nใช้ WS Client ID เพื่ออ่านหมายเลข client',
inputs: [],
outputs: [{ id: 'out', type: 'output', label: '➜', dataType: 'void' }],
toCode({ captureCode, registerPreprocessor, registerFunction }) {
registerPreprocessor('#include <WebSocketsServer.h>');
const body = captureCode('out', 1);
registerFunction(
'void _ws_on_connected()',
body,
'void _ws_on_connected();'
);
registerPreprocessor('#define WS_ON_CONNECTED_CB _ws_on_connected');
return { parts: [] };
}
},
// ─── On Disconnected ─────────────────────────────────────────────────
{
id: 'ws_on_disconnected',
name: 'WS On Disconnected',
trigger: true,
color: COLOR,
icon: '🔴',
category: 'WebSocket',
description: 'เรียกเมื่อ WebSocket client ตัดการเชื่อมต่อ\nใช้ WS Client ID เพื่ออ่านหมายเลข client',
inputs: [],
outputs: [{ id: 'out', type: 'output', label: '➜', dataType: 'void' }],
toCode({ captureCode, registerPreprocessor, registerFunction }) {
registerPreprocessor('#include <WebSocketsServer.h>');
const body = captureCode('out', 1);
registerFunction(
'void _ws_on_disconnected()',
body,
'void _ws_on_disconnected();'
);
registerPreprocessor('#define WS_ON_DISCONNECTED_CB _ws_on_disconnected');
return { parts: [] };
}
},
// ─── On Message ──────────────────────────────────────────────────────
{
id: 'ws_on_message',
name: 'WS On Message',
trigger: true,
color: COLOR,
icon: '📨',
category: 'WebSocket',
description: 'เรียกเมื่อรับข้อความจาก WebSocket client\nใช้ WS Payload และ WS Client ID เพื่ออ่านข้อมูล',
inputs: [],
outputs: [{ id: 'out', type: 'output', label: '➜', dataType: 'void' }],
toCode({ captureCode, registerPreprocessor, registerFunction }) {
registerPreprocessor('#include <WebSocketsServer.h>');
const body = captureCode('out', 1);
registerFunction(
'void _ws_on_message()',
body,
'void _ws_on_message();'
);
registerPreprocessor('#define WS_ON_MESSAGE_CB _ws_on_message');
return { parts: [] };
}
},
// ─── Payload ─────────────────────────────────────────────────────────
{
id: 'ws_payload',
name: 'WS Payload',
color: COLOR,
icon: '📦',
category: 'WebSocket',
description: 'ข้อความที่ได้รับจาก WebSocket client (_ws_payload)\nใช้ภายใน WS On Message เท่านั้น',
inputs: [],
outputs: [{ id: 'value', type: 'output', label: 'Payload', dataType: 'String' }],
toExpr: () => WS_PAYLOAD,
toCode() { return { parts: [] }; }
},
// ─── Client ID ───────────────────────────────────────────────────────
{
id: 'ws_client_id',
name: 'WS Client ID',
color: COLOR,
icon: '🆔',
category: 'WebSocket',
description: 'หมายเลข client ที่ส่งข้อความมา (_ws_client_id)\nใช้ส่งกลับหา client เดิมด้วย WS Send',
inputs: [],
outputs: [{ id: 'value', type: 'output', label: 'Client ID', dataType: 'int' }],
toExpr: () => WS_CLIENT,
toCode() { return { parts: [] }; }
},
// ─── Send to Client ───────────────────────────────────────────────────
{
id: 'ws_send',
name: 'WS Send',
color: COLOR,
icon: '📤',
category: 'WebSocket',
description: 'ส่งข้อความไปยัง WebSocket client ที่ระบุ (ws.sendTXT)',
inputs: [
{ id: 'in', type: 'input', label: '➜', dataType: 'any' },
{ id: 'client_id', type: 'input', label: 'Client ID', dataType: 'int', description: 'หมายเลข client ที่ต้องการส่ง' },
{ id: 'message', type: 'input', label: 'Message', dataType: 'any', description: 'ข้อความที่จะส่ง' },
],
outputs: [{ id: 'out', type: 'output', label: '➜', dataType: 'void' }],
params: [
{
id: 'client_id', type: 'number', label: 'Client ID', default: '0',
validation: (n: number) => Math.max(0, Math.trunc(n)),
},
{ id: 'message', type: 'text', label: 'Message', default: 'Hello' },
],
toCode({ block, pad, safeId, params, resolveInput, registerPreprocessor, registerGlobal }) {
registerPreprocessor('#include <WebSocketsServer.h>');
registerGlobal(`WebSocketsServer ${WS_VAR}(81);`);
const id = safeId(block.id);
const cid = resolveInput('client_id') ?? params.client_id ?? '0';
const message = resolveInput('message') ?? `"${(params.message ?? 'Hello').replaceAll('"', '\\"')}"`;
return {
parts: [
[
`${pad}String ${id}_msg = String(${message});`,
`${pad}${WS_VAR}.sendTXT(${cid}, ${id}_msg);`,
],
{ portId: 'out', depthDelta: 0 },
]
};
}
},
// ─── Broadcast ───────────────────────────────────────────────────────
{
id: 'ws_broadcast',
name: 'WS Broadcast',
color: COLOR,
icon: '📡',
category: 'WebSocket',
description: 'ส่งข้อความไปยัง WebSocket client ทุกตัวที่เชื่อมต่ออยู่ (ws.broadcastTXT)',
inputs: [
{ id: 'in', type: 'input', label: '➜', dataType: 'any' },
{ id: 'message', type: 'input', label: 'Message', dataType: 'any' },
],
outputs: [{ id: 'out', type: 'output', label: '➜', dataType: 'void' }],
params: [
{ id: 'message', type: 'text', label: 'Message', default: 'Hello' },
],
toCode({ block, pad, safeId, params, resolveInput, registerPreprocessor, registerGlobal }) {
registerPreprocessor('#include <WebSocketsServer.h>');
registerGlobal(`WebSocketsServer ${WS_VAR}(81);`);
const id = safeId(block.id);
const message = resolveInput('message') ?? `"${(params.message ?? 'Hello').replaceAll('"', '\\"')}"`;
return {
parts: [
[
`${pad}String ${id}_msg = String(${message});`,
`${pad}${WS_VAR}.broadcastTXT(${id}_msg);`,
],
{ portId: 'out', depthDelta: 0 },
]
};
}
},
]
};
export default websocketExtension;