2025-10-20 10:16:44 +08:00
|
|
|
|
package com.xscm.moduleutil.bean;
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import com.blankj.utilcode.util.GsonUtils;
|
|
|
|
|
|
import com.tencent.imsdk.v2.V2TIMManager;
|
|
|
|
|
|
import com.tencent.imsdk.v2.V2TIMMessage;
|
|
|
|
|
|
import com.tencent.imsdk.v2.V2TIMSendCallback;
|
|
|
|
|
|
import com.xscm.moduleutil.bean.room.Children;
|
|
|
|
|
|
import com.xscm.moduleutil.bean.room.EmotionDeatils;
|
|
|
|
|
|
import com.xscm.moduleutil.event.RoomTaskEvent;
|
|
|
|
|
|
import com.xscm.moduleutil.utils.SpUtil;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
public class RoomMessageManager {
|
2025-10-24 17:52:11 +08:00
|
|
|
|
public String roomId;
|
|
|
|
|
|
private static RoomMessageManager instance ;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
public String getRoomId() {
|
|
|
|
|
|
return roomId;
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
public void setRoomId(String roomId) {
|
|
|
|
|
|
this.roomId = roomId;
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
|
|
|
|
|
|
public static RoomMessageManager getInstance() {
|
2025-10-24 17:52:11 +08:00
|
|
|
|
if (instance == null){
|
|
|
|
|
|
instance = new RoomMessageManager();
|
|
|
|
|
|
}
|
2025-10-20 10:16:44 +08:00
|
|
|
|
return instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-24 17:52:11 +08:00
|
|
|
|
public void sendChatEmoji(EmotionDeatils emotionDeatils){
|
|
|
|
|
|
EmotionDeatils event = new EmotionDeatils();
|
|
|
|
|
|
if (emotionDeatils != null&& emotionDeatils.getChildren()!=null && !emotionDeatils.getChildren().isEmpty() && emotionDeatils.getChildren().size()>0){
|
|
|
|
|
|
int position = new Random().nextInt(emotionDeatils.getChildren().size());
|
|
|
|
|
|
Children children = emotionDeatils.getChildren().get(position);
|
|
|
|
|
|
event.setImage(children.getImage());
|
|
|
|
|
|
event.setAnimate_image(emotionDeatils.getAnimate_image());
|
|
|
|
|
|
}else {
|
|
|
|
|
|
event= emotionDeatils;
|
2025-10-20 10:16:44 +08:00
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
RoomMessageEvent roomMessageEvent=new RoomMessageEvent();
|
|
|
|
|
|
roomMessageEvent.setRoomId(roomId);
|
|
|
|
|
|
roomMessageEvent.setMsgType(2);//这是表情类型,1:单发文本消息 2:单发表情消息
|
|
|
|
|
|
UserInfo userInfo= SpUtil.getUserInfo();
|
|
|
|
|
|
RoomMessageEvent.T t=new RoomMessageEvent.T();
|
|
|
|
|
|
t.setFromUserInfo(userInfo);
|
|
|
|
|
|
t.setEmoji(event);
|
|
|
|
|
|
roomMessageEvent.setText(t);
|
|
|
|
|
|
String json = GsonUtils.toJson(roomMessageEvent);
|
|
|
|
|
|
// 转换为 byte[]
|
|
|
|
|
|
byte[] binaryData = json.getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
// 创建自定义消息
|
|
|
|
|
|
V2TIMMessage v2TIMMessage = V2TIMManager.getMessageManager().createCustomMessage(binaryData);
|
|
|
|
|
|
// 发送消息
|
|
|
|
|
|
V2TIMManager.getMessageManager().sendMessage(v2TIMMessage, null, "room" + roomId, V2TIMMessage.V2TIM_PRIORITY_NORMAL, false, null, new V2TIMSendCallback<V2TIMMessage>() {
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onProgress(int progress) {
|
|
|
|
|
|
// 自定义消息不会回调进度
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onSuccess(V2TIMMessage message) {
|
|
|
|
|
|
// 发送群聊自定义消息成功
|
|
|
|
|
|
// EventBus.getDefault().post(new RoomTaskEvent());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
public void onError(int code, String desc) {
|
|
|
|
|
|
// 发送群聊自定义消息失败
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
}
|
2025-10-24 17:52:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-10-20 10:16:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|