RCConnectionManager.m 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. //
  2. // RCConnectionManager.m
  3. // StudentDaya
  4. //
  5. // Created by Kyle on 2019/9/10.
  6. // Copyright © 2019 DayaMusic. All rights reserved.
  7. //
  8. #import "RCConnectionManager.h"
  9. #import "LoginHelper.h"
  10. #import "ClassroomService.h"
  11. @interface RCConnectionManager ()
  12. @end
  13. @implementation RCConnectionManager
  14. + (instancetype)shareManager {
  15. static RCConnectionManager *manager = nil;
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. manager = [[RCConnectionManager alloc] init];
  19. });
  20. return manager;
  21. }
  22. - (void)setIsNeedJoin:(BOOL)isNeedJoin {
  23. _isNeedJoin = isNeedJoin;
  24. if (self.isConnected && isNeedJoin) {
  25. dispatch_main_async_safe(^{
  26. [[LoginHelper sharedInstance] joinRongRTCRoom];
  27. });
  28. }
  29. else if (isNeedJoin && !self.isConnected) {
  30. dispatch_main_async_safe(^{
  31. NSString *tipsMessage = [NSString isEmptyString:UserDefault(RongTokenKey)] ? @"无IM token,请重新登录获取" : @"IM未能连接上,请检查您的网络";
  32. [MBProgressHUD ksShowMessage:tipsMessage];
  33. // 失败回调
  34. [[ClassroomService sharedService] joinRoomFailerNotify];
  35. });
  36. }
  37. }
  38. // 极端情况 IM稍后连上的 还没有进入房间
  39. - (void)setIsConnected:(BOOL)isConnected {
  40. _isConnected = isConnected;
  41. if (self.isNeedJoin && isConnected) {
  42. dispatch_main_async_safe(^{
  43. [[LoginHelper sharedInstance] joinRongRTCRoom];
  44. });
  45. }
  46. }
  47. - (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
  48. if (status == ConnectionStatus_Connected) { // RTC会自动断线重连 此处多余
  49. self.isConnected = YES;
  50. NSLog(@"connect im success");
  51. [[NSNotificationCenter defaultCenter] postNotificationName:@"RongIMConnected" object:nil];
  52. }
  53. else if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) { // 账号被挤掉
  54. self.isNeedJoin = NO;
  55. self.isNeedShowMessage = NO;
  56. [[RCIM sharedRCIM] logout];
  57. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  58. }
  59. else if (status == ConnectionStatus_Unconnected) {
  60. if (_isNeedShowMessage) {
  61. dispatch_main_async_safe(^{
  62. [MBProgressHUD ksShowMessage:@"连接断开,请检查您的网络"];
  63. });
  64. }
  65. }
  66. }
  67. @end