编组 objective c 到 nativescript js
Marshalling objective c to nativescript js
您好,我正在尝试将以下 cocoa pod 用于 ios 中的 tcp 功能:
https://cocoapods.org/pods/CocoaAsyncSocket
我在使用此库编写编组 js 时遇到问题
这是一个例子 (Objective C):
// The most common way to initialize an instance is simply like this:
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![socket connectToHost:@"deusty.com" onPort:80 error:&err]) // Asynchronous!
{
// If there was an error, it's likely something like "already connected" or "no delegate set"
NSLog(@"I goofed: %@", err);
}
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Cool, I'm connected! That was easy.");
}
JS代码:
// mainQueue var is to get dispatch_get_main_queue
var mainQueue = (function() {
var runloop = CFRunLoopGetMain();
return function(func) {
CFRunLoopPerformBlock(runloop, kCFRunLoopDefaultMode, func);
CFRunLoopWakeUp(runloop);
}
}());
var tcpClient = GCDAsyncSocket.alloc().initWithDelegateDelegateQueue(testerClass,mainQueue);
var e = new interop.Reference();
if (!tcpClient.connectToHostOnPortError('192.168.88.110',3333,e)) {
console.log('Could not connect to mipbook');
console.log(e.value);
}
function socketDidConnectToHost(sock,host,port) {
console.log('connected to host');
}
连接到端口部分工作正常,但连接成功时未调用委托实例方法。
好的,我开始工作了
问题在于编组 dispatch_get_main_queue()。我通过在所用 pod 的源代码中编辑 GDCAsyncSocket.m 让我的脚本工作。
GDCAsyncSocket.m:
delegateQueue = dq;
改为
delegateQueue = dispatch_get_main_queue();
这样dispatch_get_main_queue()就不需要js端传了,它的值是在objective c库中求值的
这是有效的 JS 代码:
var tcpClientDelegate = NSObject.extend({
socketDidConnectToHostPort(sock,host,port) {
console.log('connected to host: '+host);
console.log('connected to port: '+port);
}
}, {
protocols: [GCDAsyncSocketDelegate]
});
var clientInstance = new tcpClientDelegate();
var tcpClient = GCDAsyncSocket.alloc().initWithDelegateDelegateQueue(clientInstance,null);
var e = new interop.Reference();
if (!tcpClient.connectToHostOnPortError('192.168.88.110',3333,e)) {
console.log('Could not connect to mipbook');
console.log(e.value);
}
试过这个:
let delegate = ...
let dispatchQueue = dispatch_queue_create("test_queue", null);
let udp = GCDAsyncSocket.alloc().initWithDelegateDelegateQueue(delegate, dispatchQueue);
而且效果很好。也应该适用于 GCDAsyncSocket
。
由于某种原因 dispatch_get_main_queue() 未定义。
您好,我正在尝试将以下 cocoa pod 用于 ios 中的 tcp 功能: https://cocoapods.org/pods/CocoaAsyncSocket
我在使用此库编写编组 js 时遇到问题
这是一个例子 (Objective C):
// The most common way to initialize an instance is simply like this:
socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
NSError *err = nil;
if (![socket connectToHost:@"deusty.com" onPort:80 error:&err]) // Asynchronous!
{
// If there was an error, it's likely something like "already connected" or "no delegate set"
NSLog(@"I goofed: %@", err);
}
- (void)socket:(GCDAsyncSocket *)sender didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"Cool, I'm connected! That was easy.");
}
JS代码:
// mainQueue var is to get dispatch_get_main_queue
var mainQueue = (function() {
var runloop = CFRunLoopGetMain();
return function(func) {
CFRunLoopPerformBlock(runloop, kCFRunLoopDefaultMode, func);
CFRunLoopWakeUp(runloop);
}
}());
var tcpClient = GCDAsyncSocket.alloc().initWithDelegateDelegateQueue(testerClass,mainQueue);
var e = new interop.Reference();
if (!tcpClient.connectToHostOnPortError('192.168.88.110',3333,e)) {
console.log('Could not connect to mipbook');
console.log(e.value);
}
function socketDidConnectToHost(sock,host,port) {
console.log('connected to host');
}
连接到端口部分工作正常,但连接成功时未调用委托实例方法。
好的,我开始工作了 问题在于编组 dispatch_get_main_queue()。我通过在所用 pod 的源代码中编辑 GDCAsyncSocket.m 让我的脚本工作。
GDCAsyncSocket.m:
delegateQueue = dq;
改为
delegateQueue = dispatch_get_main_queue();
这样dispatch_get_main_queue()就不需要js端传了,它的值是在objective c库中求值的
这是有效的 JS 代码:
var tcpClientDelegate = NSObject.extend({
socketDidConnectToHostPort(sock,host,port) {
console.log('connected to host: '+host);
console.log('connected to port: '+port);
}
}, {
protocols: [GCDAsyncSocketDelegate]
});
var clientInstance = new tcpClientDelegate();
var tcpClient = GCDAsyncSocket.alloc().initWithDelegateDelegateQueue(clientInstance,null);
var e = new interop.Reference();
if (!tcpClient.connectToHostOnPortError('192.168.88.110',3333,e)) {
console.log('Could not connect to mipbook');
console.log(e.value);
}
试过这个:
let delegate = ...
let dispatchQueue = dispatch_queue_create("test_queue", null);
let udp = GCDAsyncSocket.alloc().initWithDelegateDelegateQueue(delegate, dispatchQueue);
而且效果很好。也应该适用于 GCDAsyncSocket
。
由于某种原因 dispatch_get_main_queue() 未定义。