2016-05-13 97 views
1

我正在爲我的iOS聊天應用程序使用Socket.IO。名爲socket.io-client-swift的聊天庫位於Swift中,我可以使用橋接手動將其手動集成到我的Objective-C項目中。Objective-C客戶端的Socket.IO-Swift庫

我進口的「源」文件夾從上面庫到我的Xcode和我ChatViewController放在#進口「MyProjectName-Swift.h」與下面的代碼:

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

我使用本地的node.js(http://localhost:3000/ )由this tutorial的幫助下,從發送消息,我可以在我的Xcode控制檯同時看到:

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) 

所以,我怎麼能發出這條消息在我的應用程序,以顯示和/或發送消息給服務器。沒有任何明確的教程解釋這一點,特別是在Objective-C中。誰能幫忙?謝謝。

回答

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

利用可用的格式選項確保您的答案儘可能可讀/可區分。有一個代碼高亮選項可以確保您包含的代碼片段格式正確 – JordanMazurke