2018-09-12 17:03:07 +03:00
|
|
|
//
|
|
|
|
// NCExternalSignalingController.m
|
|
|
|
// VideoCalls
|
|
|
|
//
|
|
|
|
// Created by Ivan Sein on 07.09.18.
|
|
|
|
// Copyright © 2018 struktur AG. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "NCExternalSignalingController.h"
|
|
|
|
|
|
|
|
#import "SRWebSocket.h"
|
|
|
|
#import "NCAPIController.h"
|
2019-06-16 21:21:50 +03:00
|
|
|
#import "NCDatabaseManager.h"
|
2019-01-31 16:18:11 +03:00
|
|
|
#import "NCRoomsManager.h"
|
2018-09-12 17:03:07 +03:00
|
|
|
#import "NCSettingsController.h"
|
|
|
|
|
2018-09-12 19:27:20 +03:00
|
|
|
static NSTimeInterval kInitialReconnectInterval = 1;
|
|
|
|
static NSTimeInterval kMaxReconnectInterval = 16;
|
|
|
|
|
2018-09-12 17:03:07 +03:00
|
|
|
@interface NCExternalSignalingController () <SRWebSocketDelegate>
|
|
|
|
|
|
|
|
@property (nonatomic, strong) SRWebSocket *webSocket;
|
|
|
|
@property (nonatomic, strong) dispatch_queue_t processingQueue;
|
|
|
|
@property (nonatomic, strong) NSString* serverUrl;
|
|
|
|
@property (nonatomic, strong) NSString* ticket;
|
|
|
|
@property (nonatomic, strong) NSString* resumeId;
|
2018-09-20 17:31:01 +03:00
|
|
|
@property (nonatomic, strong) NSString* sessionId;
|
2019-11-14 12:24:21 +03:00
|
|
|
@property (nonatomic, strong) NSString* userId;
|
|
|
|
@property (nonatomic, strong) NSString* authenticationBackendUrl;
|
2018-10-05 18:46:14 +03:00
|
|
|
@property (nonatomic, assign) BOOL connected;
|
2018-09-12 17:03:07 +03:00
|
|
|
@property (nonatomic, assign) BOOL mcuSupport;
|
2018-09-24 16:58:50 +03:00
|
|
|
@property (nonatomic, strong) NSMutableDictionary* participantsMap;
|
2018-10-05 18:46:14 +03:00
|
|
|
@property (nonatomic, strong) NSMutableArray* pendingMessages;
|
2018-09-12 19:27:20 +03:00
|
|
|
@property (nonatomic, assign) NSInteger reconnectInterval;
|
|
|
|
@property (nonatomic, strong) NSTimer *reconnectTimer;
|
2018-09-12 17:03:07 +03:00
|
|
|
@property (nonatomic, assign) BOOL reconnecting;
|
2019-06-07 19:26:02 +03:00
|
|
|
@property (nonatomic, assign) BOOL sessionChanged;
|
2018-09-12 17:03:07 +03:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NCExternalSignalingController
|
|
|
|
|
|
|
|
+ (NCExternalSignalingController *)sharedInstance
|
|
|
|
{
|
|
|
|
static dispatch_once_t once;
|
|
|
|
static NCExternalSignalingController *sharedInstance;
|
|
|
|
dispatch_once(&once, ^{
|
|
|
|
sharedInstance = [[self alloc] init];
|
|
|
|
});
|
|
|
|
return sharedInstance;
|
|
|
|
}
|
|
|
|
|
2019-11-14 12:24:21 +03:00
|
|
|
- (instancetype)initWithAccount:(TalkAccount *)account server:(NSString *)serverUrl andTicket:(NSString *)ticket
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
if (self) {
|
|
|
|
_account = account;
|
|
|
|
_userId = _account.userId;
|
|
|
|
_authenticationBackendUrl = [[NCAPIController sharedInstance] authenticationBackendUrlForAccount:_account];
|
|
|
|
[self setServer:serverUrl andTicket:ticket];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:03:07 +03:00
|
|
|
- (BOOL)isEnabled
|
|
|
|
{
|
|
|
|
return (_serverUrl) ? YES : NO;
|
|
|
|
}
|
|
|
|
|
2018-09-21 12:37:49 +03:00
|
|
|
- (BOOL)hasMCU
|
|
|
|
{
|
|
|
|
return _mcuSupport;
|
|
|
|
}
|
|
|
|
|
2018-09-20 17:31:01 +03:00
|
|
|
- (NSString *)sessionId
|
|
|
|
{
|
|
|
|
return _sessionId;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:03:07 +03:00
|
|
|
- (void)setServer:(NSString *)serverUrl andTicket:(NSString *)ticket
|
|
|
|
{
|
|
|
|
_serverUrl = [self getWebSocketUrlForServer:serverUrl];
|
|
|
|
_ticket = ticket;
|
|
|
|
_processingQueue = dispatch_queue_create("com.nextcloud.Talk.websocket.processing", DISPATCH_QUEUE_SERIAL);
|
2018-09-12 19:27:20 +03:00
|
|
|
_reconnectInterval = kInitialReconnectInterval;
|
2018-10-05 18:46:14 +03:00
|
|
|
_pendingMessages = [NSMutableArray new];
|
2018-09-12 17:03:07 +03:00
|
|
|
|
|
|
|
[self connect];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)getWebSocketUrlForServer:(NSString *)serverUrl
|
|
|
|
{
|
|
|
|
NSString *wsUrl = [serverUrl copy];
|
|
|
|
|
|
|
|
// Change to websocket protocol
|
2019-06-19 13:38:19 +03:00
|
|
|
wsUrl = [wsUrl stringByReplacingOccurrencesOfString:@"https://" withString:@"wss://"];
|
|
|
|
wsUrl = [wsUrl stringByReplacingOccurrencesOfString:@"http://" withString:@"ws://"];
|
2018-09-12 17:03:07 +03:00
|
|
|
// Remove trailing slash
|
|
|
|
if([wsUrl hasSuffix:@"/"]) {
|
|
|
|
wsUrl = [wsUrl substringToIndex:[wsUrl length] - 1];
|
|
|
|
}
|
|
|
|
// Add spreed endpoint
|
|
|
|
wsUrl = [wsUrl stringByAppendingString:@"/spreed"];
|
|
|
|
|
|
|
|
return wsUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - WebSocket connection
|
|
|
|
|
|
|
|
- (void)connect
|
|
|
|
{
|
2018-09-12 19:27:20 +03:00
|
|
|
[self invalidateReconnectionTimer];
|
2018-10-05 18:46:14 +03:00
|
|
|
_connected = NO;
|
2018-09-12 17:03:07 +03:00
|
|
|
NSLog(@"Connecting to: %@", _serverUrl);
|
|
|
|
NSURL *url = [NSURL URLWithString:_serverUrl];
|
|
|
|
NSURLRequest *wsRequest = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
|
|
|
|
SRWebSocket *webSocket = [[SRWebSocket alloc] initWithURLRequest:wsRequest protocols:@[] allowsUntrustedSSLCertificates:YES];
|
|
|
|
[webSocket setDelegateDispatchQueue:self.processingQueue];
|
|
|
|
webSocket.delegate = self;
|
|
|
|
_webSocket = webSocket;
|
|
|
|
|
|
|
|
[_webSocket open];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reconnect
|
|
|
|
{
|
2018-09-12 19:27:20 +03:00
|
|
|
if (_reconnectTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[_webSocket close];
|
|
|
|
_webSocket = nil;
|
2018-09-12 17:03:07 +03:00
|
|
|
_reconnecting = YES;
|
2018-09-12 19:27:20 +03:00
|
|
|
|
|
|
|
[self setReconnectionTimer];
|
|
|
|
}
|
2019-06-12 16:02:53 +03:00
|
|
|
- (void)forceReconnect
|
|
|
|
{
|
|
|
|
_resumeId = nil;
|
|
|
|
[self reconnect];
|
|
|
|
}
|
2018-09-12 19:27:20 +03:00
|
|
|
|
2018-09-21 16:17:39 +03:00
|
|
|
- (void)disconnect
|
|
|
|
{
|
|
|
|
[self invalidateReconnectionTimer];
|
|
|
|
[_webSocket close];
|
|
|
|
_webSocket = nil;
|
|
|
|
}
|
|
|
|
|
2018-09-12 19:27:20 +03:00
|
|
|
- (void)setReconnectionTimer
|
|
|
|
{
|
|
|
|
[self invalidateReconnectionTimer];
|
|
|
|
// Wiggle interval a little bit to prevent all clients from connecting
|
|
|
|
// simultaneously in case the server connection is interrupted.
|
|
|
|
NSInteger interval = _reconnectInterval - (_reconnectInterval / 2) + arc4random_uniform((int)_reconnectInterval);
|
|
|
|
NSLog(@"Reconnecting in %ld", (long)interval);
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
_reconnectTimer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(connect) userInfo:nil repeats:NO];
|
|
|
|
});
|
|
|
|
_reconnectInterval = _reconnectInterval * 2;
|
|
|
|
if (_reconnectInterval > kMaxReconnectInterval) {
|
|
|
|
_reconnectInterval = kMaxReconnectInterval;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidateReconnectionTimer
|
|
|
|
{
|
|
|
|
[_reconnectTimer invalidate];
|
|
|
|
_reconnectTimer = nil;
|
2018-09-12 17:03:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - WebSocket messages
|
|
|
|
|
2018-10-05 18:46:14 +03:00
|
|
|
- (void)sendMessage:(NSDictionary *)jsonDict
|
|
|
|
{
|
|
|
|
if (!_connected && ![[jsonDict objectForKey:@"type"] isEqualToString:@"hello"]) {
|
|
|
|
[_pendingMessages addObject:jsonDict];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *jsonString = [self createWebSocketMessage:jsonDict];
|
|
|
|
if (!jsonString) {
|
|
|
|
NSLog(@"Error creating websobket message");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLog(@"Sending: %@", jsonString);
|
|
|
|
[_webSocket send:jsonString];
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:03:07 +03:00
|
|
|
- (void)sendHello
|
|
|
|
{
|
|
|
|
NSDictionary *helloDict = @{
|
|
|
|
@"type": @"hello",
|
|
|
|
@"hello": @{
|
|
|
|
@"version": @"1.0",
|
|
|
|
@"auth": @{
|
2019-11-14 12:24:21 +03:00
|
|
|
@"url": _authenticationBackendUrl,
|
2018-09-12 17:03:07 +03:00
|
|
|
@"params": @{
|
2019-11-14 12:24:21 +03:00
|
|
|
@"userid": _userId,
|
2018-09-12 17:03:07 +03:00
|
|
|
@"ticket": _ticket
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Try to resume session
|
|
|
|
if (_resumeId) {
|
|
|
|
helloDict = @{
|
|
|
|
@"type": @"hello",
|
|
|
|
@"hello": @{
|
|
|
|
@"version": @"1.0",
|
|
|
|
@"resumeid": _resumeId
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-05 18:46:14 +03:00
|
|
|
[self sendMessage:helloDict];
|
2018-09-12 17:03:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)helloResponseReceived:(NSDictionary *)helloDict
|
|
|
|
{
|
2018-10-05 18:46:14 +03:00
|
|
|
_connected = YES;
|
2018-09-12 17:03:07 +03:00
|
|
|
_resumeId = [helloDict objectForKey:@"resumeid"];
|
2019-06-07 19:26:02 +03:00
|
|
|
NSString *newSessionId = [helloDict objectForKey:@"sessionid"];
|
|
|
|
_sessionChanged = _sessionId && ![_sessionId isEqualToString:newSessionId];
|
|
|
|
_sessionId = newSessionId;
|
2018-09-21 12:37:49 +03:00
|
|
|
NSArray *serverFeatures = [[helloDict objectForKey:@"server"] objectForKey:@"features"];
|
|
|
|
for (NSString *feature in serverFeatures) {
|
|
|
|
if ([feature isEqualToString:@"mcu"]) {
|
|
|
|
_mcuSupport = YES;
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 18:46:14 +03:00
|
|
|
|
|
|
|
// Send pending messages
|
|
|
|
for (NSDictionary *message in _pendingMessages) {
|
|
|
|
[self sendMessage:message];
|
|
|
|
}
|
|
|
|
_pendingMessages = [NSMutableArray new];
|
2019-01-31 16:18:11 +03:00
|
|
|
|
|
|
|
// Re-join if user was in a room
|
2019-06-07 19:26:02 +03:00
|
|
|
if (_currentRoom && _sessionChanged) {
|
2019-11-14 12:24:21 +03:00
|
|
|
[self.delegate externalSignalingControllerWillRejoinCall:self];
|
2019-01-31 16:18:11 +03:00
|
|
|
[[NCRoomsManager sharedInstance] rejoinRoom:_currentRoom];
|
|
|
|
}
|
2018-09-12 17:03:07 +03:00
|
|
|
}
|
|
|
|
|
2018-09-12 19:27:20 +03:00
|
|
|
- (void)errorResponseReceived:(NSDictionary *)errorDict
|
|
|
|
{
|
|
|
|
NSString *errorCode = [errorDict objectForKey:@"code"];
|
|
|
|
if ([errorCode isEqualToString:@"no_such_session"]) {
|
|
|
|
_resumeId = nil;
|
|
|
|
[self reconnect];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:03:07 +03:00
|
|
|
- (void)joinRoom:(NSString *)roomId withSessionId:(NSString *)sessionId
|
|
|
|
{
|
|
|
|
NSDictionary *messageDict = @{
|
|
|
|
@"type": @"room",
|
|
|
|
@"room": @{
|
|
|
|
@"roomid": roomId,
|
|
|
|
@"sessionid": sessionId
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-05 18:46:14 +03:00
|
|
|
[self sendMessage:messageDict];
|
2018-09-12 17:03:07 +03:00
|
|
|
}
|
|
|
|
|
2018-09-13 17:12:01 +03:00
|
|
|
- (void)leaveRoom:(NSString *)roomId
|
2018-09-12 17:03:07 +03:00
|
|
|
{
|
2018-10-12 20:33:14 +03:00
|
|
|
if ([_currentRoom isEqualToString:roomId]) {
|
2019-01-31 16:18:11 +03:00
|
|
|
_currentRoom = nil;
|
2018-10-12 20:33:14 +03:00
|
|
|
[self joinRoom:@"" withSessionId:@""];
|
|
|
|
}
|
2018-09-12 17:03:07 +03:00
|
|
|
}
|
|
|
|
|
2018-09-20 17:31:01 +03:00
|
|
|
- (void)sendCallMessage:(NCSignalingMessage *)message
|
|
|
|
{
|
|
|
|
NSDictionary *messageDict = @{
|
|
|
|
@"type": @"message",
|
|
|
|
@"message": @{
|
|
|
|
@"recipient": @{
|
|
|
|
@"type": @"session",
|
|
|
|
@"sessionid": message.to
|
|
|
|
},
|
|
|
|
@"data": [message functionDict]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-05 18:46:14 +03:00
|
|
|
[self sendMessage:messageDict];
|
2018-09-20 17:31:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestOfferForSessionId:(NSString *)sessionId andRoomType:(NSString *)roomType
|
|
|
|
{
|
|
|
|
NSDictionary *messageDict = @{
|
|
|
|
@"type": @"message",
|
|
|
|
@"message": @{
|
|
|
|
@"recipient": @{
|
|
|
|
@"type": @"session",
|
|
|
|
@"sessionid": sessionId
|
|
|
|
},
|
|
|
|
@"data": @{
|
|
|
|
@"type": @"requestoffer",
|
|
|
|
@"roomType": roomType
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-05 18:46:14 +03:00
|
|
|
[self sendMessage:messageDict];
|
2018-09-20 17:31:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)roomMessageReceived:(NSDictionary *)messageDict
|
2018-09-12 17:03:07 +03:00
|
|
|
{
|
2018-09-24 16:58:50 +03:00
|
|
|
_participantsMap = [NSMutableDictionary new];
|
|
|
|
_currentRoom = [messageDict objectForKey:@"roomid"];
|
2019-06-07 19:26:02 +03:00
|
|
|
|
|
|
|
// Notify that session has change to rejoin the call if currently in a call
|
|
|
|
if (_sessionChanged) {
|
|
|
|
_sessionChanged = NO;
|
2019-11-14 12:24:21 +03:00
|
|
|
[self.delegate externalSignalingControllerShouldRejoinCall:self];
|
2019-06-07 19:26:02 +03:00
|
|
|
}
|
2018-09-17 17:30:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)eventMessageReceived:(NSDictionary *)eventDict
|
|
|
|
{
|
|
|
|
NSString *eventTarget = [eventDict objectForKey:@"target"];
|
|
|
|
if ([eventTarget isEqualToString:@"room"]) {
|
|
|
|
[self processRoomEvent:eventDict];
|
|
|
|
} else if ([eventTarget isEqualToString:@"roomlist"]) {
|
|
|
|
[self processRoomListEvent:eventDict];
|
|
|
|
} else if ([eventTarget isEqualToString:@"participants"]) {
|
|
|
|
[self processRoomParticipantsEvent:eventDict];
|
|
|
|
} else {
|
|
|
|
NSLog(@"Unsupported event target: %@", eventDict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)processRoomEvent:(NSDictionary *)eventDict
|
|
|
|
{
|
|
|
|
NSString *eventType = [eventDict objectForKey:@"type"];
|
|
|
|
if ([eventType isEqualToString:@"join"]) {
|
2018-09-20 17:31:01 +03:00
|
|
|
NSArray *joins = [eventDict objectForKey:@"join"];
|
|
|
|
for (NSDictionary *participant in joins) {
|
2018-09-24 16:58:50 +03:00
|
|
|
NSString *participantId = [participant objectForKey:@"userid"];
|
|
|
|
if (!participantId || [participantId isEqualToString:@""]) {
|
|
|
|
NSLog(@"Guest joined room.");
|
|
|
|
} else {
|
2019-11-14 12:24:21 +03:00
|
|
|
if ([participantId isEqualToString:_userId]) {
|
2018-09-24 16:58:50 +03:00
|
|
|
NSLog(@"App user joined room.");
|
|
|
|
} else {
|
2018-09-25 14:43:47 +03:00
|
|
|
[_participantsMap setObject:participant forKey:[participant objectForKey:@"sessionid"]];
|
2018-09-24 16:58:50 +03:00
|
|
|
NSLog(@"Participant joined room.");
|
|
|
|
}
|
2018-09-20 17:31:01 +03:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 17:30:56 +03:00
|
|
|
} else if ([eventType isEqualToString:@"leave"]) {
|
|
|
|
NSLog(@"Participant left room.");
|
|
|
|
} else if ([eventType isEqualToString:@"message"]) {
|
|
|
|
[self processRoomMessageEvent:[eventDict objectForKey:@"message"]];
|
|
|
|
} else {
|
|
|
|
NSLog(@"Unknown room event: %@", eventDict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)processRoomMessageEvent:(NSDictionary *)messageDict
|
|
|
|
{
|
|
|
|
NSString *messageType = [[messageDict objectForKey:@"data"] objectForKey:@"type"];
|
|
|
|
if ([messageType isEqualToString:@"chat"]) {
|
|
|
|
NSLog(@"Chat message received.");
|
|
|
|
} else {
|
|
|
|
NSLog(@"Unknown room message type: %@", messageDict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)processRoomListEvent:(NSDictionary *)eventDict
|
|
|
|
{
|
|
|
|
NSLog(@"Refresh room list.");
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)processRoomParticipantsEvent:(NSDictionary *)eventDict
|
|
|
|
{
|
|
|
|
NSString *eventType = [eventDict objectForKey:@"type"];
|
|
|
|
if ([eventType isEqualToString:@"update"]) {
|
2019-06-16 21:10:58 +03:00
|
|
|
NSLog(@"Participant list changed: %@", [eventDict objectForKey:@"update"]);
|
2019-11-14 12:24:21 +03:00
|
|
|
[self.delegate externalSignalingController:self didReceivedParticipantListMessage:[eventDict objectForKey:@"update"]];
|
2018-09-17 17:30:56 +03:00
|
|
|
} else {
|
|
|
|
NSLog(@"Unknown room event: %@", eventDict);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)messageReceived:(NSDictionary *)messageDict
|
|
|
|
{
|
|
|
|
NSLog(@"Message received");
|
2019-11-14 12:24:21 +03:00
|
|
|
[self.delegate externalSignalingController:self didReceivedSignalingMessage:messageDict];
|
2018-09-12 17:03:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - SRWebSocketDelegate
|
|
|
|
|
|
|
|
- (void)webSocketDidOpen:(SRWebSocket *)webSocket
|
|
|
|
{
|
|
|
|
if (webSocket == _webSocket) {
|
|
|
|
NSLog(@"WebSocket Connected!");
|
2018-09-12 19:27:20 +03:00
|
|
|
_reconnectInterval = kInitialReconnectInterval;
|
2018-09-12 17:03:07 +03:00
|
|
|
[self sendHello];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)messageData
|
|
|
|
{
|
|
|
|
if (webSocket == _webSocket) {
|
|
|
|
NSLog(@"WebSocket didReceiveMessage: %@", messageData);
|
|
|
|
NSData *data = [messageData dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
NSDictionary *messageDict = [self getWebSocketMessageFromJSONData:data];
|
|
|
|
NSString *messageType = [messageDict objectForKey:@"type"];
|
|
|
|
if ([messageType isEqualToString:@"hello"]) {
|
|
|
|
[self helloResponseReceived:[messageDict objectForKey:@"hello"]];
|
2018-09-12 19:27:20 +03:00
|
|
|
} else if ([messageType isEqualToString:@"error"]) {
|
|
|
|
[self errorResponseReceived:[messageDict objectForKey:@"error"]];
|
2018-09-17 17:30:56 +03:00
|
|
|
} else if ([messageType isEqualToString:@"room"]) {
|
|
|
|
[self roomMessageReceived:[messageDict objectForKey:@"room"]];
|
|
|
|
} else if ([messageType isEqualToString:@"event"]) {
|
|
|
|
[self eventMessageReceived:[messageDict objectForKey:@"event"]];
|
|
|
|
} else if ([messageType isEqualToString:@"message"]) {
|
|
|
|
[self messageReceived:[messageDict objectForKey:@"message"]];
|
2020-03-17 20:17:43 +03:00
|
|
|
} else if ([messageType isEqualToString:@"control"]) {
|
|
|
|
[self messageReceived:[messageDict objectForKey:@"control"]];
|
2018-09-12 17:03:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error
|
|
|
|
{
|
|
|
|
if (webSocket == _webSocket) {
|
|
|
|
NSLog(@"WebSocket didFailWithError: %@", error);
|
|
|
|
[self reconnect];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean
|
|
|
|
{
|
|
|
|
if (webSocket == _webSocket) {
|
|
|
|
NSLog(@"WebSocket didCloseWithCode:%ld reason:%@", (long)code, reason);
|
|
|
|
[self reconnect];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Utils
|
|
|
|
|
2018-09-25 14:43:47 +03:00
|
|
|
- (NSString *)getUserIdFromSessionId:(NSString *)sessionId
|
|
|
|
{
|
|
|
|
NSString *userId = nil;
|
|
|
|
NSDictionary *user = [_participantsMap objectForKey:sessionId];
|
|
|
|
if (user) {
|
|
|
|
userId = [user objectForKey:@"userid"];
|
|
|
|
}
|
|
|
|
return userId;
|
|
|
|
}
|
|
|
|
|
2018-09-12 17:03:07 +03:00
|
|
|
- (NSDictionary *)getWebSocketMessageFromJSONData:(NSData *)jsonData
|
|
|
|
{
|
|
|
|
NSError *error;
|
|
|
|
NSDictionary* messageDict = [NSJSONSerialization JSONObjectWithData:jsonData
|
|
|
|
options:kNilOptions
|
|
|
|
error:&error];
|
|
|
|
if (!messageDict) {
|
|
|
|
NSLog(@"Error parsing websocket message: %@", error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return messageDict;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)createWebSocketMessage:(NSDictionary *)message
|
|
|
|
{
|
|
|
|
NSError *error;
|
|
|
|
NSString *jsonString = nil;
|
|
|
|
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:message
|
|
|
|
options:0
|
|
|
|
error:&error];
|
|
|
|
|
|
|
|
if (!jsonData) {
|
|
|
|
NSLog(@"Error creating websocket message: %@", error);
|
|
|
|
} else {
|
|
|
|
jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonString;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|