nativescript - CocoaAsyncSocket 和 NativeScript 问题
问题描述
我正在为基于 CocoaAsyncSocket 的 UDP 通信编写 Nativescript 插件,但是当我尝试在此 pod 的上下文中执行任何异步操作时,我的应用程序崩溃了。这似乎与 iOS x {N} 上的队列/线程有关,但我无法解决它,知道吗?
这是我在 Typescript 中创建 Plugin iOS 类的方式:
export class UdpProtocol extends NSObject implements GCDAsyncUdpSocketDelegate
这是我初始化我的方式GCDAsyncUdpSocket
:
let dispatchQueue = dispatch_queue_create("sendUnicast_queue", null);
let sock = GCDAsyncUdpSocket.alloc()
.initWithDelegateDelegateQueue(this, dispatchQueue);
这是我在 .ts 中调用的异步方法的一个示例:
sock.sendDataToHostPortWithTimeoutTag(sendData, address, port, -1, this.tag);
这是我期望正确触发的回调:
udpSocketDidSendDataWithTag(sock: GCDAsyncUdpSocket, tag: number): void {
console.log('udpSocketDidSendDataWithTag callback');
}
但不幸的是,我得到的是下面的 JS 异常:
CONSOLE LOG file:///node_modules/@nativescript/core/inspector_modules.js:1:0: Loading inspector modules...
CONSOLE LOG file:///node_modules/@nativescript/core/inspector_modules.js:6:0: Finished loading inspector modules.
NativeScript debugger attached.
***** Fatal JavaScript exception - application has been terminated. *****
Native stack trace:
1 0x11033960e NativeScript::reportFatalErrorBeforeShutdown(JSC::ExecState*, JSC::Exception*, bool)
2 0x110388f76 -[TNSRuntimeInspector reportFatalError:]
3 0x10fc2d23b TNSInspectorUncaughtExceptionHandler
4 0x7fff23e3a36d __handleUncaughtException
5 0x7fff50ad7c05 _objc_terminate()
6 0x7fff4f926c87 std::__terminate(void (*)())
7 0x7fff4f926c29 std::terminate()
8 0x7fff519128df _dispatch_client_callout
9 0x7fff5191860c _dispatch_lane_serial_drain
10 0x7fff51919044 _dispatch_lane_invoke
11 0x7fff519230c4 _dispatch_workloop_worker_thread
12 0x7fff51b37a3d _pthread_wqthread
13 0x7fff51b36b77 start_wqthread
JavaScript stack trace:
UIApplicationMain(file:///node_modules/@nativescript/core/application/application.js:312:0)
at run(file:///node_modules/@nativescript/core/application/application.js:312:0)
at file:///node_modules/@nativescript/angular/platform-common.js:210:0
at file:///node_modules/@nativescript/angular/platform-common.js:111:0
at file:///node_modules/@nativescript/angular/platform-common.js:91:0
at file:///app/bundle.js:198:144
at ./main.ts(file:///app/bundle.js:203:34)
at __webpack_require__(file:///src/webpack/bootstrap:750:0)
at checkDeferredModules(file:///src/webpack/bootstrap:43:0)
at webpackJsonpCallback(file:///src/webpack/bootstrap:30:0)
at anonymous(file:///app/bundle.js:2:61)
at evaluate([native code])
at moduleEvaluation([native code])
at [native code]
at asyncFunctionResume([native code])
at [native code]
at promiseReactionJob([native code])
JavaScript error:
file:///node_modules/@nativescript/core/application/application.js:312:0: JS ERROR Error: -[__NSCFString bytes]: unrecognized selector sent to instance 0x6000036496c0
NativeScript caught signal 6.
Native Stack:
1 0x11038a251 sig_handler(int)
2 0x7fff51b2f5fd _sigtramp
3 0x7fff51a23f39 itoa64
4 0x7fff51a1fb7c abort
5 0x7fff4f927858 abort_message
6 0x7fff4f926cad std::__terminate(void (*)())
7 0x7fff4f926c29 std::terminate()
8 0x7fff519128df _dispatch_client_callout
9 0x7fff5191860c _dispatch_lane_serial_drain
10 0x7fff51919044 _dispatch_lane_invoke
11 0x7fff519230c4 _dispatch_workloop_worker_thread
12 0x7fff51b37a3d _pthread_wqthread
13 0x7fff51b36b77 start_wqthread
JS Stack unavailable. Current thread hasn't initialized a {N} runtime.
我已经在这里尝试过这样的解决方案,但没有成功。
此分支中提供了带有角度演示的插件
非常欢迎任何提示或帮助:)
解决方案
实际上我发现该错误与我创建sendData变量的方式有关,我已将其更改为:
let sendData: any = NSString.alloc().initWithCString(msg);
到
const sendData: NSData = NSData.alloc().initWithBase64EncodedStringOptions(msg, 1);
推荐阅读
- python - 如何使用列表推导从文件中删除所有逗号、换行符、引号、句点?
- jakarta-ee - SOAP 不支持的媒体异常 text/plain 支持的有:[text/xml]
- c++ - 错误:C++ 中的“此声明没有存储类或类型说明符”
- sqlite - 如何创建虚拟列表以实现“多方”无外键的一对多引用
- powershell - 允许 GPO 禁用远程 Shell
- android - 发布到 Google Play 后,区域设置更改不起作用
- heroku - 在 Heroku 上部署不和谐机器人时出现推送错误
- json - 如何将 hcl tf 文件转换为 json
- c - C中字符串旁边的垃圾
- stripe-payments - 收到“payout.created”Stripe webhook 时,确定它用于哪个 Stripe Connect 自定义帐户的最简单方法是什么?