86 lines
2.8 KiB
Mathematica
86 lines
2.8 KiB
Mathematica
|
|
//
|
||
|
|
// 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
|