Files

86 lines
2.8 KiB
Mathematica
Raw Permalink Normal View History

2025-08-08 11:05:33 +08:00
//
// BJAgoraRtmManager.m
// romantic
//
// Created by bj_szd on 2023/3/18.
// Copyright © 2023 romantic. All rights reserved.
//
#import "BJAgoraRtmManager.h"
#import <AgoraRtmKit/AgoraRtmKit.h>
@interface BJAgoraRtmManager () <AgoraRtmChannelDelegate, AgoraRtmDelegate>
@property (nonatomic, strong) AgoraRtmKit *kit;
@property (nonatomic, strong) AgoraRtmChannel *roomChannel;
@property (nonatomic, strong) AgoraRtmChannel *wholeChannel;
@end
@implementation BJAgoraRtmManager
+(instancetype)shared{
static BJAgoraRtmManager *__instance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__instance = [[BJAgoraRtmManager alloc] init];
});
return __instance;
}
- (void)loginAndJoinChannelWithAppId:(NSString *)appId token:(NSString *)token roomId:(NSString *)roomId {
if (_kit == nil) {
// AgoraRtmKit
_kit = [[AgoraRtmKit alloc] initWithAppId:appId delegate:self];
}
// RTM
[_kit loginByToken:token user:BJUserManager.userInfo.uid completion:^(AgoraRtmLoginErrorCode errorCode) {
if (errorCode == AgoraRtmLoginErrorOk || errorCode == AgoraRtmLoginErrorAlreadyLogin){//
[self onJoinChannel:roomId];
}
}];
}
- (void)onJoinChannel:(NSString *)roomId {
_roomChannel = [_kit createChannelWithId:roomId delegate:self];
[_roomChannel joinWithCompletion:^(AgoraRtmJoinChannelErrorCode errorCode) {
}];
_wholeChannel = [_kit createChannelWithId:@"123456789" delegate:self];
[_wholeChannel joinWithCompletion:^(AgoraRtmJoinChannelErrorCode errorCode) {
}];
}
- (void)logoutAndLeaveChannel {
WEAK_SELF
[_roomChannel leaveWithCompletion:^(AgoraRtmLeaveChannelErrorCode errorCode) {
[weakSelf.wholeChannel leaveWithCompletion:^(AgoraRtmLeaveChannelErrorCode errorCode) {
// RTM
[weakSelf.kit logoutWithCompletion:^(AgoraRtmLogoutErrorCode errorCode) {
}];
}];
}];
}
//
- (void)channel:(AgoraRtmChannel *)channel messageReceived:(AgoraRtmMessage *)message fromMember:(AgoraRtmMember *)member {
NSDictionary *dict = [message.text mj_JSONObject];
if (self.delegate && [self.delegate respondsToSelector:@selector(BJAgoraRtmDidReceiveMessage:)]) {
[self.delegate BJAgoraRtmDidReceiveMessage:[dict safeDictionaryForKey:@"content"]];
}
}
//
- (void)rtmKit:(AgoraRtmKit *_Nonnull)kit messageReceived:(AgoraRtmMessage *_Nonnull)message fromPeer:(NSString *_Nonnull)peerId {
NSDictionary *dict = [message.text mj_JSONObject];
if (self.delegate && [self.delegate respondsToSelector:@selector(BJAgoraRtmDidReceiveMessage:)]) {
[self.delegate BJAgoraRtmDidReceiveMessage:[dict safeDictionaryForKey:@"content"]];
}
}
@end