Umit Kaya
Umit Kaya

Reputation: 5951

Socket.IO-Swift library with Objective-C Client

I am using Socket.IO for my iOS chat application. Chat library called socket.io-client-swift is in Swift and i could manage to manually integrate it to my Objective-C project using bridging.

I imported 'Source' folder from above library into my Xcode and placed #import "MyProjectName-Swift.h" in my ChatViewController with code below:

//in viewDidLoad
NSURL* url = [[NSURL alloc] initWithString:@"http://localhost:3000/"];
SocketIOClient* socket = [[SocketIOClient alloc] initWithSocketURL:url options:@{@"log": @YES, @"forcePolling": @YES}];

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

[socket on:@"currentAmount" callback:^(NSArray* data, SocketAckEmitter* ack) {
double cur = [[data objectAtIndex:0] floatValue];

[socket emitWithAck:@"canUpdate" withItems:@[@(cur)]](0, ^(NSArray* data) {
    [socket emit:@"update" withItems:@[@{@"amount": @(cur + 2.50)}]];
});

[ack with:@[@"Got your currentAmount, ", @"dude"]];
}];

[socket connect];

I am using node.js locally(http://localhost:3000/) by the help of this tutorial to send message from, and i can see it simultaneously in my Xcode console:

2016-05-13 14:59:20.345 CoreData_Chat[45303:372543] LOG SocketEnginePolling: Doing polling request
2016-05-13 14:59:24.033 CoreData_Chat[45303:373287] LOG SocketEnginePolling: Got polling response
2016-05-13 14:59:24.033 CoreData_Chat[45303:373287] LOG SocketEnginePolling: Doing polling request
2016-05-13 14:59:24.033 CoreData_Chat[45303:373285] LOG SocketEngine: Got message: 42["chat message","hii"]
2016-05-13 14:59:24.034 CoreData_Chat[45303:373285] LOG SocketIOClient: Should parse message: 2["chat message","hii"]
2016-05-13 14:59:24.034 CoreData_Chat[45303:373285] LOG SocketParser: Parsing 2["chat message","hii"]
2016-05-13 14:59:24.035 CoreData_Chat[45303:373285] LOG SocketParser: Decoded packet as: SocketPacket {type: 2; data: [chat message, hii]; id: -1; placeholders: -1; nsp: /}
2016-05-13 14:59:24.035 CoreData_Chat[45303:373285] LOG SocketIOClient: Handling event: chat message with data: (
hii)

So, how can i emit this message to show in my app and/or send message to server. There is no any clear tutorial explaining this, especially in Objective-C. Can anyone help? Thank you.

Upvotes: 1

Views: 1345

Answers (1)

Neeraj Sonaro
Neeraj Sonaro

Reputation: 346

NSMutableDictionary *messageServer = [[NSMutableDictionary alloc]init];
[messageServer setObject:@"bet" forKey:userActivity];
[messageServer setObject:@"0" forKey:betMoney];
SocketIOClient emit:EVENTNAME withItems:@[messageServer];

Upvotes: 2

Related Questions