Objective-C:无法使用Socket.IO连接到服务器

时间:2018-05-31 16:05:58

标签: ios objective-c socket.io

我正在使用Socket.IO-Client-Swift' -13.2.0'在Objective-C中实现我的项目。我尝试使用Socket.IO连接到Node.js服务,并遵循Objective-C的文档。但是当我尝试运行我的代码时,控制台记录并尝试在引擎未打开时连接套接字'并且无法通过。

以下是我的代码,

@import SocketIO;
NSURL* url = [[NSURL alloc] initWithString:@"http://URL_STRING:PORT_NUMBER"];

SocketManager *manager = [[SocketManager alloc] initWithSocketURL:url config:@{@"log":@yES, @"compress":@yES}];
SocketIOClient *socket = [manager defaultSocket];

[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(@"socket connected");
}];

[socket connect];

和控制台,

2018-06-01 00:22:06.694853+1000 [885:732318] LOG SocketIOClient{/}: Adding handler for event: connect
2018-06-01 00:22:06.695467+1000 [885:732318] LOG SocketIOClient{/}: Handling event: statusChange with data: [connecting]
2018-06-01 00:22:06.695578+1000 [885:732318] LOG SocketIOClient{/}: Joining namespace /
2018-06-01 00:22:06.695639+1000 [885:732318] LOG SocketManager: Tried connecting socket when engine isn't open. Connecting
2018-06-01 00:22:06.695679+1000 [885:732318] LOG SocketManager: Adding engine
2018-06-01 00:22:06.697262+1000 [885:732355] LOG SocketEngine: Starting engine. Server: http://URL_STRING:PORT_NUMBER
2018-06-01 00:22:06.697323+1000 [885:732355] LOG SocketEngine: Handshaking
2018-06-01 00:22:06.699886+1000 [885:732355] LOG SocketEnginePolling: Doing polling GET http://URL_STRING:PORT_NUMBER/socket.io/?transport=polling&b64=1

如何解决这个问题? 感谢。

2 个答案:

答案 0 :(得分:1)

已解决:

将经理设置为强。

model.R1 = Param(model.D_S, initialize = R)

答案 1 :(得分:0)

SocketIOClient *socket = [manager socketForNamespace:@"/"];

添加此行,它将

代替

SocketIOClient *socket = [manager defaultSocket];