shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.tcp.websocket;
 
import com.walker.infrastructure.utils.JsonUtils;
import com.walker.infrastructure.utils.StringUtils;
import com.walker.tcp.data.BaseResponse;
import com.walker.tcp.util.WebSocketUtils;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
 
import java.util.HashMap;
import java.util.Map;
 
public abstract class JsonResponse extends BaseResponse<Object> {
 
    /**
     *
     */
    private static final long serialVersionUID = -7971496621709107413L;
 
    private Map<String, Object> result = new HashMap<>(4);
 
    public JsonResponse(){
        this.setDelimiter(StringUtils.EMPTY_STRING);
    }
 
    @Override
    public TextWebSocketFrame toData() {
        result.put(WebSocketUtils.WEB_SOCKET_KEY_PROTOCOL, this.getProtocolNum());
        result.put(WebSocketUtils.WEB_SOCKET_KEY_UID, this.getName());
        this.translateProperties(result);
//        return new TextWebSocketFrame(result.toJSONString());
        try {
            return new TextWebSocketFrame(JsonUtils.objectToJsonString(result));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * 由子类实现,把业务属性转换到系统生成的Json对象中。如:</p>
     * <pre>
     * result.put("name", this.name);
     * result.put("money", this.money);
     * </pre>
     *
     * @param result
     */
    protected abstract void translateProperties(Map<String, Object> result);
 
//    @Override
//    public String getDelimiter() {
//        return StringUtils.EMPTY_STRING;
//    }
}