在委托方法返回时使用Cocoa异步库在Phonegap插件中缺少回调ID

时间:2014-02-06 22:12:40

标签: ios cocoa cordova callback

我无法让我的插件在使用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

1 个答案:

答案 0 :(得分:1)

你的.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]];