From 9e6054ca0612a05ec2b698b8a63db7605c600725 Mon Sep 17 00:00:00 2001 From: Helius <wangdoubleone@gmail.com> Date: Fri, 05 Mar 2021 14:51:08 +0800 Subject: [PATCH] modify --- src/main/java/com/xcong/excoin/netty/dispatch/MsgDispatch.java | 44 +++++++++++++++++++++++++++++++++++--------- 1 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/xcong/excoin/netty/dispatch/MsgDispatch.java b/src/main/java/com/xcong/excoin/netty/dispatch/MsgDispatch.java index 540fd36..3241e3c 100644 --- a/src/main/java/com/xcong/excoin/netty/dispatch/MsgDispatch.java +++ b/src/main/java/com/xcong/excoin/netty/dispatch/MsgDispatch.java @@ -1,7 +1,9 @@ package com.xcong.excoin.netty.dispatch; import com.alibaba.fastjson.JSONObject; -import com.xcong.excoin.netty.bean.RequestBean; +import com.xcong.excoin.netty.bean.SubResponse; +import com.xcong.excoin.netty.bean.UnSubResponse; +import com.xcong.excoin.netty.common.ServerChannelManager; import com.xcong.excoin.netty.common.NettyTools; import com.xcong.excoin.netty.logic.MsgLogic; import io.netty.channel.ChannelHandlerContext; @@ -27,14 +29,38 @@ private MsgLogic msgLogic; public void webSocketDispatch(ChannelHandlerContext ctx, String msg) { - RequestBean requestBean = null; - try { - requestBean = JSONObject.parseObject(msg, RequestBean.class); - requestBean.setChannelId(ctx.channel().id().asShortText()); - msgLogic.webSocketMsgLogic(requestBean); - } catch (Exception e) { - log.info("#websocket json error:{}#", e); - ctx.channel().writeAndFlush(NettyTools.webSocketBytes("params error")); + 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; + } + + ServerChannelManager.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; + } + + ServerChannelManager.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); } } -- Gitblit v1.9.1