|
|
@@ -2,23 +2,29 @@ package com.hy.modules.bz.websocket;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.hy.common.web.domain.response.Result;
|
|
|
import com.hy.common.websocket.AbstractWebSocketServer;
|
|
|
import com.hy.modules.bz.domain.BzAnchorStudio;
|
|
|
import com.hy.modules.bz.domain.BzGiftRecord;
|
|
|
+import com.hy.modules.bz.exception.BzException;
|
|
|
import com.hy.modules.bz.service.IBzAnchorStudioService;
|
|
|
import com.hy.modules.bz.service.IBzGiftRecordService;
|
|
|
import com.hy.modules.bz.utils.TokenStatusManager;
|
|
|
+import com.hy.modules.bz.webapi.ErrorCode;
|
|
|
import com.hy.modules.bz.websocket.message.GiftMessage;
|
|
|
import com.hy.modules.bz.websocket.message.MessageType;
|
|
|
import com.hy.modules.sys.domain.SysDictData;
|
|
|
import com.hy.modules.sys.service.SysDictDataService;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.web.socket.TextMessage;
|
|
|
import org.springframework.web.socket.WebSocketSession;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
@@ -69,7 +75,6 @@ public class BarrageWebSocketServer extends AbstractWebSocketServer {
|
|
|
}
|
|
|
}
|
|
|
},reconnectTime, TimeUnit.SECONDS);
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -77,15 +82,35 @@ public class BarrageWebSocketServer extends AbstractWebSocketServer {
|
|
|
JSONObject object = JSON.parseObject(message);
|
|
|
|
|
|
if(MessageType.GiftMessage.equals(MessageType.getMessageType(object.getString(MESSAGE_TYPE_CODE)))){
|
|
|
- // 暂时只处理礼物消息
|
|
|
- GiftMessage giftMessage = JSON.parseObject(message, GiftMessage.class);
|
|
|
- if(!session.getAttributes().containsKey(TOKEN_PRIFIX)){
|
|
|
- session.getAttributes().put(TOKEN_PRIFIX, giftMessage.getToken());
|
|
|
- }
|
|
|
- saveGiftRecord(giftMessage);
|
|
|
- }
|
|
|
+ try{
|
|
|
+ // 暂时只处理礼物消息
|
|
|
+ GiftMessage giftMessage = JSON.parseObject(message, GiftMessage.class);
|
|
|
+ if(StringUtils.isBlank(giftMessage.getToken())){
|
|
|
+ throw new BzException(ErrorCode.TOKEN_INVALID);
|
|
|
+ }
|
|
|
+ if(!session.getAttributes().containsKey(TOKEN_PRIFIX)){
|
|
|
+ // 只有在第一次消息过来的时候发现没有token,此时会设置token
|
|
|
+ session.getAttributes().put(TOKEN_PRIFIX, giftMessage.getToken());
|
|
|
+ }
|
|
|
|
|
|
+ saveGiftRecord(giftMessage);
|
|
|
+ }catch (BzException ex){
|
|
|
+ // 捕获异常时发送消息
|
|
|
+ try {
|
|
|
+ session.sendMessage(new TextMessage(JSON.toJSONString(Result.failure(ex.getErrorCode().getCode(),ex.getMessage()))));
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }catch (Exception ex){
|
|
|
+ try {
|
|
|
+ session.sendMessage(new TextMessage(JSON.toJSONString(Result.failure(500,ex.getMessage()))));
|
|
|
+ throw new RuntimeException(ex);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private BigDecimal getGiftScore(String giftName) throws RuntimeException{
|
|
|
@@ -101,8 +126,12 @@ public class BarrageWebSocketServer extends AbstractWebSocketServer {
|
|
|
}
|
|
|
|
|
|
private void saveGiftRecord(GiftMessage giftMessage){
|
|
|
- BigDecimal score = getGiftScore(giftMessage.getGiftName());
|
|
|
BzAnchorStudio anchorStudio = anchorStudioService.getByRoomId(giftMessage.getToken());
|
|
|
+ if(null == anchorStudio){
|
|
|
+ throw new BzException(ErrorCode.TOKEN_INVALID);
|
|
|
+ }
|
|
|
+
|
|
|
+ BigDecimal score = getGiftScore(giftMessage.getGiftName());
|
|
|
BzGiftRecord giftRecord = new BzGiftRecord();
|
|
|
giftRecord.setAnchorCode(anchorStudio.getCode());
|
|
|
giftRecord.setAnchorId(anchorStudio.getAnchorId());
|