Socket.io-client-swift: Objective-C : Cant connect to server using Socket.IO

Created on 31 May 2018  路  4Comments  路  Source: socketio/socket.io-client-swift

Hi,

I'm using Socket.IO-Client-Swift' -13.2.0' to implement my project in Objective-C. I'm trying to connect to a Node.js service using Socket.IO and have followed the documentation for Objective-C. But when i try to run my code, the console logs 'tried connecting socket when engine isn't open' and cant get through.

Following is my code,

@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];

And the console,

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

How to solve this issue?
Thanks.

Most helpful comment

@property (nonatomic, strong) SocketManager * manager;
@property (nonatomic, strong) SocketIOClient * socket;
Replace this with the following two
SocketManager *manager
SocketIOClient *socket

All 4 comments

@property (nonatomic, strong) SocketManager * manager;
@property (nonatomic, strong) SocketIOClient * socket;
Replace this with the following two
SocketManager *manager
SocketIOClient *socket

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

Is this for macOS or iOS? But yes, the manager must be a strong property or a global. Also, the readme examples just assume you're running that code in main.swift or in a main function. It also leaves off starting the run loop.

Thanks a lot @liuxinSaberFans

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zevarito picture zevarito  路  3Comments

janiduw picture janiduw  路  5Comments

njawahire picture njawahire  路  4Comments

alionthego picture alionthego  路  4Comments

Seonift picture Seonift  路  4Comments