3 changed files with 182 additions and 1 deletions
Unified View
Diff Options
@ -0,0 +1,178 @@ |
|||||
|
import 'package:im_flutter_sdk/im_flutter_sdk.dart'; |
||||
|
|
||||
|
// 完整的IM管理器实现,使用实际的SDK类型和方法 |
||||
|
class IMManager { |
||||
|
// 单例模式 |
||||
|
static final IMManager _instance = IMManager._internal(); |
||||
|
factory IMManager() => _instance; |
||||
|
bool _isInitialized = false; |
||||
|
|
||||
|
IMManager._internal() { |
||||
|
print('IMManager instance created'); |
||||
|
} |
||||
|
|
||||
|
/// 初始化IM SDK |
||||
|
Future<bool> initialize(String appKey) async { |
||||
|
try { |
||||
|
if (_isInitialized) { |
||||
|
print('IM SDK already initialized'); |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
// 创建EMOptions实例 |
||||
|
final options = EMOptions( |
||||
|
appKey: appKey, |
||||
|
autoLogin: false, |
||||
|
acceptInvitationAlways: false, |
||||
|
); |
||||
|
|
||||
|
// 初始化SDK |
||||
|
await EMClient.getInstance.init(options); |
||||
|
|
||||
|
// 注册监听器 |
||||
|
_registerListeners(); |
||||
|
|
||||
|
_isInitialized = true; |
||||
|
print('IM SDK initialized successfully'); |
||||
|
return true; |
||||
|
} catch (e) { |
||||
|
print('Failed to initialize IM SDK: $e'); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 注册监听器 |
||||
|
void _registerListeners() { |
||||
|
try { |
||||
|
// 消息监听器 |
||||
|
|
||||
|
|
||||
|
// 连接监听器 |
||||
|
EMClient.getInstance.addConnectionEventHandler('', EMConnectionEventHandler( |
||||
|
onConnected: () { |
||||
|
print('Connected to IM server'); |
||||
|
}, |
||||
|
onDisconnected: () { |
||||
|
print('Disconnected from IM server:'); |
||||
|
}, |
||||
|
onTokenDidExpire: () { |
||||
|
print('IM token about to expire'); |
||||
|
}, |
||||
|
onUserKickedByOtherDevice: () { |
||||
|
print('User kicked out of IM server'); |
||||
|
}, |
||||
|
)); |
||||
|
} catch (e) { |
||||
|
print('Failed to register listeners: $e'); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 登录IM服务 |
||||
|
Future<bool> login(String userId, String token) async { |
||||
|
try { |
||||
|
if (!_isInitialized) { |
||||
|
print('IM SDK not initialized'); |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
await EMClient.getInstance.loginWithToken(userId, token); |
||||
|
print('IM login successful'); |
||||
|
return true; |
||||
|
} catch (e) { |
||||
|
print('IM login failed: $e'); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 登出IM服务 |
||||
|
Future<bool> logout() async { |
||||
|
try { |
||||
|
await EMClient.getInstance.logout(); |
||||
|
print('IM logout successful'); |
||||
|
return true; |
||||
|
} catch (e) { |
||||
|
print('IM logout failed: $e'); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 发送文本消息 |
||||
|
Future<EMMessage?> sendTextMessage( |
||||
|
String content, |
||||
|
String toChatUsername, |
||||
|
) async { |
||||
|
try { |
||||
|
// 创建文本消息 |
||||
|
final message = EMMessage.createTxtSendMessage( |
||||
|
targetId: toChatUsername, |
||||
|
content: content, |
||||
|
); |
||||
|
|
||||
|
// 发送消息 |
||||
|
await EMClient.getInstance.chatManager.sendMessage(message); |
||||
|
print('Text message sent successfully'); |
||||
|
return message; |
||||
|
} catch (e) { |
||||
|
print('Failed to send text message: $e'); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 发送图片消息 |
||||
|
Future<EMMessage?> sendImageMessage( |
||||
|
String imagePath, |
||||
|
String toChatUsername, |
||||
|
) async { |
||||
|
try { |
||||
|
// 创建图片消息 |
||||
|
final message = EMMessage.createImageSendMessage( |
||||
|
targetId: toChatUsername, |
||||
|
filePath: imagePath, |
||||
|
sendOriginalImage: false, |
||||
|
); |
||||
|
|
||||
|
// 发送消息 |
||||
|
await EMClient.getInstance.chatManager.sendMessage(message); |
||||
|
print('Image message sent successfully'); |
||||
|
return message; |
||||
|
} catch (e) { |
||||
|
print('Failed to send image message: $e'); |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 获取会话列表 |
||||
|
Future<List<EMConversation>> getConversations() async { |
||||
|
try { |
||||
|
return EMClient.getInstance.chatManager.loadAllConversations(); |
||||
|
} catch (e) { |
||||
|
print('Failed to get conversations: $e'); |
||||
|
return []; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// 获取指定会话的消息记录 |
||||
|
Future<List<EMMessage?>> getMessages( |
||||
|
String conversationId, { |
||||
|
int pageSize = 20, |
||||
|
String? startMsgId, |
||||
|
}) async { |
||||
|
EMConversationType convType = EMConversationType.Chat; |
||||
|
EMCursorResult<EMMessage?> cursor = await EMClient.getInstance.chatManager.fetchHistoryMessagesByOption( |
||||
|
conversationId, convType, pageSize: pageSize |
||||
|
); |
||||
|
return cursor.data; |
||||
|
} |
||||
|
|
||||
|
/// 清理资源 |
||||
|
void dispose() { |
||||
|
try { |
||||
|
EMClient.getInstance.removeConnectionEventHandler(""); |
||||
|
} catch (e) { |
||||
|
print('Failed to dispose resources: $e'); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 导出单例实例 |
||||
|
final IMManager imManager = IMManager(); |
||||
Write
Preview
Loading…
Cancel
Save