现在我正在使用google webRTC(会议)iOS应用并使用socket.io作为信令协议。
对于用户之间信令的个别管理,我需要为每个客户端创建套接字。
在第一个客户端,我创建了两个频道,一个是公共的(用于公共广播的消息),一个是我自己的私有(使用我的id作为命名空间)。请参阅 - “initChannel”和“createMyChannel”。
当新用户加入房间时,我在公共频道中获得'newParticipant'事件,使用新的用户ID我创建一个私人频道,将私人消息发送到该特定用户的频道,该频道已由该用户创建客户在他/她加入房间时。请参阅 - “WRTCSignalingType”切换案例。
一切正常,除非我收到'newParticipant'后发送消息给用户的私人频道,在接收方一侧多次调用处理器
[self.myChannel on:@"message" callback:^(NSArray * data, SocketAckEmitter * ack) {
NSLog(@"Data >>> %@", data);
}];
我不知道为什么会发生这种情况......是多次发送的消息或其他内容,或者是这个库的错误。
请帮助......这只会让我发疯。
@interface WRTCMultiPeerConnection ()
@property (strong, nonatomic) SocketIOClient *channel;
@property (strong, nonatomic) SocketIOClient *myChannel;
@property (copy, nonatomic) NSString *room;
@property (copy, nonatomic) NSString *participant;
@property (copy, nonatomic) NSURL *socketUri;
@property (nonatomic, strong) NSArray *iceServers;
@property (nonatomic, strong) NSDictionary *connectionOptions;
@property (nonatomic, strong) NSMutableDictionary *peerToConnectionMap;
@property (nonatomic, strong) NSMutableDictionary *offerChannels;
@end
@implementation WRTCMultiPeerConnection
- (instancetype)initConferenceCallWithRoom:(NSString *)room nickname:(NSString *)nickname delegate:(id <WRTCMultiPeerConnectionDelegate>)delegate options:(WRTCMediaConfiguration *)options {
self = [super init];
if (self) {
self.delegate = delegate;
self.room = room;
self.socketUri = [[NSURL alloc] initWithString:kSocketUrl];
self.participant = nickname;
self.connectionOptions = @{@"log" : @YES};
self.peerToConnectionMap = [NSMutableDictionary new];
self.offerChannels = [NSMutableDictionary new];
[self initChannel];
[self createMyChannel];
}
return self;
}
#pragma mark - Socket.io
- (void)createMyChannel {
self.myChannel = [[SocketIOClient alloc] initWithSocketURL:self.socketUri options:@{@"log" : @NO, @"nsp" : [NSString stringWithFormat:@"/%@", self.participant]}];
[self.myChannel on:@"connect" callback:^(NSArray * data, SocketAckEmitter * ack) {
NSLog(@"Private channel connected.");
}];
[self.myChannel on:@"message" callback:^(NSArray * data, SocketAckEmitter * ack) {
NSLog(@"Data >>> %@", data);
}];
[self.myChannel connect];
}
- (void)initChannel {
self.channel = [[SocketIOClient alloc] initWithSocketURL:self.socketUri options:self.connectionOptions];
[self.channel on:@"connect" callback:^(NSArray * data, SocketAckEmitter * ack) {
NSDictionary *packet = @{kRoom : self.room,kFrom : self.participant};
[self.channel emit:@"create_join" withItems:@[packet]];
}];
[self.channel on:@"room.created" callback:^(NSArray * data, SocketAckEmitter * ack) {
}];
[self.channel on:@"room.join" callback:^(NSArray * data, SocketAckEmitter * ack) {
//NSLog(@"User send request to join room");
}];
[self.channel on:@"room.joined" callback:^(NSArray * data, SocketAckEmitter * ack) {
//NSLog(@"Room Joined");
if (data.count) {
NSDictionary *message = [data firstObject];
if (message && [message[kFrom] isEqualToString:self.participant]) {
NSDictionary *packet = @{kType : kEventTypeNewParticipant, kFrom : self.participant};
[self.channel emit:@"message" withItems:@[packet]];
}
}
}];
[self.channel on:@"message" callback:^(NSArray * data, SocketAckEmitter * ack) {
NSLog(@"\n\nMessage recieved in Public channel >> %@ Data >> %@", self.participant, data);
if (data.count) {
NSDictionary *message = [data firstObject];
if (message) {
WRTCSignalingType signalType = [self signalTypeStringToEnum:message[kType]];
switch (signalType) {
case WRTCSignalingTypeNewParticipant:
{
NSString *otherUserId = message[kFrom];
if (![self.offerChannels hasValueForKey:otherUserId]) {
//NSLog(@"\n\nRecieved New Participant from >>> %@", otherUserId);
self.offerChannels[otherUserId] = [[SocketIOClient alloc] initWithSocketURL:self.socketUri options:@{@"log" : @YES, @"nsp" : [NSString stringWithFormat:@"/%@", otherUserId]}];
[self.offerChannels[otherUserId] on:@"connect" callback:^(NSArray * data, SocketAckEmitter * ack) {
NSLog(@"\n\nPrivate channel connected for user %@", otherUserId);
//NSLog(@"\n\nTotal offer channels %@", self.offerChannels);
NSDictionary *packet = @{kType : @"offer", kFrom : self.participant, @"sdp" : @"Test Connect"};
[self.offerChannels[otherUserId] emit:@"message" withItems:@[packet]];
}];
[self.offerChannels[otherUserId] connect];
}
}
break;
default:
break;
}
}
}
}];
[self.channel connect];
}