@@ -38,12 +38,16 @@ class Server
38
38
private array $ allowContentTypes = [
39
39
'application/json ' ,
40
40
'application/x-compress ' ,
41
+ 'application/x-e2e-compress+json ' ,
42
+ 'application/x-e2e-json ' ,
41
43
];
42
44
43
45
private array $ allowClient = [];
44
46
45
- const PING_CONTENT = "\x05\x22\x09" ;
46
- const PONG_CONTENT = "\x05\x22\x0A" ;
47
+ const PING_CONTENT = "\x05\x22\x09" ;
48
+ const PONG_CONTENT = "\x05\x22\x0A" ;
49
+ const FLAG_COMPRESS = 0x0001 ;
50
+ const FLAG_ENCRYPTION = 0x0002 ;
47
51
48
52
public function __construct (
49
53
protected LoggerInterface $ logger ,
@@ -278,9 +282,13 @@ private function onRequest(Request $request, Response $response): void
278
282
279
283
$ rawBody = $ request ->getContent ();
280
284
281
- if ('application/x-compress ' === $ contentType ) {
285
+ $ isCompress = 'application/x-compress ' === $ contentType || 'application/x-e2e-compress+json ' === $ contentType ;
286
+ $ isEncryption = 'application/x-e2e-json ' === $ contentType || 'application/x-e2e-compress+json ' === $ contentType ;
287
+
288
+ if ($ isCompress && !$ isEncryption ) {
282
289
$ message = zlib_decode ($ rawBody );
283
290
$ messageSize = sprintf ('%s(compress: %s) ' , format_byte (strlen ($ message )), format_byte (strlen ($ rawBody )));
291
+ $ isCompress = false ;
284
292
} else {
285
293
$ message = $ rawBody ;
286
294
$ messageSize = format_byte (strlen ($ message ));
@@ -299,26 +307,39 @@ private function onRequest(Request $request, Response $response): void
299
307
)
300
308
);
301
309
302
- $ this ->broadcast ($ clientId , $ message );
310
+ $ this ->broadcast ($ clientId , $ message, $ isCompress , $ isEncryption );
303
311
}
304
312
305
- private function broadcast (string $ clientId , string $ message ): void
313
+ private function broadcast (string $ clientId , string $ message, bool $ isCompress , bool $ isEncryption ): void
306
314
{
307
315
$ fds = $ this ->broadcastMap [$ clientId ] ?? [];
308
316
if (empty ($ fds )) {
309
317
return ;
310
318
}
311
319
$ total = \count ($ fds );
312
320
$ i = 0 ;
321
+
322
+ $ frameFlags = $ isCompress
323
+ ? SWOOLE_WEBSOCKET_FLAG_FIN
324
+ : SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS ;
325
+
326
+ $ opcode = $ isEncryption ? WEBSOCKET_OPCODE_BINARY : WEBSOCKET_OPCODE_TEXT ;
327
+
328
+ if (WEBSOCKET_OPCODE_BINARY === $ opcode ) {
329
+ $ _f = 0 ;
330
+ if ($ isCompress ) {
331
+ $ _f |= self ::FLAG_COMPRESS ;
332
+ }
333
+ if ($ isEncryption ) {
334
+ $ _f |= self ::FLAG_ENCRYPTION ;
335
+ }
336
+ $ message = "\x05\x21" . pack ('n ' , $ _f ) . $ message ;
337
+ }
338
+
313
339
foreach ($ fds as $ fd ) {
314
340
$ i ++;
315
341
$ this ->logger ->debug ("broadcast message to # {$ fd }[ {$ i }/ $ total]. " );
316
- $ this ->server ->push (
317
- $ fd ,
318
- $ message ,
319
- WEBSOCKET_OPCODE_TEXT ,
320
- SWOOLE_WEBSOCKET_FLAG_FIN | SWOOLE_WEBSOCKET_FLAG_COMPRESS ,
321
- );
342
+ $ this ->server ->push ($ fd , $ message , $ opcode , $ frameFlags );
322
343
}
323
344
}
324
345
0 commit comments