puzhibing
2022-08-22 1421f8e9c308c4741cb396309035009393b5b036
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.sinata.zuul.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 {
    public static void main(String[] args) {
        Map<String,Object> map = new HashMap<String, Object>();
        //用户信息
        map.put("imgUrl","1");
        map.put("nickName","1");
        map.put("licensePlate","1");
        map.put("phone",1);
        map.put("driverId",1);
        map.put("carColor","1");
        map.put("modelName","1");
        map.put("brandName","1");
        map.put("driverOrderNums",1);
        map.put("score",1);
        
        
        map.put("id",1);
        map.put("orderNum", "123456");
        map.put("startAddress", "测试");
        map.put("endAddress", "测试1");
        map.put("departureTime", 1533608196000L);
        map.put("type",1);
        map.put("mileage",1);
        map.put("mileageMoney",10);
        map.put("duration",10);
        map.put("durationMoney",10);
        map.put("nightMoney",1);
        map.put("serverMoney",1);
        map.put("nightMileage",10);
        map.put("longMileage",10);
        map.put("longDurationMoney",10);
        map.put("orderMoney",10);
        map.put("payMoney",10);
        map.put("couponsMoney",10);
        System.out.println(setMsg(Method.ping, new HashMap<String, Object>()));
    }
    
    /**
     * 返回一个正确数据
     * 
     * @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);
            }
        }
    }
 
 
}