首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Cocoa异步库返回代理方法返回Phonegap插件中缺少的回调Id

使用Cocoa异步库返回代理方法返回Phonegap插件中缺少的回调Id
EN

Stack Overflow用户
提问于 2014-02-06 22:12:40
回答 1查看 2.5K关注 0票数 1

在利用Cocoa异步套接字库的Phonegap3.3上,我很难让插件正确运行。这个问题发生在下面代码中的didReadData方法中。当我将响应数据输出到控制台时,响应数据是正确的,但是,我无法使对javascript代码的回调正常工作。据我所知,在[self writeJavascript:pluginResult toErrorCallbackString: callbackId ]中使用的callbackId与sendMessage方法中计算的callbackId不同。任何帮助都是非常感谢的!

代码语言:javascript
复制
/********* 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-07 17:57:07

在.h上创建一个属性

代码语言:javascript
复制
@property (nonatomic, strong) NSString* myCallbackId;

在sendData上更改

代码语言:javascript
复制
NSString* myCallbackId = command.callbackId;

代码语言:javascript
复制
self.myCallbackId = command.callbackId;

在didReadData上更改

代码语言:javascript
复制
[self writeJavascript:[pluginResult toErrorCallbackString:callbackId]];

代码语言:javascript
复制
[self writeJavascript:[pluginResult toErrorCallbackString:self.callbackId]];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21615195

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档