fs-danaus
2021-04-26 f10e31da6d8c3e0925bf72828715afdf7d2fe769
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.yc.sdk.WebSocketMessage.entity;
 
import javax.websocket.Session;
 
import com.alibaba.fastjson.JSON;
import com.yc.action.grid.GridUtils;
import com.yc.im.action.WebSocketIMServer;
 
import lombok.Getter;
import lombok.Setter;
 
@Setter
@Getter
public class WsMessageUserEntity extends WsUserEntity {
    transient Session session;
    
    public WsMessageUserEntity(String userCode,Integer dbId,String sessionId,String userFromType,Session session) {
        this.dbId = dbId;
        this.userCode = userCode;
        this.userFromType = userFromType;
        this.sessionId = sessionId;
        this.session = session;
    }
    
    public void sendMessage(MessageInfo message){
 
        if (!this.session.isOpen()){
            return;
        }
        synchronized (this.session) {
            CallBackMessage info = new CallBackMessage();
            //message.setMsgDateTime(new SimpleDateFormat(DateUtil.dateFormatStr,Locale.getDefault()).format(new Date()));
            message.setDbId(this.getDbId());
            info.setInfo(message);
            try {
                this.session.getBasicRemote().sendText(JSON.toJSONString(info));//用异步发送
            } catch (Exception e) {
                e.printStackTrace();
                WebSocketIMServer.printError(e,this.session);
            }
        }
    }
    public void sendMessageV2(Object message){
 
        if (!this.session.isOpen()){
            return;
        }
        synchronized (this.session) {
            CallBackMessage info = new CallBackMessage();
            info.setInfo(message);
            try {
                this.session.getBasicRemote().sendText(JSON.toJSONString(info));//用异步发送
            } catch (Exception e) {
                e.printStackTrace();
                WebSocketIMServer.printError(e,this.session);
            }
        }
    }
}