liujie
2023-10-17 083bed1be51dc21792a245b035b50dfef0a24db3
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cn.mb.cloud.gateway.util.echo;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
 
public class NettyMsg {
 
    /**
     * 返回一个正确数据
     * 
     * @param method
     * @param data
     * @return
     * @author TaoNingBo
     */
    public static String setMsg(String method, Map<String, Object> data) {
        StringBuffer json = new StringBuffer();
        json.append(getHeader(200, "SUCCESS", method));
        json.append(JSON.toJSONString(data));
        json.append("}");
        //return JSON.toJSONString(json);
        return json.toString();
    }
    
    /**
     * 返回一个正确数据
     * 
     * @param method
     * @param data
     * @return
     */
    public static String setMsg(String method, List<Map<String, Object>> data) {
        StringBuffer json = new StringBuffer();
        json.append(getHeader(200, "SUCCESS", method));
        List<JSONObject> jsonList = new ArrayList<JSONObject>();
        for(Map<String, Object> map : data) {
            JSONObject dataJson = new JSONObject(map);
            jsonList.add(dataJson);
        }
        json.append(jsonList);
        json.append("}");
        
//        return JSON.toJSONString(json);
        return json.toString();
    }
    
    /**
     * 返回一个错误数据
     * 
     * @param method
     * @param data
     * @return
     * @author TaoNingBo
     */
    public static String setErrMsg(String method, String data) {
        StringBuffer json = new StringBuffer();
        json.append(getHeader(-1, "FAILURE", method));
        json.append("\"" + data + "\"");
        json.append("}");
//        return JSON.toJSONString(json);
        return json.toString();
    }
    
    /**
     * 生成一个返回JSON的头
     * 
     * @param code
     * @param msg
     * @param method
     * @return
     * @author TaoNingBo
     */
    private static String getHeader(int code, String msg, String method) {
        StringBuffer header = new StringBuffer();
        header.append("{");
        header.append("\"code\":\"" + code);
        header.append("\",\"msg\":\"" + msg);
        header.append("\",\"method\":\"" + method);
        header.append("\",\"data\":");
        return header.toString();
    }
    
    /**
     * 发送消息给客户端
     * 
     * @param cacheType
     * @param id
     * @param method
     * @param data
     * @author TaoNingBo
     */
    public static void sendMsg(String cacheType, Integer id, String method, Map<String, Object> data) {
        //NettyServerController.sendMsgToClient(NettyChannelMap.getData(cacheType + id), setMsg(method, data));
        NettyServerController.sendMsgToClient(cacheType,id, setMsg(method, data));
    }
 
 
    public static void resendMsg(String token){
        String msg = NettyServerController.table.get(token);
        ChannelHandlerContext ctx = NettyChannelMap.getData(token);
        if(null != msg && !"".equals(msg) && ctx != null && ctx.channel().isActive()){
            ByteBuf buffer = Unpooled.copiedBuffer((msg).getBytes());
            ChannelFuture sync;
            try {
                sync = ctx.writeAndFlush(buffer).sync();
                System.err.println("重发异常推送状态"+sync.isSuccess()+",位置:"+token+",消息内容:"+msg);
                if(!sync.isSuccess()){
                    resendMsg(token);
                    System.err.println("重发异常推送不成功,将继续推送"+msg);
                }
                NettyServerController.table.remove(token);
            } catch (Exception e) {
                resendMsg(token);
                System.err.println("重发推送发生异常,记录:"+msg);
            }
        }
    }
 
 
}