我在客观c客户端使用Socket IO swift client,我无法加入特定房间,因为我需要通过房间来加入房间ID:
/**
* join room
*
* @on -> 'join'
*
* @access -> user
*
* @param ->
* |room {messagingId}
*
* @emit -> {status:, message}
*/
我找到的只是:
[socket on:@"join" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(@"join=>%@", data);
}];
我的问题是如何将messaginID作为参数传递?
感谢。
答案 0 :(得分:1)
首先,为了清楚起见,进行一些描述
/ ** 为事件添加侦听器。 返回:处理程序的唯一ID * /
public func on(event: String, callback: NormalCallback) -> NSUUID
/ ** 向服务器发送消息 * /
public func emit(event: String, _ items: AnyObject...)
回答您的问题
on(event:String,callback:NormalCallback) - >您正在使用的NSUUID方法只会从Server获取响应,以便在代码下面发送数据
所以你需要使用emit来加入空间
[socket emit: @"join", messaginID];
答案 1 :(得分:1)
<强> /////// ********* ******** SocketIO /////// 强>
NSString *User = FORMAT(@"user_%@",[USERDEFAULT objectForKey:useridkey]);
NSDictionary *param = @{@"id": User};
NSData *data = [NSJSONSerialization dataWithJSONObject:param options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc]initWithData: data
encoding: NSUTF8StringEncoding];
NSURL* url = [[NSURL alloc] initWithString:CHAT_URL];
socket = [[SocketIOClient alloc] initWithSocketURL:url config:@{@"log": @NO, @"reconnectAttempts" : @50, @"reconnects" : @YES, @"forcePolling": @YES}];
//******* connect socket *******////
[socket on:@"connect" callback:^(NSArray* data, SocketAckEmitter* ack) {
NSLog(@"socket connected");
[socket emit:@"join" with:@[jsonString]];
}];
////******* For receive events *******////
[socket on:@"message" callback:^(NSArray * _Nonnull arrayData, SocketAckEmitter * _Nonnull ack) {
NSLog(@"%@", [arrayData description]);
NSString *other_userid = FORMAT(@"%@",[[arrayData valueForKey:@"r_id"] objectAtIndex:0]);
NSString *senderid = FORMAT(@"%@",[[arrayData valueForKey:@"s_id"] objectAtIndex:0]);
NSString *classified_id = FORMAT(@"%@",[[arrayData valueForKey:@"classified_id"]objectAtIndex:0]);
NSString *time = FORMAT(@"%@",[[arrayData valueForKey:@"time"]objectAtIndex:0]);
NSDictionary *temp;
if([classified_id isEqualToString:_classifiedid] && [other_userid isEqualToString:FORMAT(@"user_%@",selfuser.idstring)]) {
NSString *message = FORMAT(@"%@",[[arrayData valueForKey:@"msg"]objectAtIndex:0]);
temp = @{
@"msg":message,
@"time":time,
@"s_id":senderid,
@"r_id":other_userid
};
[self addChatObjectToChatArray:temp];
}
}];
[socket on:@"error" callback:^(NSArray * _Nonnull arrayData, SocketAckEmitter * _Nonnull ack) {
NSLog(@"error with -- %@ && ACK -- %@", [arrayData description], [ack debugDescription]);
}];
[socket setReconnects:YES];
[socket connect];