NathanaelA / nativescript-websockets

Websockets for NativeScript
83 stars 43 forks source link

[Bug] webSocketInstance._notify is not a function #94

Closed lapaliv closed 2 years ago

lapaliv commented 2 years ago

Hello. I have an error when I am trying to use your library. I am using:

The code:

require('@master.technology/websockets');

const client = new WebSocket('ws://localhost:3000');

client.on('open', function() {
  console.log('ws connected');
});

client.on('close', function(event, code, reason) {
  console.log('ws closed', code, reason);
});

client.on('error', function(event, error) {
  console.log('ws error', error);
});

client.on('message', function(event, message) {
  console.log('ws msg', message);
});

And the error:

***** Fatal JavaScript exception - application has been terminated. *****
NativeScript encountered a fatal error: Uncaught TypeError: webSocketInstance._notify is not a function
at
URLSessionWebSocketTaskDidOpenWithProtocol(file: app/webpack:/test-project/node_modules/@master.technology/websockets/websockets-base.ios.js:42:0)
(CoreFoundation) *** Terminating app due to uncaught exception 'NativeScript encountered a fatal error: Uncaught TypeError: webSocketInstance._notify is not a function
at
URLSessionWebSocketTaskDidOpenWithProtocol(file: app/webpack:/test-project/node_modules/@master.technology/websockets/websockets-base.ios.js:42:0)
', reason: '(null)'
*** First throw call stack:
(
0   CoreFoundation                      0x00007fff20422fba __exceptionPreprocess + 242
1   libobjc.A.dylib                     0x00007fff20193ff5 objc_exception_throw + 48
2   NativeScript                        0x000000010b6893ee _ZN3tns21NativeScriptException15OnUncaughtErrorEN2v85LocalINS1_7MessageEEENS2_INS1_5ValueEEE + 880
3   NativeScript                        0x000000010ba710f4 _ZN2v88internal14MessageHandler25ReportMessageNoExceptionsEPNS0_7IsolateEPKNS0_15MessageLocationENS0_6HandleINS0_6ObjectEEENS_5LocalINS_5ValueEEE + 356
4   NativeScript                        0x000000010ba70f53 _ZN2v88internal14MessageHandler13ReportMessageEPNS0_7IsolateEPKNS0_15MessageLocationENS0_6HandleINS0_15JSMessageObjectEEE + 883
5 <…>

I added console.log before 42 line:

// ...
console.log('_notify', 'object', webSocketInstance, 'type', typeof webSocketInstance._notify)
webSocketInstance._notify("open", [webSocketInstance]);
// ...

and got it:

CONSOLE LOG: _notify object {
"NOT_YET_CONNECTED": -1,
"CONNECTING": 0,
"OPEN": 1,
"CLOSING": 2,
"CLOSED": 3,
"_debug": false,
"_queue": [],
"_queueRunner": null,
"_protocols": [],
"_allowCell": true,
"_headers": [],
"_webSocketDelegate": {
"wrapper": {},
"debug": false
},
"_nsWebSocketTask": {},
"binaryType": "arraybuffer",
"onclose": null,
"onerror": null,
"onmessage": null,
"onopen": null
} type undefined

Thank you!

lapaliv commented 2 years ago

And while doing so, I successfully connected to the server, but could not continue to work with it

NathanaelA commented 2 years ago

This should be fixed in v2.02.

Archez commented 2 years ago

This error still occurs with v2.0.2 when using the browser based method.

In the BrowserWebSockets class, if you comment out the line, then it works as expected (unless doing this breaks something else).

Object.defineProperty(this, "_notify", {enumerable: false});

This seems to make the NativeWebSockets class it extends not have reference to the function.


If the intent is to not have the property enumerable, then to correctly modify the property I believe we would want this instead

Object.defineProperty(this, "_notify", {enumerable: false, value: this._notify});
NathanaelA commented 2 years ago

That is interesting. I'll do an update.

NathanaelA commented 2 years ago

I've just removed all the "function/property" hiding code, let me know if you have any other issues in 2.03. Technically the defineproperty code should work in a class constructor, but must have some weird issue with _notify being also in the superclass. In all reality as I was already debating stripping that code in the ES6 rewrite since it is mainly pointless.