websocket 整合
This commit is contained in:
@@ -32,9 +32,8 @@
|
||||
<version>2.0.32</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-websocket</artifactId>
|
||||
<version>4.8.2</version>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
@@ -59,11 +59,11 @@ public class ChatManager {
|
||||
if(anchor == null){
|
||||
throw new ServiceException("主播技能不存在");
|
||||
}
|
||||
String roomId = null;
|
||||
Long roomId = null;
|
||||
Room room = webSocketManager.checkOnlineRoom(fromUser.getId(), toUser.getId());
|
||||
if(room == null){
|
||||
CaiUserCall call = userCallService.createCall(fromUser, toUser, anchor);
|
||||
roomId = webSocketManager.createRoom(call.getId()+"");
|
||||
roomId = webSocketManager.createRoom(call.getId());
|
||||
}
|
||||
String weSocketUrl = String.format(properties.getWebSocketUrl(),"token",roomId);
|
||||
Long guardPrice = systemConfigManager.getSystemConfigOfLong(SystemConfigEnum.GUARD_PRICE);
|
||||
|
||||
@@ -9,7 +9,7 @@ import java.math.BigDecimal;
|
||||
@Schema(description = "拨打视频返回模型")
|
||||
public class CallResp {
|
||||
@Schema(description = "房间号")
|
||||
private String roomId;
|
||||
private Long roomId;
|
||||
@Schema(description = "拨打者-用户ID")
|
||||
private Long fromUid;
|
||||
@Schema(description = "拨打者-昵称")
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Agora {
|
||||
}
|
||||
|
||||
|
||||
public List<String> getChannelUsers(String roomId){
|
||||
public List<String> getChannelUsers(Long roomId){
|
||||
String url = "http://api.sd-rtn.com/dev/v1/channel/user/%s/%s";
|
||||
url = String.format(url, agoraProperties.getAppId(), roomId);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
@@ -42,7 +42,7 @@ public class Agora {
|
||||
return jsonArray.toJavaList(String.class);
|
||||
}
|
||||
|
||||
public void closeChannel(String roomId){
|
||||
public void closeChannel(Long roomId){
|
||||
String url = "http://api.sd-rtn.com/dev/v1/kicking-rule";
|
||||
Map<String,Object> bodyData = new HashMap<>();
|
||||
bodyData.put("appid",agoraProperties.getAppId());
|
||||
|
||||
@@ -11,8 +11,8 @@ public class ImDataRes {
|
||||
public static class ImData {
|
||||
private Integer callType;
|
||||
private Integer status;
|
||||
private String fromUid;
|
||||
private String toUid;
|
||||
private Integer callTime;
|
||||
private Long fromUid;
|
||||
private Long toUid;
|
||||
private Long callTime;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ public class ImMsgGen {
|
||||
* @param to
|
||||
* @param callTime
|
||||
*/
|
||||
public static ImDataRes callNotice(int status,String from,String to,int 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();
|
||||
|
||||
@@ -5,7 +5,6 @@ import cn.hutool.core.lang.UUID;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.cai.util.RestTemplateUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -23,7 +22,7 @@ public class Yunxin {
|
||||
private final static String SEND_URL = "https://api.netease.im/nimserver/msg/sendMsg.action";
|
||||
private final static String SEND_ATTR_URL = "https://api.netease.im/nimserver/msg/sendAttachMsg.action";
|
||||
|
||||
public void sendTo(String toUid,Object data,String fromUid){
|
||||
public void sendTo(Long toUid,Object data,Long fromUid){
|
||||
Map<String,Object> option = new HashMap<>();
|
||||
option.put("push",true); // 该消息是否需要APNS推送或安卓系统通知栏推送
|
||||
option.put("roam",false); // 该消息是否需要漫游(需要app开通漫游消息功能)
|
||||
@@ -32,7 +31,7 @@ public class Yunxin {
|
||||
option.put("route",false); // 该消息是否需要抄送第三方 (需要app开通消息抄送功能)
|
||||
|
||||
Map<String,Object> bodyData = new HashMap<>();
|
||||
bodyData.put("from", StringUtils.isBlank(fromUid) ? yunxinProperties.getDefaultFromUid() :fromUid);
|
||||
bodyData.put("from", fromUid == null ? yunxinProperties.getDefaultFromUid() :fromUid);
|
||||
bodyData.put("ope", 0);
|
||||
bodyData.put("to", toUid);
|
||||
bodyData.put("type", 100);
|
||||
@@ -49,7 +48,7 @@ public class Yunxin {
|
||||
RestTemplateUtil.restTemplate.postForObject(SEND_URL,httpEntity, JSONObject.class);
|
||||
}
|
||||
|
||||
public void sendAttachMsg(String toUid,Object data,String fromUid){
|
||||
public void sendAttachMsg(Long toUid,Object data,Long fromUid){
|
||||
Map<String,Object> option = new HashMap<>();
|
||||
option.put("push",true); // 该消息是否需要APNS推送或安卓系统通知栏推送
|
||||
option.put("roam",false); // 该消息是否需要漫游(需要app开通漫游消息功能)
|
||||
@@ -58,7 +57,7 @@ public class Yunxin {
|
||||
option.put("route",false); // 该消息是否需要抄送第三方 (需要app开通消息抄送功能)
|
||||
|
||||
Map<String,Object> bodyData = new HashMap<>();
|
||||
bodyData.put("from", StringUtils.isBlank(fromUid) ? yunxinProperties.getDefaultFromUid() :fromUid);
|
||||
bodyData.put("from", fromUid == null ? yunxinProperties.getDefaultFromUid() :fromUid);
|
||||
bodyData.put("ope", 0);
|
||||
bodyData.put("to", toUid);
|
||||
bodyData.put("type", 100);
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package com.ruoyi.cai.ws.bean;
|
||||
|
||||
public class CallerRoom {
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import lombok.Data;
|
||||
public class FdCtxData {
|
||||
|
||||
private String sessionKey;
|
||||
private String roomId;
|
||||
private Long roomId;
|
||||
private Long userId;
|
||||
private Integer userType;
|
||||
private Long tarUserId;
|
||||
|
||||
@@ -10,7 +10,7 @@ public class Room {
|
||||
private UserData callUserData;
|
||||
private UserData receiverUserData;
|
||||
|
||||
public String getRoomId(){
|
||||
public Long getRoomId(){
|
||||
return roomData.getRoomId();
|
||||
}
|
||||
|
||||
@@ -26,4 +26,20 @@ public class Room {
|
||||
return RoomStatusEnums.STATUS_AGREE.getCode().equals(roomData.getStatus());
|
||||
}
|
||||
|
||||
/**
|
||||
* 房间是否已经结算
|
||||
* @return
|
||||
*/
|
||||
public boolean isSettle() {
|
||||
return roomData.getSettleTime() != null && roomData.getSettleTime() > 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 是否已经释放房间资源
|
||||
* @return
|
||||
*/
|
||||
public boolean isReleaseRes(){
|
||||
return roomData.isReleaseRes();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,14 +6,18 @@ import java.math.BigDecimal;
|
||||
|
||||
@Data
|
||||
public class RoomData {
|
||||
private String roomId;
|
||||
private Long roomId;
|
||||
private Long callPrice;
|
||||
private String skillName;
|
||||
private Integer status;
|
||||
private BigDecimal videoDivide;
|
||||
private Integer payCoin = 0;
|
||||
private Integer payIncome = 0;
|
||||
private Long payCoin = 0L;
|
||||
private Long payIncome = 0L;
|
||||
private Long hangUpTime;
|
||||
|
||||
private Long settleTime;
|
||||
|
||||
private Long beginTime;
|
||||
|
||||
private boolean releaseRes = false;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import java.math.BigDecimal;
|
||||
@Data
|
||||
public class UserData {
|
||||
private Long id;
|
||||
private String roomId;
|
||||
private Long roomId;
|
||||
private int userType;
|
||||
private String nickname;
|
||||
private String userCode;
|
||||
@@ -18,4 +18,6 @@ public class UserData {
|
||||
private Long unionUserId;
|
||||
private BigDecimal unionUserRate;
|
||||
private Boolean unionIsGet;
|
||||
|
||||
private Long connectTime;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
package com.ruoyi.cai.ws.cache;
|
||||
|
||||
import com.ruoyi.cai.ws.constant.RedisConstant;
|
||||
import org.apache.commons.lang3.BooleanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.script.DefaultRedisScript;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -17,10 +20,10 @@ public class CallerRoomCache {
|
||||
return String.format(RedisConstant.CALLER_ROOM_DATA,fromUserId);
|
||||
}
|
||||
|
||||
public String getRoomId(Long fromUserId, Long toUserId){
|
||||
public Long getRoomId(Long fromUserId, Long toUserId){
|
||||
String key = getKey(fromUserId);
|
||||
Object roomId = redisTemplate.opsForHash().get(key, toUserId);
|
||||
return roomId == null ? null : String.valueOf(roomId);
|
||||
return roomId == null ? null : Long.valueOf(roomId.toString());
|
||||
}
|
||||
|
||||
public void addRoom(Long fromUid, Long toUid, Long roomId) {
|
||||
@@ -38,4 +41,17 @@ public class CallerRoomCache {
|
||||
String key = getKey(fromUid);
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
private final static String DEL_ROOM_LUA = "return KEYS[1]\n" +
|
||||
"local r = tonumber(redis.call('hget',KEYS[1],ARGV[1]))\n" +
|
||||
"if r == tonumber(ARGV[2]) then\n" +
|
||||
" return redis.call('hdel',KEYS[1],ARGV[1])\n" +
|
||||
"end\n" +
|
||||
"return 0";
|
||||
|
||||
public boolean delRoom(Long receiverId, Long roomId) {
|
||||
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(DEL_ROOM_LUA,Boolean.class);
|
||||
Boolean execute = redisTemplate.execute(redisScript, Collections.singletonList(getKey(roomId)), receiverId,roomId);
|
||||
return BooleanUtils.isTrue(execute);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,11 @@ public class OnlineDataCache {
|
||||
return RedisConstant.ONLINE_ROOM_DATA;
|
||||
}
|
||||
|
||||
public void add(String roomId){
|
||||
redisTemplate.opsForSet().add(getKey(),roomId);
|
||||
public void add(Long roomId){
|
||||
redisTemplate.opsForSet().add(getKey(), String.valueOf(roomId));
|
||||
}
|
||||
|
||||
public void remove(Long roomId) {
|
||||
redisTemplate.opsForSet().remove(getKey(),roomId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package com.ruoyi.cai.ws.cache;
|
||||
|
||||
import com.ruoyi.cai.ws.constant.RedisConstant;
|
||||
import com.ruoyi.cai.ws.constant.UserDataConstant;
|
||||
import com.ruoyi.websocket.holder.WebSocketSessionHolder;
|
||||
import com.ruoyi.cai.ws.holder.WebSocketSessionHolder;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -17,17 +17,17 @@ public class RoomCtxCache {
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
public String getKey(String roomId){
|
||||
public String getKey(Long roomId){
|
||||
return String.format(RedisConstant.FDCTX_ROOM_DATA,roomId);
|
||||
}
|
||||
|
||||
public void addFd(String sessionKey,String roomId,Integer userType){
|
||||
public void addFd(String sessionKey,Long roomId,Integer userType){
|
||||
String key = getKey(roomId);
|
||||
redisTemplate.opsForHash().putIfAbsent(key,sessionKey,userType);
|
||||
redisTemplate.expire(key,7, TimeUnit.DAYS);
|
||||
}
|
||||
|
||||
public List<String> getSessionKeysByRoomId(String roomId){
|
||||
public List<String> getSessionKeysByRoomId(Long roomId){
|
||||
String key = getKey(roomId);
|
||||
Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
|
||||
List<String> res = new ArrayList<>();
|
||||
@@ -37,7 +37,7 @@ public class RoomCtxCache {
|
||||
return res;
|
||||
}
|
||||
|
||||
public String getSessionKeyByRoomIdAndUserType(String roomId,Integer userType){
|
||||
public String getSessionKeyByRoomIdAndUserType(Long roomId,Integer userType){
|
||||
String key = getKey(roomId);
|
||||
Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
|
||||
for (Map.Entry<Object, Object> entry : entries.entrySet()) {
|
||||
@@ -50,15 +50,15 @@ public class RoomCtxCache {
|
||||
return null;
|
||||
}
|
||||
|
||||
public String getSessionKeyReceiverByRoomId(String roomId){
|
||||
public String getSessionKeyReceiverByRoomId(Long roomId){
|
||||
return getSessionKeyByRoomIdAndUserType(roomId, UserDataConstant.TYPE_RECEIVER);
|
||||
}
|
||||
|
||||
public String getSessionKeyCallerByRoomId(String roomId){
|
||||
public String getSessionKeyCallerByRoomId(Long roomId){
|
||||
return getSessionKeyByRoomIdAndUserType(roomId, UserDataConstant.TYPE_CALLER);
|
||||
}
|
||||
|
||||
public void del(String roomId) {
|
||||
public void del(Long roomId) {
|
||||
String key = getKey(roomId);
|
||||
redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
@@ -27,19 +27,16 @@ public class RoomDataCache {
|
||||
@Autowired
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
public String getKey(String roomId){
|
||||
public String getKey(Long roomId){
|
||||
return String.format(RedisConstant.ROOM_DATA,roomId);
|
||||
}
|
||||
|
||||
public RoomData getByRoomId(String roomId){
|
||||
public RoomData getByRoomId(Long roomId){
|
||||
Map<Object, Object> map = stringRedisTemplate.opsForHash().entries(getKey(roomId));
|
||||
if(map.get("roomId") == null){
|
||||
return null;
|
||||
}
|
||||
RoomData roomData = new RoomData();
|
||||
roomData.setRoomId(map.get("roomId").toString());
|
||||
roomData.setCallPrice(Long.valueOf(map.get("callPrice").toString()));
|
||||
return roomData;
|
||||
return JSON.parseObject(JSON.toJSONString(map),RoomData.class);
|
||||
}
|
||||
|
||||
public void init(RoomData roomData) {
|
||||
@@ -47,6 +44,16 @@ public class RoomDataCache {
|
||||
stringRedisTemplate.opsForHash().putAll(getKey(roomData.getRoomId()),map);
|
||||
}
|
||||
|
||||
public void hMSet(Long roomId,Map<String,Object> data) {
|
||||
String key = getKey(roomId);
|
||||
stringRedisTemplate.opsForHash().putAll(key,data);
|
||||
}
|
||||
|
||||
public void hMSet(Long roomId,String mKey,Object mData) {
|
||||
String key = getKey(roomId);
|
||||
stringRedisTemplate.opsForHash().put(key,mKey,mData);
|
||||
}
|
||||
|
||||
|
||||
private final static String HANG_UP = "local hangupTime = tonumber(redis.call('hGet', KEYS[1], 'hangupTime'))\n" +
|
||||
"if hangupTime > 0 then\n" +
|
||||
@@ -54,14 +61,14 @@ public class RoomDataCache {
|
||||
"end\n" +
|
||||
"return redis.call('hMSet', KEYS[1], 'status', 8, 'hangupTime', ARGV[1])";
|
||||
|
||||
public boolean hangUp(String roomId) {
|
||||
public boolean hangUp(Long roomId) {
|
||||
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(HANG_UP,Boolean.class);
|
||||
String currentTime = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
Boolean execute = stringRedisTemplate.execute(redisScript, Collections.singletonList(getKey(roomId)), currentTime);
|
||||
return BooleanUtils.isTrue(execute);
|
||||
}
|
||||
|
||||
public void setStatus(String roomId, RoomStatusEnums status) {
|
||||
public void setStatus(Long roomId, RoomStatusEnums status) {
|
||||
String key = getKey(roomId);
|
||||
stringRedisTemplate.opsForHash().put(key,"status",status.getCode());
|
||||
}
|
||||
@@ -72,7 +79,7 @@ public class RoomDataCache {
|
||||
"end\n" +
|
||||
"return redis.call('hmset', KEYS[1], 'status', 3)";
|
||||
|
||||
public boolean setStatusReceiverConnection(String roomId) {
|
||||
public boolean setStatusReceiverConnection(Long roomId) {
|
||||
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(SET_STATUS_RECEIVER_CONNECTION,Boolean.class);
|
||||
Boolean execute = stringRedisTemplate.execute(redisScript, Collections.singletonList(getKey(roomId)));
|
||||
return BooleanUtils.isTrue(execute);
|
||||
@@ -86,7 +93,7 @@ public class RoomDataCache {
|
||||
"end\n" +
|
||||
"return redis.call('hmset', KEYS[1], 'status', 7, 'beginTime', ARGV[1])";
|
||||
|
||||
public boolean setStatusAgree(String roomId) {
|
||||
public boolean setStatusAgree(Long roomId) {
|
||||
DefaultRedisScript<Boolean> redisScript = new DefaultRedisScript<>(SET_STATUS_AGREE,Boolean.class);
|
||||
String currentTime = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
Boolean execute = stringRedisTemplate.execute(redisScript, Collections.singletonList(getKey(roomId)),currentTime);
|
||||
|
||||
@@ -4,7 +4,6 @@ import com.alibaba.fastjson2.JSON;
|
||||
import com.ruoyi.cai.ws.bean.UserData;
|
||||
import com.ruoyi.cai.ws.constant.RedisConstant;
|
||||
import com.ruoyi.cai.ws.constant.UserDataConstant;
|
||||
import com.ruoyi.cai.ws.util.MapGetUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -16,37 +15,25 @@ public class UserDataCache {
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
public String getKey(String roomId,int type){
|
||||
public String getKey(Long roomId,int type){
|
||||
return String.format(RedisConstant.USER_ROOM_DATA,roomId,type== UserDataConstant.TYPE_CALLER?"caller":"receiver");
|
||||
}
|
||||
|
||||
public UserData getCallerUserDataByRoom(String roomId){
|
||||
public UserData getCallerUserDataByRoom(Long roomId){
|
||||
return getUserDataByRoom(roomId,UserDataConstant.TYPE_CALLER);
|
||||
}
|
||||
|
||||
public UserData getReceiverUserDataByRoom(String roomId){
|
||||
public UserData getReceiverUserDataByRoom(Long roomId){
|
||||
return getUserDataByRoom(roomId,UserDataConstant.TYPE_RECEIVER);
|
||||
}
|
||||
|
||||
public UserData getUserDataByRoom(String roomId,int type){
|
||||
public UserData getUserDataByRoom(Long roomId,int type){
|
||||
String key = getKey(roomId, type);
|
||||
Map<Object, Object> entries = redisTemplate.opsForHash().entries(key);
|
||||
if(entries.get("roomId") == null){
|
||||
return null;
|
||||
}
|
||||
UserData userData = new UserData();
|
||||
userData.setId(MapGetUtil.getLong(entries.get("id")));
|
||||
userData.setRoomId(MapGetUtil.getString(entries.get("roomId")));
|
||||
userData.setUserType(MapGetUtil.getInt(entries.get("userType")));
|
||||
userData.setNickname(MapGetUtil.getString(entries.get("nickname")));
|
||||
userData.setUserCode(MapGetUtil.getString(entries.get("userCode")));
|
||||
userData.setInviterId(MapGetUtil.getLong(entries.get("inviterId")));
|
||||
userData.setInviterRate(MapGetUtil.getBigDecimal(entries.get("inviterRate")));
|
||||
userData.setInviterIsGet(MapGetUtil.getBoolean(entries.get("inviterIsGet")));
|
||||
userData.setUnionUserId(MapGetUtil.getLong(entries.get("unionUserId")));
|
||||
userData.setUnionUserRate(MapGetUtil.getBigDecimal(entries.get("unionUserRate")));
|
||||
userData.setUnionIsGet(MapGetUtil.getBoolean(entries.get("unionIsGet")));
|
||||
return userData;
|
||||
return JSON.parseObject(JSON.toJSONString(entries),UserData.class);
|
||||
}
|
||||
|
||||
public void init(UserData userData,int type){
|
||||
@@ -64,7 +51,7 @@ public class UserDataCache {
|
||||
init(callerUserData,UserDataConstant.TYPE_RECEIVER);
|
||||
}
|
||||
|
||||
public void hMSet(String roomId,Integer userType,Map<String,Object> data) {
|
||||
public void hMSet(Long roomId,Integer userType,Map<String,Object> data) {
|
||||
String key = getKey(roomId, userType);
|
||||
redisTemplate.opsForHash().putAll(key,data);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.config;
|
||||
package com.ruoyi.cai.ws.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.config;
|
||||
package com.ruoyi.cai.ws.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.constant;
|
||||
package com.ruoyi.cai.ws.constant;
|
||||
|
||||
/**
|
||||
* websocket的常量配置
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.dto;
|
||||
package com.ruoyi.cai.ws.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.dto;
|
||||
package com.ruoyi.cai.ws.dto;
|
||||
|
||||
import com.ruoyi.common.constant.HttpStatus;
|
||||
import lombok.Data;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.dto;
|
||||
package com.ruoyi.cai.ws.dto;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.ruoyi.cai.domain.CaiGift;
|
||||
@@ -8,7 +8,7 @@ import java.util.Map;
|
||||
|
||||
public class WsRMsgGen {
|
||||
|
||||
public static WsR response(String roomId){
|
||||
public static WsR response(Long roomId){
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("roomid",roomId);
|
||||
WsR<Map<String, Object>> ok = WsR.ok(map);
|
||||
@@ -17,7 +17,7 @@ public class WsRMsgGen {
|
||||
return ok;
|
||||
}
|
||||
|
||||
public static WsR startVideo(String roomId,Long duration){
|
||||
public static WsR startVideo(Long roomId,Long duration){
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("roomid",roomId);
|
||||
map.put("duration",duration);
|
||||
@@ -51,7 +51,7 @@ public class WsRMsgGen {
|
||||
return ok;
|
||||
}
|
||||
|
||||
public static WsR hangup(String message, String roomId, Integer hangUpType) {
|
||||
public static WsR hangup(String message, Long roomId, Integer hangUpType) {
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("roomid","roomId");
|
||||
map.put("type","hangUpType");
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.ruoyi.websocket.handler;
|
||||
package com.ruoyi.cai.ws.handler;
|
||||
|
||||
import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.cache.RoomCtxCache;
|
||||
import com.ruoyi.websocket.dto.WsR;
|
||||
import com.ruoyi.websocket.util.RoomWebSocketUtil;
|
||||
import com.ruoyi.cai.ws.dto.WsR;
|
||||
import com.ruoyi.cai.ws.util.RoomWebSocketUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -24,12 +24,12 @@ public abstract class AbstractMessageHandle implements IMessageHandler {
|
||||
RoomWebSocketUtil.sendSendMessage(sessionKey, r);
|
||||
}
|
||||
|
||||
protected void sendToReceiver(String roomId, WsR r){
|
||||
protected void sendToReceiver(Long roomId, WsR r){
|
||||
String receiverSessionKey = roomCtxCache.getSessionKeyReceiverByRoomId(roomId);
|
||||
RoomWebSocketUtil.sendSendMessage(receiverSessionKey, r);
|
||||
}
|
||||
|
||||
protected void sendToAll(String roomId, WsR ... r ){
|
||||
protected void sendToAll(Long roomId, WsR ... r ){
|
||||
List<String> sessionKeys = roomCtxCache.getSessionKeysByRoomId(roomId);
|
||||
for (WsR wsR : r) {
|
||||
RoomWebSocketUtil.sendSendMessage(sessionKeys, wsR);
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.handler;
|
||||
package com.ruoyi.cai.ws.handler;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.handler;
|
||||
package com.ruoyi.cai.ws.handler;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
@@ -8,6 +8,7 @@ import com.ruoyi.cai.ws.cache.FdCtxDataCache;
|
||||
import com.ruoyi.cai.ws.constant.HangUpEnums;
|
||||
import com.ruoyi.cai.ws.service.CheckConnectionDTO;
|
||||
import com.ruoyi.cai.ws.service.RoomService;
|
||||
import com.ruoyi.cai.ws.util.MapGetUtil;
|
||||
import com.ruoyi.cai.ws.util.WsExceptionUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -37,8 +38,8 @@ public class MessageHandleApplication {
|
||||
return;
|
||||
}
|
||||
Map<String, Object> attributes = session.getAttributes();
|
||||
String roomId = (String) attributes.get("roomId");
|
||||
String sessionKey = (String) attributes.get("token");
|
||||
Long roomId = MapGetUtil.getLong(attributes.get("roomId"));
|
||||
String sessionKey = MapGetUtil.getString(attributes.get("token"));
|
||||
Room room = roomService.load(roomId);
|
||||
if(room == null){
|
||||
WsExceptionUtil.throwException("房间不可用", sessionKey,HangUpEnums.OTHER, roomId);
|
||||
@@ -1,8 +1,9 @@
|
||||
package com.ruoyi.websocket.handler;
|
||||
package com.ruoyi.cai.ws.handler;
|
||||
|
||||
import com.ruoyi.websocket.constant.WebSocketConstants;
|
||||
import com.ruoyi.websocket.holder.WebSocketSessionHolder;
|
||||
import com.ruoyi.websocket.util.WebSocketUtils;
|
||||
import com.ruoyi.cai.ws.constant.WebSocketConstants;
|
||||
import com.ruoyi.cai.ws.holder.WebSocketSessionHolder;
|
||||
import com.ruoyi.cai.ws.processon.OpenLogic;
|
||||
import com.ruoyi.cai.ws.util.WebSocketUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -21,7 +22,7 @@ import java.util.Map;
|
||||
public class RoomWebSocketHandler extends AbstractWebSocketHandler {
|
||||
|
||||
@Autowired
|
||||
private IOpenLogic openLogic;
|
||||
private OpenLogic openLogic;
|
||||
@Autowired
|
||||
private MessageHandleApplication messageHandleApplication;
|
||||
/**
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
@@ -8,9 +8,9 @@ import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.cache.RoomDataCache;
|
||||
import com.ruoyi.cai.ws.constant.RoomStatusEnums;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.service.RoomService;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
@@ -11,10 +11,10 @@ import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.constant.HangUpEnums;
|
||||
import com.ruoyi.cai.ws.constant.RoomStatusEnums;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.service.RoomService;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -38,13 +38,13 @@ public class CancelMessageHandler extends AbstractMessageHandle implements IMess
|
||||
&& !RoomStatusEnums.STATUS_RECEIVER_CONNECT.getCode().equals(room.getStatus())){
|
||||
return;
|
||||
}
|
||||
String roomId = room.getRoomId();
|
||||
Long roomId = room.getRoomId();
|
||||
sendToCurrent(fdCtxData,WsRMsgGen.hangup("通话已取消",roomId, HangUpEnums.CANCEL.getCode()));
|
||||
sendToReceiver(roomId,WsRMsgGen.hangup("对方已取消",roomId, HangUpEnums.CANCEL.getCode()));
|
||||
roomService.closeAllFd(roomId);
|
||||
// IM
|
||||
String receiverId = room.getReceiverUserData().getId() +"";
|
||||
String callerId = room.getCallUserData().getId()+"";
|
||||
Long receiverId = room.getReceiverUserData().getId();
|
||||
Long callerId = room.getCallUserData().getId();
|
||||
ImDataRes imDataRes = ImMsgGen.callNotice(1, callerId, receiverId, 0);
|
||||
yunxin.sendTo(receiverId,imDataRes,callerId);
|
||||
// 更新房间状态
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.cai.domain.CaiAccount;
|
||||
@@ -9,9 +9,9 @@ import com.ruoyi.cai.service.CaiGiftService;
|
||||
import com.ruoyi.cai.service.CaiUserGiftService;
|
||||
import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,23 +1,17 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.cache.RoomCtxCache;
|
||||
import com.ruoyi.cai.ws.cache.RoomDataCache;
|
||||
import com.ruoyi.cai.ws.constant.HangUpEnums;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.service.RoomService;
|
||||
import com.ruoyi.websocket.dto.WsR;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.websocket.util.RoomWebSocketUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 挂断处理
|
||||
*/
|
||||
@@ -30,7 +24,7 @@ public class HangupMessageHandler extends AbstractMessageHandle implements IMess
|
||||
|
||||
@Override
|
||||
public void processOn(Room room, FdCtxData fdCtxData, JSONObject map) {
|
||||
if(StringUtils.isEmpty(room.getRoomId())){
|
||||
if(room.getRoomId() == null){
|
||||
return;
|
||||
}
|
||||
// 经测试,app端挂断时,可能会把旧的房间id传上来,所以需要判断id与fd上下文的一致性
|
||||
@@ -43,6 +37,7 @@ public class HangupMessageHandler extends AbstractMessageHandle implements IMess
|
||||
}
|
||||
// 触发结算 TODO
|
||||
|
||||
|
||||
Integer type = fdCtxData.isCaller() ? HangUpEnums.FROM.getCode() : HangUpEnums.TO.getCode();
|
||||
sendToCurrent(fdCtxData,WsRMsgGen.hangup("您已挂断",room.getRoomId(), type));
|
||||
sendToTar(fdCtxData,WsRMsgGen.hangup("对方已挂断",room.getRoomId(), type));
|
||||
@@ -1,13 +1,13 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.cache.UserDataCache;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.cai.ws.bean.FdCtxData;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.handler.message;
|
||||
package com.ruoyi.cai.ws.handler.message;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
@@ -12,10 +12,10 @@ import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.cache.RoomDataCache;
|
||||
import com.ruoyi.cai.ws.constant.HangUpEnums;
|
||||
import com.ruoyi.cai.ws.constant.RoomStatusEnums;
|
||||
import com.ruoyi.cai.ws.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.service.RoomService;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.AbstractMessageHandle;
|
||||
import com.ruoyi.websocket.handler.IMessageHandler;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.handler.AbstractMessageHandle;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -46,8 +46,8 @@ public class RefuseMessageHandler extends AbstractMessageHandle implements IMess
|
||||
roomService.closeAllFd(room.getRoomId());
|
||||
|
||||
//发送IM通知
|
||||
String receiverId = room.getReceiverUserData().getId()+"";
|
||||
String callerId = room.getCallUserData().getId()+"";
|
||||
Long receiverId = room.getReceiverUserData().getId();
|
||||
Long callerId = room.getCallUserData().getId();
|
||||
ImDataRes imDataRes = ImMsgGen.callNotice(1, callerId, receiverId, 0);
|
||||
yunxin.sendTo(receiverId,imDataRes,callerId);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.holder;
|
||||
package com.ruoyi.cai.ws.holder;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.ruoyi.websocket.interceptor;
|
||||
package com.ruoyi.cai.ws.interceptor;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.server.ServerHttpRequest;
|
||||
@@ -16,7 +16,6 @@ import com.ruoyi.cai.ws.cache.UserDataCache;
|
||||
import com.ruoyi.cai.ws.constant.RedisConstant;
|
||||
import com.ruoyi.cai.ws.service.RoomService;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.redisson.api.RLock;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -44,8 +43,8 @@ public class WebSocketManager {
|
||||
private RedissonClient redissonClient;
|
||||
|
||||
public Room checkOnlineRoom(Long fromUserId,Long toUserId){
|
||||
String roomId = callerRoomCache.getRoomId(fromUserId, toUserId);
|
||||
if(StringUtils.isEmpty(roomId)){
|
||||
Long roomId = callerRoomCache.getRoomId(fromUserId, toUserId);
|
||||
if(roomId == null){
|
||||
return null;
|
||||
}
|
||||
Room room = roomService.load(roomId);
|
||||
@@ -55,7 +54,7 @@ public class WebSocketManager {
|
||||
return room;
|
||||
}
|
||||
|
||||
public String createRoom(String roomId) {
|
||||
public Long createRoom(Long roomId) {
|
||||
CaiUserCall userCall = userCallService.getById(roomId);
|
||||
if(userCall == null){
|
||||
throw new ServiceException("无效房间");
|
||||
@@ -77,7 +76,7 @@ public class WebSocketManager {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public String initRoom(CaiUserCall call){
|
||||
public Long initRoom(CaiUserCall call){
|
||||
call = userCallService.getById(call.getId());
|
||||
CaiUser callUser = userService.getById(call.getFromUid());
|
||||
if(callUser == null){
|
||||
@@ -103,7 +102,7 @@ public class WebSocketManager {
|
||||
roomService.delCallRoom(call.getFromUid());
|
||||
// 初始化
|
||||
RoomData roomData = new RoomData();
|
||||
roomData.setRoomId(call.getId()+"");
|
||||
roomData.setRoomId(call.getId());
|
||||
roomData.setCallPrice(call.getCallPrice());
|
||||
roomData.setSkillName(call.getSkillName());
|
||||
roomData.setStatus(call.getStatus());
|
||||
@@ -111,13 +110,13 @@ public class WebSocketManager {
|
||||
roomDataCache.init(roomData);
|
||||
UserData callerUserData = new UserData();
|
||||
callerUserData.setId(call.getFromUid());
|
||||
callerUserData.setRoomId(call.getId()+"");
|
||||
callerUserData.setRoomId(call.getId());
|
||||
callerUserData.setNickname(callUser.getNickname());
|
||||
callerUserData.setUserCode(callUser.getUsercode());
|
||||
userDataCache.initCaller(callerUserData);
|
||||
UserData receiveUserData = new UserData();
|
||||
receiveUserData.setId(call.getToUid());
|
||||
receiveUserData.setRoomId(call.getId()+"");
|
||||
receiveUserData.setRoomId(call.getId());
|
||||
receiveUserData.setNickname(receiverUser.getNickname());
|
||||
receiveUserData.setUserCode(receiverUser.getUsercode());
|
||||
receiveUserData.setInviterId(call.getReceiverInviteUserId());
|
||||
@@ -128,7 +127,7 @@ public class WebSocketManager {
|
||||
receiveUserData.setUnionIsGet(call.getReceiverUnionGet());
|
||||
userDataCache.initReceiver(callerUserData);
|
||||
callerRoomCache.addRoom(call.getFromUid(),call.getToUid(),call.getId());
|
||||
return call.getId()+"";
|
||||
return call.getId();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,12 @@ import com.ruoyi.cai.ws.cache.*;
|
||||
import com.ruoyi.cai.ws.constant.HangUpEnums;
|
||||
import com.ruoyi.cai.ws.constant.RoomStatusEnums;
|
||||
import com.ruoyi.cai.ws.constant.UserDataConstant;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import com.ruoyi.cai.ws.service.CheckConnectionDTO;
|
||||
import com.ruoyi.cai.ws.service.RoomService;
|
||||
import com.ruoyi.cai.ws.util.MapGetUtil;
|
||||
import com.ruoyi.cai.ws.util.RoomWebSocketUtil;
|
||||
import com.ruoyi.cai.ws.util.WsExceptionUtil;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.handler.IOpenLogic;
|
||||
import com.ruoyi.websocket.util.RoomWebSocketUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -27,7 +27,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
public class OpenLogic implements IOpenLogic {
|
||||
public class OpenLogic {
|
||||
@Autowired
|
||||
private RoomService roomService;
|
||||
@Autowired
|
||||
@@ -47,17 +47,16 @@ public class OpenLogic implements IOpenLogic {
|
||||
@Autowired
|
||||
private Agora agora;
|
||||
|
||||
@Override
|
||||
public void processOn(WebSocketSession session) {
|
||||
Map<String, Object> map = session.getAttributes();
|
||||
String token = map.get("token").toString();
|
||||
String roomId = map.get("roomId").toString();
|
||||
Long userId = Long.valueOf(map.get("userId").toString());
|
||||
String token = MapGetUtil.getString(map.get("token"));
|
||||
Long roomId = MapGetUtil.getLong(map.get("roomId"));
|
||||
Long userId = MapGetUtil.getLong(map.get("userId"));
|
||||
// 校验token
|
||||
process(token,roomId,userId);
|
||||
}
|
||||
|
||||
public void process(String sessionKey,String roomId,Long userId){
|
||||
public void process(String sessionKey,Long roomId,Long userId){
|
||||
Room room = roomService.load(roomId);
|
||||
if(room == null || (room.getCallUserData().getId().equals(userId) && room.getReceiverUserData().getId().equals(userId))){
|
||||
WsExceptionUtil.throwException(sessionKey,"房间不可用", HangUpEnums.OTHER,roomId);
|
||||
@@ -117,7 +116,7 @@ public class OpenLogic implements IOpenLogic {
|
||||
return;
|
||||
}
|
||||
Long callTime = roomService.getCallTime(room);
|
||||
RoomWebSocketUtil.sendSendMessage(sessionKey,WsRMsgGen.startVideo(room.getRoomId(), callTime));
|
||||
RoomWebSocketUtil.sendSendMessage(sessionKey, WsRMsgGen.startVideo(room.getRoomId(), callTime));
|
||||
RoomWebSocketUtil.sendSendMessage(sessionKey,WsRMsgGen.sysNotice("重连成功,房间已通话(转换成时分秒) "));
|
||||
String sessionKeyReceiver = roomCtxCache.getSessionKeyReceiverByRoomId(room.getRoomId());
|
||||
if(StringUtils.isNotEmpty(sessionKeyReceiver)){
|
||||
|
||||
@@ -2,14 +2,19 @@ package com.ruoyi.cai.ws.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ruoyi.cai.domain.CaiAccount;
|
||||
import com.ruoyi.cai.executor.ExecutorConstant;
|
||||
import com.ruoyi.cai.service.CaiAccountService;
|
||||
import com.ruoyi.cai.service.CaiAnchorService;
|
||||
import com.ruoyi.cai.service.CaiUserService;
|
||||
import com.ruoyi.cai.trd.Agora;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.bean.RoomData;
|
||||
import com.ruoyi.cai.ws.bean.UserData;
|
||||
import com.ruoyi.cai.ws.cache.*;
|
||||
import com.ruoyi.cai.ws.constant.HangUpEnums;
|
||||
import com.ruoyi.cai.ws.constant.RoomStatusEnums;
|
||||
import com.ruoyi.websocket.util.WebSocketUtils;
|
||||
import com.ruoyi.cai.ws.util.MapGetUtil;
|
||||
import com.ruoyi.cai.ws.util.WebSocketUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -33,8 +38,14 @@ public class RoomService {
|
||||
private FdCtxDataCache fdCtxDataCache;
|
||||
@Autowired
|
||||
private RoomCtxCache roomCtxCache;
|
||||
@Autowired
|
||||
private CaiUserService userService;
|
||||
@Autowired
|
||||
private CaiAnchorService anchorService;
|
||||
@Autowired
|
||||
private Agora agora;
|
||||
|
||||
public Room load(String roomId){
|
||||
public Room load(Long roomId){
|
||||
Room room = new Room();
|
||||
RoomData roomData = roomDataCache.getByRoomId(roomId);
|
||||
if(roomData == null){
|
||||
@@ -57,7 +68,7 @@ public class RoomService {
|
||||
public void closeAllRoom(Long fromUid){
|
||||
Map<Object, Object> all = callerRoomCache.getAll(fromUid);
|
||||
for (Map.Entry<Object, Object> entry : all.entrySet()) {
|
||||
String roomId = String.valueOf(entry.getValue());
|
||||
Long roomId = MapGetUtil.getLong(entry.getValue());
|
||||
Room room = load(roomId);
|
||||
if(room == null){
|
||||
continue;
|
||||
@@ -74,7 +85,7 @@ public class RoomService {
|
||||
}
|
||||
}
|
||||
|
||||
public void closeAllFd(String roomId){
|
||||
public void closeAllFd(Long roomId){
|
||||
List<String> sessionKeysByRoomId = roomCtxCache.getSessionKeysByRoomId(roomId);
|
||||
for (String sessionKey : sessionKeysByRoomId) {
|
||||
WebSocketUtils.close(sessionKey);
|
||||
@@ -86,7 +97,7 @@ public class RoomService {
|
||||
public void delCallRoom(Long fromUid) {
|
||||
callerRoomCache.del(fromUid);
|
||||
}
|
||||
private static Map<Integer, CheckConnectionDTO> STATUS_TO_HANG_UP = new HashMap<>();
|
||||
private static final Map<Integer, CheckConnectionDTO> STATUS_TO_HANG_UP = new HashMap<>();
|
||||
static {
|
||||
STATUS_TO_HANG_UP.put(RoomStatusEnums.STATUS_CALLER_CANCEL.getCode(),new CheckConnectionDTO(HangUpEnums.CANCEL,"通话已取消"));
|
||||
STATUS_TO_HANG_UP.put(RoomStatusEnums.STATUS_CONNECT_CANCEL.getCode(),new CheckConnectionDTO(HangUpEnums.TIMEOUT,"通话未接听"));
|
||||
@@ -127,7 +138,7 @@ public class RoomService {
|
||||
return 0L;
|
||||
}
|
||||
RoomData roomData = room.getRoomData();
|
||||
int blockAmount = roomData.getPayCoin() + roomData.getPayIncome();
|
||||
long blockAmount = roomData.getPayCoin() + roomData.getPayIncome();
|
||||
long totalAmount = account.getTotalCoin()+account.getIncomeCoin() + blockAmount;
|
||||
long totalSecond = (totalAmount / roomData.getCallPrice()) / 60;
|
||||
long useTime = 0;
|
||||
@@ -140,4 +151,40 @@ public class RoomService {
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放房间资源
|
||||
*/
|
||||
public void releaseRes(Long roomId) {
|
||||
Room room = this.load(roomId);
|
||||
if(room == null){
|
||||
return;
|
||||
}
|
||||
try {
|
||||
UserData receiverUserData = room.getReceiverUserData();
|
||||
Long receiverId = receiverUserData.getId();
|
||||
Long callerId = room.getCallUserData().getId();
|
||||
// 呼叫方释放资源
|
||||
callerRoomCache.delRoom(receiverId,roomId);
|
||||
userService.updateVideoStatus(callerId,0);
|
||||
// 接收方释放资源(已连接的情况下)
|
||||
if(receiverUserData.getConnectTime() != null && receiverUserData.getConnectTime() > 0){
|
||||
userService.updateVideoStatus(receiverId,0);
|
||||
anchorService.updateVideoStatus(receiverId,0);
|
||||
}
|
||||
// 声网踢人
|
||||
if(room.getRoomData().getBeginTime() != null && room.getRoomData().getBeginTime() > 0){
|
||||
ExecutorConstant.ROOM_EXECUTOR.execute(() -> {
|
||||
agora.closeChannel(roomId);
|
||||
});
|
||||
}
|
||||
// 修改释放状态
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("releaseRes",1);
|
||||
roomDataCache.hMSet(roomId,map);
|
||||
}catch (Exception e){
|
||||
log.error("释放房间资源失败!房间号:{}",roomId,e);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
package com.ruoyi.cai.ws.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.ruoyi.cai.domain.CaiAccount;
|
||||
import com.ruoyi.cai.service.CaiAccountService;
|
||||
import com.ruoyi.cai.trd.ImDataRes;
|
||||
import com.ruoyi.cai.trd.ImMsgGen;
|
||||
import com.ruoyi.cai.trd.Yunxin;
|
||||
import com.ruoyi.cai.ws.bean.Room;
|
||||
import com.ruoyi.cai.ws.bean.RoomData;
|
||||
import com.ruoyi.cai.ws.bean.UserData;
|
||||
import com.ruoyi.cai.ws.cache.OnlineDataCache;
|
||||
import com.ruoyi.cai.ws.cache.RoomDataCache;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class SettleService {
|
||||
|
||||
@Autowired
|
||||
private RoomService roomService;
|
||||
@Autowired
|
||||
private RoomDataCache roomDataCache;
|
||||
@Autowired
|
||||
private CaiAccountService accountService;
|
||||
@Autowired
|
||||
private OnlineDataCache onlineDataCache;
|
||||
@Autowired
|
||||
private Yunxin yunxin;
|
||||
|
||||
public void processOn(Room room){
|
||||
try {
|
||||
deal(room);
|
||||
}finally {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void deal(Room room){
|
||||
if(room.isCanCall() || room.isSettle()){
|
||||
return;
|
||||
}
|
||||
if(!room.isReleaseRes()){
|
||||
roomService.releaseRes(Long.valueOf(room.getRoomId()));
|
||||
return;
|
||||
}
|
||||
// 结算
|
||||
if(!settle(room)){
|
||||
return;
|
||||
}
|
||||
// 移除在线房间状态
|
||||
onlineDataCache.remove(room.getRoomId());
|
||||
// 更新支付金额信息
|
||||
Map<String,Object> map = new HashMap<>();
|
||||
map.put("payCoin", ""); // 实际支付的金额
|
||||
map.put("payIncome", "");
|
||||
map.put("settleTime", DateUtil.currentSeconds());
|
||||
roomDataCache.hMSet(Long.valueOf(room.getRoomId()),map);
|
||||
// 主叫方通话市场通知
|
||||
Long callTime = roomService.getCallTime(room);
|
||||
Long receiverUserId = room.getReceiverUserData().getId();
|
||||
Long callUserId = room.getCallUserData().getId();
|
||||
if(callTime > 0){
|
||||
ImDataRes imDataRes = ImMsgGen.callNotice(4, receiverUserId, callUserId, callTime);
|
||||
yunxin.sendTo(room.getCallUserData().getId(),imDataRes,room.getReceiverUserData().getId());
|
||||
}
|
||||
// 收入通知
|
||||
if(room != null){ // TODO修改数据
|
||||
|
||||
}
|
||||
// 邀请人收入通知
|
||||
// 排行榜通知
|
||||
}
|
||||
|
||||
/**
|
||||
* 结算
|
||||
*/
|
||||
private boolean settle(Room room){
|
||||
Long roomId = room.getRoomId();
|
||||
// 未通话,无需结算
|
||||
if(room.getRoomData().getBeginTime() == null){
|
||||
roomDataCache.hMSet(roomId,"settleTime", DateUtil.currentSeconds());
|
||||
return false;
|
||||
}
|
||||
// 开始结算
|
||||
// 呼叫方扣费
|
||||
this.computeCallerPay(room);
|
||||
// 结束结算
|
||||
return true;
|
||||
}
|
||||
|
||||
private void computeCallerPay(Room room) {
|
||||
RoomData roomData = room.getRoomData();
|
||||
UserData callUserData = room.getCallUserData();
|
||||
Long callPrice = roomData.getCallPrice();
|
||||
Long callTime = roomService.getCallTime(room);
|
||||
// 本次支付金额
|
||||
Long totalAmount = callPrice * (callTime / 60);
|
||||
// 补差价
|
||||
Long diff = totalAmount - roomData.getPayCoin() + roomData.getPayIncome();
|
||||
|
||||
CaiAccount callAccount = accountService.getByUserId(callUserData.getId());
|
||||
Long userAmount = callAccount.getCoin() + callAccount.getIncomeCoin();
|
||||
if(diff > 0){
|
||||
// 账户上面有余额
|
||||
if(userAmount > 0){
|
||||
diff = (userAmount > diff) ? diff : userAmount;
|
||||
log.info("roomid {} 已支付 {} 仍需要补差价:{}", roomData.getRoomId(),roomData.getPayCoin() + roomData.getPayIncome(),diff);
|
||||
// TODO 对账户进行统一扣费
|
||||
|
||||
}else{
|
||||
log.info("房间需要补差价,但用户余额不足 roomid {} 已支付 {} 仍需要补差价:{}", roomData.getRoomId(),roomData.getPayCoin() + roomData.getPayIncome(),diff);
|
||||
}
|
||||
}else{
|
||||
// 退钱逻辑
|
||||
diff = Math.abs(diff);
|
||||
// TODO 对账户进行统一退钱逻辑
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.ruoyi.websocket.util;
|
||||
package com.ruoyi.cai.ws.util;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.ruoyi.websocket.dto.WsR;
|
||||
import com.ruoyi.cai.ws.dto.WsR;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RoomWebSocketUtil {
|
||||
public static void sendSendMessage(String sessionKey,WsR r){
|
||||
public static void sendSendMessage(String sessionKey, WsR r){
|
||||
WebSocketUtils.sendMessage(sessionKey, JSON.toJSONString(r));
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.ruoyi.websocket.util;
|
||||
package com.ruoyi.cai.ws.util;
|
||||
|
||||
import com.ruoyi.websocket.holder.WebSocketSessionHolder;
|
||||
import jdk.nashorn.internal.runtime.regexp.JoniRegExp;
|
||||
import com.ruoyi.cai.ws.holder.WebSocketSessionHolder;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -1,15 +1,12 @@
|
||||
package com.ruoyi.cai.ws.util;
|
||||
|
||||
import com.esotericsoftware.minlog.Log;
|
||||
import com.ruoyi.cai.ws.constant.HangUpEnums;
|
||||
import com.ruoyi.websocket.dto.WsRMsgGen;
|
||||
import com.ruoyi.websocket.util.RoomWebSocketUtil;
|
||||
import com.ruoyi.websocket.util.WebSocketUtils;
|
||||
import com.ruoyi.cai.ws.dto.WsRMsgGen;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
public class WsExceptionUtil {
|
||||
public static void throwException(String sessionKey, String message, HangUpEnums hangUpType, String roomId){
|
||||
public static void throwException(String sessionKey, String message, HangUpEnums hangUpType, Long roomId){
|
||||
if(hangUpType == null){
|
||||
hangUpType = HangUpEnums.OTHER;
|
||||
}
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-vue-plus</artifactId>
|
||||
<version>4.8.2</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>ruoyi-websocket</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-cai</artifactId>
|
||||
<version>4.8.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>2.0.32</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,9 +0,0 @@
|
||||
package com.ruoyi.websocket.handler;
|
||||
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
public interface IOpenLogic {
|
||||
|
||||
void processOn(WebSocketSession session);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user