RCConnectionManager.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // RCConnectionManager.m
  3. // MusicGradeExam
  4. //
  5. // Created by Kyle on 2020/7/10.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "RCConnectionManager.h"
  9. #import "RoomLoginHelper.h"
  10. @interface RCConnectionManager ()
  11. @end
  12. @implementation RCConnectionManager
  13. + (instancetype)shareManager {
  14. static RCConnectionManager *manager = nil;
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. manager = [[RCConnectionManager alloc] init];
  18. });
  19. return manager;
  20. }
  21. - (void)setIsNeedJoin:(BOOL)isNeedJoin {
  22. _isNeedJoin = isNeedJoin;
  23. if (self.isConnected && isNeedJoin) {
  24. dispatch_main_async_safe(^{
  25. [[RoomLoginHelper sharedInstance] joinRongRTCRoom];
  26. });
  27. }
  28. else if (isNeedJoin && !self.isConnected) {
  29. dispatch_main_async_safe(^{
  30. [MBProgressHUD ksShowMessage:@"IM未连接上"];
  31. });
  32. }
  33. }
  34. // 极端情况 IM稍后连上的
  35. - (void)setIsConnected:(BOOL)isConnected {
  36. _isConnected = isConnected;
  37. if (self.isNeedJoin && isConnected) {
  38. dispatch_main_async_safe(^{
  39. [[RoomLoginHelper sharedInstance] joinRongRTCRoom];
  40. });
  41. }
  42. }
  43. - (void)onRCIMConnectionStatusChanged:(RCConnectionStatus)status {
  44. if (status == ConnectionStatus_Connected) { // RTC会自动断线重连 此处多余
  45. self.isConnected = YES;
  46. NSLog(@"connect im success");
  47. }
  48. else if (status == ConnectionStatus_KICKED_OFFLINE_BY_OTHER_CLIENT) { // 账号被挤掉
  49. self.isNeedJoin = NO;
  50. self.isNeedShowMessage = NO;
  51. [[NSNotificationCenter defaultCenter] postNotificationName:@"otherLogin" object:nil];
  52. }
  53. else if (status == ConnectionStatus_Unconnected) {
  54. if (_isNeedShowMessage) {
  55. dispatch_main_async_safe(^{
  56. [MBProgressHUD ksShowMessage:@"连接断开,请检查您的网络"];
  57. });
  58. }
  59. }
  60. }
  61. @end