gpt4 book ai didi

ios - 在委托(delegate)方法返回时使用 Cocoa 异步库的 Phonegap 插件中缺少回调 ID

转载 作者:行者123 更新时间:2023-11-29 12:54:03 25 4
gpt4 key购买 nike

我无法让我的插件在使用 Cocoa 异步套接字库的 Phonegap 3.3 上正常运行。问题出现在下面代码中的 didReadData 方法中。当我将响应数据输出到控制台时,响应数据是正确的,但是,我无法使 javascript 代码的回调正常工作。我能说的最好的是 [self writeJavascript:[pluginResult toErrorCallbackString:callbackId]] 中使用的 callbackId 与 sendMessage 方法中计算的 callbackId 不同。非常感谢任何帮助!

/********* TCPSockets.m Cordova Plugin Implementation *******/

#import "TCPSockets.h"
#import "GCDAsyncSocket.h"

@implementation TCPSockets

#define ENABLE_BACKGROUNDING 1

#pragma mark Plugin Start

- (void)sendMessage:(CDVInvokedUrlCommand*)command;
{
NSString* sendMessage = [command.arguments objectAtIndex:0];
NSString* myCallbackId = command.callbackId;

NSLog(@"Callback Id is %@", myCallbackId);

if (sendMessage == nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsString: @"Error: No parameters for plugin"];
[self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];
}
else
{
port = [[command.arguments objectAtIndex:0] retain];
host = [[command.arguments objectAtIndex:1] retain];
message = [[command.arguments objectAtIndex:2] retain];
connectionTimeout = [[command.arguments objectAtIndex:3] retain];
secureConnection = [[command.arguments objectAtIndex:4] retain];
response = @"";
error = nil;

dispatch_queue_t mainQueue = dispatch_get_main_queue();

asyncSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:mainQueue];

if ([secureConnection boolValue])
{
NSLog(@"Connecting (SSL) to \"%@\" on port %i...", host, [port intValue]);

if (![asyncSocket connectToHost:host onPort:[port intValue] withTimeout: ([connectionTimeout intValue] / 1000) error:&error])
{
NSLog(@"Error connecting: %@", error);

response = error.localizedDescription;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];

}
}
else
{
NSLog(@"Connecting to \"%@\" on port %i...", host, [port intValue]);

if (![asyncSocket connectToHost:host onPort:[port intValue] withTimeout:([connectionTimeout intValue] / 1000) error:&error])
{
NSLog(@"Error connecting: %@", error);

response = error.localizedDescription;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR];
[pluginResult setKeepCallback:[NSNumber numberWithBool:YES]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:myCallbackId];
}
}
}
}



- (id)init
{
if ((self = [super init]))
{
// Setup logging framework
//[DDLog addLogger:[DDTTYLogger sharedInstance]];
}

return self;
}

#pragma mark Socket Delegate

- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port
{
NSLog(@"socket:%p didConnectToHost:%@ port:%hu", sock, self->host, (UInt16)self->port);

if ([secureConnection boolValue])
{
// SSL connection

#if ENABLE_BACKGROUNDING && !TARGET_IPHONE_SIMULATOR
{
// Backgrounding doesn't seem to be supported on the simulator yet

[sock performBlock:^{
if ([sock enableBackgroundingOnSocket])
{
NSLog(@"Enabled backgrounding on socket");
}
else
{
NSLog(@"Enabling backgrounding failed!");
}
}];
}
#endif

// Configure SSL/TLS settings (see documentation for the startTLS method in GCDAsyncSocket.h)
NSMutableDictionary *settings = [NSMutableDictionary dictionaryWithCapacity:3];

// To connect to a test server, with a self-signed certificate, use settings similar to this:

// Allow expired certificates
[settings setObject:[NSNumber numberWithBool:YES]
forKey:(NSString *)kCFStreamSSLAllowsExpiredCertificates];

// Allow self-signed certificates
[settings setObject:[NSNumber numberWithBool:YES]
forKey:(NSString *)kCFStreamSSLAllowsAnyRoot];

// Don't validate the certificate chain
[settings setObject:[NSNumber numberWithBool:NO]
forKey:(NSString *)kCFStreamSSLValidatesCertificateChain];

NSLog(@"Starting SSL with settings:\n%@", settings);

[sock startTLS:settings];
}
else
{

#if ENABLE_BACKGROUNDING && !TARGET_IPHONE_SIMULATOR
{
// Backgrounding doesn't seem to be supported on the simulator yet

[sock performBlock:^{
if ([sock enableBackgroundingOnSocket])
{
NSLog(@"Enabled backgrounding on socket");
}
else
{
NSLog(@"Enabling backgrounding failed!");
}
}];
}
#endif

NSData *requestData = [[message dataUsingEncoding:NSUTF8StringEncoding] retain];

NSLog(@"Sending:%@", message);

[sock writeData:requestData withTimeout:([connectionTimeout intValue] / 1000) tag:0];
[sock readDataToData:[GCDAsyncSocket LFData] withTimeout:([connectionTimeout intValue] / 1000) tag:0];
}
}

- (void)socketDidSecure:(GCDAsyncSocket *)sock
{
NSLog(@"socketDidSecure:%p", sock);

NSData *requestData = [[message dataUsingEncoding:NSUTF8StringEncoding] retain];

NSLog(@"Sending:%@", message);

[sock writeData:requestData withTimeout:([connectionTimeout intValue] / 1000) tag:0];
[sock readDataToData:[GCDAsyncSocket LFData] withTimeout:([connectionTimeout intValue] / 1000) tag:0];
}

- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag
{
NSLog(@"socket:%p didWriteDataWithTag:%ld", sock, tag);
}

- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag
{

NSLog(@"socket:%p didReadData:WithTag:%ld", sock, tag);

response = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] retain];

response = [[response stringByReplacingOccurrencesOfString:@"\n" withString:@""] retain];

NSLog(@"Resposne data: %@", response);

pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:response] retain];
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];


// Release instance of GCDAsyncSocket
[asyncSocket setDelegate:nil delegateQueue:NULL];
[asyncSocket disconnect];
[asyncSocket release];
}

- (void)socketDidDisconnect:(GCDAsyncSocket *)sock withError:(NSError *)err
{
NSLog(@"socketDidDisconnect:%p withError: %@", sock, err);

if (err != nil) {
response = [err.localizedDescription retain];
pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:response] retain];

[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];
} else {
response = [[response stringByReplacingOccurrencesOfString:@"\n" withString:@""] retain];
pluginResult = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:response] retain];

[self writeJavascript:[pluginResult toSuccessCallbackString:callbackId]];
}
}

@end

最佳答案

在你的 .h 上创建一个属性

@property (nonatomic, strong) NSString* myCallbackId;

在 sendData 上改变

NSString* myCallbackId = command.callbackId;

self.myCallbackId = command.callbackId;

在 didReadData 上改变

[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];

[self writeJavascript:[pluginResult toErrorCallbackString:self.callbackId]];

关于ios - 在委托(delegate)方法返回时使用 Cocoa 异步库的 Phonegap 插件中缺少回调 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21615195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com