This commit is contained in:
张良(004796)
2024-01-25 19:04:03 +08:00
parent 7874e38d17
commit 13fa059d02
14 changed files with 171 additions and 89 deletions

View File

@@ -0,0 +1,40 @@
package com.ruoyi.yunxin;
import com.alibaba.ttl.threadpool.TtlExecutors;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import java.util.concurrent.*;
/**
* 线程变量定义
* <p>created on 2023/3/3 11:16</p>
* @author ZL
*/
public class YunExecutor {
private final static int CPU_NUM = Runtime.getRuntime().availableProcessors();
public static Executor YUN_EXECUTOR;
static {
ThreadPoolExecutor roomExecutor = new ThreadPoolExecutor(CPU_NUM,
CPU_NUM << 2,
5,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(5),
init("yunxinThreadPoll-%d"),
new ThreadPoolExecutor.CallerRunsPolicy());
YUN_EXECUTOR = TtlExecutors.getTtlExecutor(roomExecutor);
}
private static ThreadFactory init(String nameFormat){
return new ThreadFactoryBuilder().setNameFormat(nameFormat).build();
}
private static ThreadPoolExecutor initExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, TimeUnit timeUnit,
BlockingQueue<Runnable> workQueue, String nameFormat){
return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, workQueue,
init(nameFormat));
}
}

View File

@@ -28,9 +28,7 @@ public class Yunxin {
private ImMessageClient messageClient;
public void sendToSync(Long toUid,Long fromUid,Object data){
YunxinExecutorConstant.COMMON_EXECUTOR.execute(() -> {
this.sendTo(toUid,fromUid,data);
});
this.sendTo(toUid,fromUid,data);
}
public YxDataR<SendMsgResp> sendTo(Long toUid,Long fromUid,Object data){
@@ -39,6 +37,7 @@ public class Yunxin {
req.setTo(toUid+"");
req.setBody(JSON.toJSONString(data));
req.setOption(JSON.toJSONString(new Option()));
req.setType(100);
return messageClient.sendMsg(req);
}

View File

@@ -15,7 +15,7 @@ import com.ruoyi.yunxin.resp.YxDataR;
public interface ImMessageClient {
/**
* 发送自定义消息
* 发送消息
* @param req
* @return
*/

View File

@@ -0,0 +1,19 @@
package com.ruoyi.yunxin.data;
import lombok.Data;
@Data
public class ImDataRes {
private Integer type;
private ImData data;
@Data
public static class ImData {
private Integer calltype;
// 通话状态1 取消 2 拒绝 3 超时 4 已通话
private Integer status;
private Long from_uid;
private Long to_uid;
private Long call_time;
}
}

View File

@@ -0,0 +1,25 @@
package com.ruoyi.yunxin.data;
public class ImMsgGen {
/**
* 通话通知
* @param status
* @param from
* @param to
* @param callTime
*/
public static ImDataRes callNotice(int status,Long from,Long to,long callTime){
ImDataRes imDataRes = new ImDataRes();
imDataRes.setType(15);
ImDataRes.ImData imData = new ImDataRes.ImData();
imData.setCalltype(1);
imData.setStatus(status); // 通话状态1 取消 2 拒绝 3 超时 4 已通话
imData.setFrom_uid(from);
imData.setTo_uid(to);
imData.setCall_time(callTime);
imDataRes.setData(imData);
return imDataRes;
}
}

View File

@@ -1,33 +0,0 @@
package com.ruoyi.yunxin.data;
import cn.hutool.core.date.DateUtil;
import lombok.Data;
/**
* 系统消息通知
*/
@Data
public class SendAttachMsgData {
private Long id = 11L;
private SendAttachMsgDataMsg data;
@Data
public static class SendAttachMsgDataMsg {
private Long amount;
private Long callTime = DateUtil.currentSeconds();
private Long giftTotal = 0L;
private Long id = 0L;
private Integer linkType = 0;
private Long time = 0L;
private Long toid = 0L;
private Long roomId;
}
public static SendAttachMsgData init(Long roomId){
SendAttachMsgData data = new SendAttachMsgData();
SendAttachMsgDataMsg msgDataMsg = new SendAttachMsgDataMsg();
msgDataMsg.setRoomId(roomId);
data.setData(msgDataMsg);
return data;
}
}

View File

@@ -0,0 +1,26 @@
package com.ruoyi.yunxin.data;
import cn.hutool.core.date.DateUtil;
import lombok.Data;
/**
* ws专用
*/
@Data
public class WsSendAttachMsgData {
private Integer id = 11;
private SendAttachMsgDataMsg data;
@Data
public static class SendAttachMsgDataMsg {
private Integer amount = 0;
private Long callTime = DateUtil.currentSeconds();
private Long gifttotal = 0L;
private Integer id = 0;
private Integer link_type = 0;
private Long time = 0L;
private Long toid = 0L;
private Long roomid;
}
}

View File

@@ -0,0 +1,45 @@
package com.ruoyi.yunxin.service;
import com.alibaba.fastjson.JSON;
import com.ruoyi.yunxin.YunExecutor;
import com.ruoyi.yunxin.Yunxin;
import com.ruoyi.yunxin.data.ImDataRes;
import com.ruoyi.yunxin.data.ImMsgGen;
import com.ruoyi.yunxin.data.WsSendAttachMsgData;
import com.ruoyi.yunxin.resp.SendMsgResp;
import com.ruoyi.yunxin.resp.YxCommonR;
import com.ruoyi.yunxin.resp.YxDataR;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class YunxinWsService {
@Autowired
private Yunxin yunxin;
public void sendCallAsync(Long roomId,Long callId,Long receiverId){
YunExecutor.YUN_EXECUTOR.execute(() -> {
WsSendAttachMsgData data = new WsSendAttachMsgData();
WsSendAttachMsgData.SendAttachMsgDataMsg msgDataMsg = new WsSendAttachMsgData.SendAttachMsgDataMsg();
msgDataMsg.setRoomid(roomId);
data.setData(msgDataMsg);
YxDataR<YxCommonR> r = yunxin.sendAttachMsg(callId, receiverId, data);
if(r == null || !r.isSuccess()){
log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
}
});
}
public void sendToCallNotifyAsync(Long from,Long to,Integer status,Long calltime){
YunExecutor.YUN_EXECUTOR.execute(() -> {
ImDataRes imDataRes = ImMsgGen.callNotice(status, from, to, calltime);
YxDataR<SendMsgResp> r = yunxin.sendTo(from, to, imDataRes);
if(r == null || !r.isSuccess()){
log.error("云信发送失败【sendCallAsync】r={}", JSON.toJSONString(r));
}
});
}
}