Helius
2021-02-24 5285d249e702eeab3ad3847aaa24523ef0f1a544
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
package com.xcong.excoin.netty.dispatch;
 
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.netty.bean.RequestBean;
import com.xcong.excoin.netty.bean.SubRequest;
import com.xcong.excoin.netty.bean.SubResponse;
import com.xcong.excoin.netty.bean.UnSubResponse;
import com.xcong.excoin.netty.common.ChannelManager;
import com.xcong.excoin.netty.common.NettyTools;
import com.xcong.excoin.netty.logic.MsgLogic;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
 
/**
 * @author wzy
 * @date 2019-05-08
 */
@Slf4j
@Component("msgDispatch")
public class MsgDispatch implements ApplicationContextAware {
 
    private ApplicationContext applicationContext;
 
    @Autowired
    private MsgLogic msgLogic;
 
    public void webSocketDispatch(ChannelHandlerContext ctx, String msg) {
        JSONObject jsonObject = JSONObject.parseObject(msg);
        if (jsonObject.containsKey("sub")) {
            String sub = jsonObject.getString("sub");
            log.info("{}", sub);
            String[] split = sub.split("\\.");
            if (split.length != 4) {
                ctx.writeAndFlush("error");
                return;
            }
 
            ChannelManager.putSymbolSubChannel(split[1], ctx.channel(), split[2]);
            SubResponse subResponse = new SubResponse();
            subResponse.setSubbed(sub);
            subResponse.setId(jsonObject.getString("id"));
            subResponse.setTs(System.currentTimeMillis());
            subResponse.setStatus("ok");
            ctx.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(subResponse)));
        } else if (jsonObject.containsKey("unsub")) {
            String sub = jsonObject.getString("unsub");
            String[] split = sub.split("\\.");
            if (split.length != 4) {
                ctx.writeAndFlush("error");
                return;
            }
 
            ChannelManager.removeSymbolUnSubChannel(split[1], ctx.channel(), split[2]);
            UnSubResponse resp = new UnSubResponse();
            resp.setSubbed(sub);
            resp.setId(jsonObject.getString("id"));
            resp.setTs(System.currentTimeMillis());
            resp.setStatus("ok");
            ctx.writeAndFlush(resp);
        }
    }
 
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}