endel / NativeWebSocket

🔌 WebSocket client for Unity - with no external dependencies (WebGL, Native, Android, iOS, UWP)
Other
1.2k stars 158 forks source link

Can't receive binary data (Unity 2020.1.4f1) in WebGL builds (works in other builds) #19

Closed iurii-provizio closed 4 years ago

iurii-provizio commented 4 years ago

Dear @endel ,

Thank you a lot for the great library. I can't make it work for WebGL builds though (at least in Unity 2020.1.4f1, I haven't tried other versions yet). The same code works just fine in non-WebGL builds. When I enable debug in the jslib, I get in the console (Google Chrome, Windows 10):

webgl.framework.js:3635 [JSLIB WebSocket] Connected.
webgl.framework.js:3293 Connection established 
(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

webgl.framework.js:3639 [JSLIB WebSocket] Received message: ArrayBuffer(11548) {}[[Int8Array]]: Int8Array(11548) [12, 0, 0, 0, 8, 0, 14, 0, 7, 0, 8, 0, 8, 0, 0, 0, 0, 0, 0, 2, 12, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, -49, 2, 0, 0, 78, -47, -41, -64, 83, 116, -36, 64, -49, 68, -72, -65, 96, -61, -77, 63, -40, -93, -70, -64, -14, -46, -7, 64, 85, -63, 104, -66, 96, -61, -77, 63, 53, 17, -36, -64, -72, 30, -41, 64, 43, -87, -53, -65, 58, 35, -62, 63, 9, -118, -65, -64, -8, 49, -10, 64, …][[Int16Array]]: Int16Array(5774) [12, 0, 8, 14, 7, 8, 8, 0, 0, 512, 12, 0, 0, 6, 8, 4, 6, 0, 4, 0, 719, 0, -11954, -16169, 29779, 16604, 17615, -16456, -15520, 16307, -23592, -16198, -11534, 16633, -16043, -16792, -15520, 16307, 4405, -16164, 7864, 16599, -22229, -16437, 9018, 16322, -30199, -16193, 12792, 16630, 0, 0, 9018, 16322, 10486, -16092, 14995, 16493, 15834, -16679, -6606, 16478, 3251, -16103, -25690, 16550, -31793, 16195, 21538, 16073, 10800, -16105, -7235, 16554, 4195, 16280, 14575, 16515, 13946, -16205, -5453, 16655, 23488, -16344, -30828, -16185, 16043, -16201, 29571, 16650, 15833, 16473, 24641, -16347, 2098, -16110, -20447, 16596, 32694, 16157, …][[Int32Array]]: Int32Array(2887) [12, 917512, 524295, 8, 33554432, 12, 393216, 262152, 6, 4, 719, -1059598002, 1088189523, -1078442801, 1068745568, -1061510184, 1090114290, -1100431019, 1068745568, -1059319499, 1087839928, -1077171925, 1069687610, -1061189111, 1089876472, 0, 1069687610, -1054594826, 1080900243, -1093059110, 1079961138, -1055322957, 1084660646, 1061389263, 1053381666, -1055446480, 1084941245, 1066930275, 1082341615, -1061996934, 1091562163, -1071096896, -1060665452, -1061732693, 1091203971, 1079590361, -1071292351, -1055782862, 1087680545, 1058897846, -1088540671, -1054204756, 1081463118, -1098448959, 1079961138, -1054854873, 1084724609, -1092550760, 1079961138, -1055589190, 1086778979, 1070235386, 1079961138, -1054676615, 1085811563, 1044001860, 1076427017, -1055058086, 1087070904, -1103481788, 1076427017, -1056123754, 1089441943, 1072218452, 1076427017, -1054485774, 1085110485, 1044001860, 1076898038, -1055913306, 1089023770, 1070693404, 1076898038, -1055462419, 1088147161, 1062304628, -1088540671, -1055284474, 1087592884, 1065354055, -1095986734, -1054042227, 1083036821, -1083653635, 1079254397, -1054226775, 1085854345, 1052797484, 1076427017, -1055868217, …][[Uint8Array]]: Uint8Array(11548) [12, 0, 0, 0, 8, 0, 14, 0, 7, 0, 8, 0, 8, 0, 0, 0, 0, 0, 0, 2, 12, 0, 0, 0, 0, 0, 6, 0, 8, 0, 4, 0, 6, 0, 0, 0, 4, 0, 0, 0, 207, 2, 0, 0, 78, 209, 215, 192, 83, 116, 220, 64, 207, 68, 184, 191, 96, 195, 179, 63, 216, 163, 186, 192, 242, 210, 249, 64, 85, 193, 104, 190, 96, 195, 179, 63, 53, 17, 220, 192, 184, 30, 215, 64, 43, 169, 203, 191, 58, 35, 194, 63, 9, 138, 191, 192, 248, 49, 246, 64, …][0 … 9999][0 … 99][100 … 199][200 … 299][300 … 399][400 … 499][500 … 599][600 … 699][700 … 799][800 … 899][900 … 999][1000 … 1099][1100 … 1199][1200 … 1299][1300 … 1399][1400 … 1499][1500 … 1599][1600 … 1699][1700 … 1799][1800 … 1899][1900 … 1999][2000 … 2099][2100 … 2199][2200 … 2299][2300 … 2399][2400 … 2499][2500 … 2599][2600 … 2699][2700 … 2799][2800 … 2899][2900 … 2999][3000 … 3099][3100 … 3199][3200 … 3299][3300 … 3399][3400 … 3499][3500 … 3599][3600 … 3699][3700 … 3799][3800 … 3899][3900 … 3999][4000 … 4099][4100 … 4199][4200 … 4299][4300 … 4399][4400 … 4499][4500 … 4599][4600 … 4699][4700 … 4799][4800 … 4899][4900 … 4999][5000 … 5099][5100 … 5199][5200 … 5299][5300 … 5399][5400 … 5499][5500 … 5599][5600 … 5699][5700 … 5799][5800 … 5899][5900 … 5999][6000 … 6099][6100 … 6199][6200 … 6299][6300 … 6399][6400 … 6499][6500 … 6599][6600 … 6699][6700 … 6799][6800 … 6899][6900 … 6999][7000 … 7099][7100 … 7199][7200 … 7299][7300 … 7399][7400 … 7499][7500 … 7599][7600 … 7699][7700 … 7799][7800 … 7899][7900 … 7999][8000 … 8099][8100 … 8199][8200 … 8299][8300 … 8399][8400 … 8499][8500 … 8599][8600 … 8699][8700 … 8799][8800 … 8899][8900 … 8999][9000 … 9099][9100 … 9199][9200 … 9299][9300 … 9399][9400 … 9499][9500 … 9599][9600 … 9699][9700 … 9799][9800 … 9899][9900 … 9999][10000 … 11547]__proto__: TypedArraybyteLength: (...)__proto__: ArrayBufferbyteLength: (...)constructor: ƒ ArrayBuffer()slice: ƒ slice()Symbol(Symbol.toStringTag): "ArrayBuffer"get byteLength: ƒ byteLength()__proto__: Object

webgl.framework.js:3885 Uncaught 25674192
___resumeException @ webgl.framework.js:3885
_ReversePInvokeWrapper_WebSocketFactory_DelegateOnMessageEvent_mE581241D6F0AD9A885B109DF8F0A99B98D04CAF1 @ webgl.wasm:0xb29f06
dynCall_viii @ webgl.wasm:0xbcc891
(anonymous) @ webgl.framework.js:24620
(anonymous) @ webgl.framework.js:3646

Is there anything I could check to see why I get this ___resumeException?

Btw, as suggested in Unity forums, I tried replacing Runtime.dynCall stuff with dynCall_viii(...) like calls. Though works the same as the Runtime version (f.e. successfully handles openning the connection) it still fails (the same way) to deliver the binary message with that ___resumeException.

Thank you a lot!

iurii-provizio commented 4 years ago

Turns out the issue is on the side of C# event handler, possibly related to using reflection there. Closing the issue.

iurii-provizio commented 4 years ago

Reopened: turns out it wasn't bacause of reflection. Though OnMessage handler gets invoked, the length of the message is always 0, even though jslib logs the correct length when debug is enabled there. It works fine in Unity Editor and non-WebGL builds.

endel commented 4 years ago

Hi @iurii-provizio 👋

I couldn't reproduce the problem you've described. Here's what I've done:

Client-side logs (WebGL):

Received OnMessage! (8 bytes) ��A�\s: 
(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

WebGLBuild.framework.js:3304 Received OnMessage! (12 bytes) hello world! 
(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

WebGLBuild.framework.js:3304 Received OnMessage! (8 bytes) �%�p櫙 
(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

WebGLBuild.framework.js:3304 Received OnMessage! (12 bytes) hello world! 
(Filename: ./Runtime/Export/Debug/Debug.bindings.h Line: 35)

Server-side logs:

binary received from client -> 10, 20, 30
string received from client -> 'plain text message'
binary received from client -> 10, 20, 30
string received from client -> 'plain text message'
binary received from client -> 10, 20, 30
string received from client -> 'plain text message'
binary received from client -> 10, 20, 30
string received from client -> 'plain text message'

The logs seem to be receiving binary messages on both sides. Let me know if you see a different result.

(Unity Version: 2020.1.6f1.4510 Personal)

Note: I've also now copied over the latest changes from https://github.com/colyseus/colyseus-unity3d/pull/119 to this project.

iurii-provizio commented 4 years ago

@endel Thank you a lot, the latest version (with colyseus/colyseus-unity3d#119) works fine!