【问题标题】:Socket.io disconnect errorSocket.io 断开连接错误
【发布时间】:2014-06-24 14:23:57
【问题描述】:

我正在使用基于iOS7的https://github.com/pkyeck/socket.IO-objc的socket.io-obj库,但是我发现由于网络环境不好导致socket断开服务器时,我的应用程序会挂起很长时间。 这是我的代码:

#define NOTE_CENTER [NSNotificationCenter defaultCenter]
-(void)connectServer
{
    [socketIO connectToHost:SOCKETADDRESS
                     onPort:SOCKETPORT
                 withParams:nil
     ];
    status = Connecting;
    NSLog(@"%@",socketIO.debugDescription);
    [[] postNotificationName:kConnectingServer object:nil];
}

-(void)disConnectServer
{
    [socketIO disconnect];
}

-(BOOL)hasConnected {
    if (status==Connected) {
        return true;
    }
    return false;
}
-(NetworkStatus)networkStatus
{
    return status;
}

-(void) sendEvent:(OrderEvent)eventIndex withData:(NSDictionary *)data
{
    if (status==Connected) {
        [socketIO sendEvent:@"close" withData:data];
        //[socketIO sendEvent:[NSString stringWithFormat:@"%lu",eventIndex] withData:data];
    }
}

-(void) sendEventWithName:(NSString*)eventName withData:(NSDictionary *)data
{
    if (status==Connected) {
        [socketIO sendEvent:eventName withData:data];
    }
}
#pragma socketIO Delegate methods
- (void) socketIODidConnect:(SocketIO *)socket
{
    status = Connected;
    [NOTE_CENTER postNotificationName:kConnectedServer object:nil];
}

- (void) socketIODidDisconnect:(SocketIO *)socket disconnectedWithError:(NSError *)error
{
    status = OffLine;
    NSLog(@"socket.io disconnected. did error occur? %@", error);
    [NOTE_CENTER postNotificationName:kDisConnectedServer object:nil];
}

- (void) socketIO:(SocketIO *)socket onError:(NSError *)error
{
    [socket  disconnectForced];
    [NOTE_CENTER postNotificationName:kConnecteServerError object:nil];
    if ([error code] == SocketIOUnauthorized) {
        NSLog(@"not authorized");
    } else {
        NSLog(@"onError() %@", error);
    }
}

- (void) socketIO:(SocketIO *)socket didReceiveJSON:(SocketIOPacket *)packet
{
}

- (void) socketIO:(SocketIO *)socket didReceiveEvent:(SocketIOPacket *)packet
{
    NSLog(@"didReceiveEvent()");
    // test acknowledge
    NSString *packName = [packet name];
    //if (packet.name) {
    [NOTE_CENTER postNotificationName:packName object:nil userInfo:[[NSDictionary alloc] initWithObjectsAndKeys:[packet data],@"data", nil]];
    //}
    /*
    SocketIOCallback cb = ^(id argsData) {
        NSDictionary *response = argsData;
        // do something with response
        NSString *packetName = [response  stringValueForKey:@"name"];
        [NOTE_CENTER postNotificationName:packetName object:nil userInfo:response];
    };*/
}

我遇到的另一个问题是当我调用 "[socketIO disconnect]" 时,服务器不会立即断开我的应用程序。请帮我找到这些问题的答案。

非常感谢。

【问题讨论】:

    标签: ios socket.io disconnect


    【解决方案1】:

    对于迟到的回复,我深表歉意。我发现如果您的 socket.io 服务器在尝试 disconnectForced 时关闭,UI 将阻塞,因为如果您查看该方法,请求是使用 sendSynchronousRequest.

    您可以尝试修改方法以使用 sendAsynchronousRequest,但我建议进一步检查该类。您还可以在使用 disconnectForced 之前检查连接状态。

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 2016-09-20
      • 1970-01-01
      • 2012-11-09
      • 2017-07-17
      • 2016-07-08
      • 1970-01-01
      • 2014-04-30
      • 2014-10-13
      相关资源
      最近更新 更多