pom.xml
@@ -218,11 +218,11 @@ </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>${netty.version}</version> </dependency> <!-- <dependency>--> <!-- <groupId>io.netty</groupId>--> <!-- <artifactId>netty-all</artifactId>--> <!-- <version>${netty.version}</version>--> <!-- </dependency>--> <!--<dependency>--> <!--<groupId>com.squareup.okhttp3</groupId>--> src/main/java/com/xcong/excoin/ExcoinApplication.java
@@ -1,6 +1,12 @@ package com.xcong.excoin; import com.xcong.excoin.netty.ChatServer; import com.xcong.excoin.netty.server.WebSocketServer; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; @@ -15,10 +21,17 @@ @EnableAsync @SpringBootApplication @MapperScan("com.xcong.excoin.modules.*.dao") public class ExcoinApplication { public class ExcoinApplication implements CommandLineRunner { @Autowired private WebSocketServer webSocketServer; public static void main(String[] args) { SpringApplication.run(ExcoinApplication.class, args); } @Override public void run(String... args) throws Exception { // webSocketServer.start(); } } src/main/java/com/xcong/excoin/common/aop/ExceptionCatchAspect.java
@@ -87,7 +87,7 @@ } else { msg = exStr; } DingTalkUtils.sendMsg(profiles + "--" + ex.getMessage(), msg, exceptionData.getId()); DingTalkUtils.sendMsg(projectName + "-" +profiles + "--" + ex.getMessage(), msg, exceptionData.getId()); } catch (Exception e) { log.error("exception aop", e); } src/main/java/com/xcong/excoin/modules/otc/entity/OtcMsgHistory.java
New file @@ -0,0 +1,23 @@ package com.xcong.excoin.modules.otc.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.xcong.excoin.common.system.base.BaseEntity; import lombok.Data; @Data @TableName("otc_msg_history") public class OtcMsgHistory extends BaseEntity { private Long memberId; private Long targetId; private String msg; /** * 消息类型 1-文本 2-图片 */ private Integer msgType; } src/main/java/com/xcong/excoin/modules/otc/entity/OtcMsgUserList.java
New file @@ -0,0 +1,26 @@ package com.xcong.excoin.modules.otc.entity; import com.baomidou.mybatisplus.annotation.TableName; import com.xcong.excoin.common.system.base.BaseEntity; import lombok.Data; import java.util.Date; @Data @TableName("otc_msg_user_list") public class OtcMsgUserList extends BaseEntity { private Long memberId; private Long targetId; /** * 是否已读 1-是 2-否 */ private Integer isRead; /** * 最后聊天时间 */ private Date lastMsgTime; } src/main/java/com/xcong/excoin/netty/bean/AuthRequest.java
New file @@ -0,0 +1,29 @@ package com.xcong.excoin.netty.bean; import java.io.Serializable; public class AuthRequest implements Serializable { private static final long serialVersionUID = 5557983797622950620L; private Integer cmd; private String token; public Integer getCmd() { return cmd; } public void setCmd(Integer cmd) { this.cmd = cmd; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } } src/main/java/com/xcong/excoin/netty/bean/ChatRequest.java
@@ -14,6 +14,16 @@ private String content; private Integer cmd; public Integer getCmd() { return cmd; } public void setCmd(Integer cmd) { this.cmd = cmd; } public String getTo() { return to; } src/main/java/com/xcong/excoin/netty/bean/RequestBean.java
@@ -3,88 +3,31 @@ import java.io.Serializable; /** * @author wzy * @email wangdoubleone@gmail.com * @date 2019-05-09 */ public class RequestBean implements Serializable { private static final long serialVersionUID = 1L; /** * 请求类型 * 请求类型 1-鉴权 2-发送消息 */ private String type; private Integer type; /** * 当前通道id */ private String channelId; private Object data; /** * web端通道ID */ private String reqId; public Object getData() { return data; } /** * 请求参数 */ private String params; public void setData(Object data) { this.data = data; } /** * 手持端是否同意连接 0-否 1-是 */ private String tag; public String getType() { public Integer getType() { return type; } public void setType(String type) { public void setType(Integer type) { this.type = type; } public String getChannelId() { return channelId; } public void setChannelId(String channelId) { this.channelId = channelId; } public String getParams() { return params; } public void setParams(String params) { this.params = params; } public String getReqId() { return reqId; } public void setReqId(String reqId) { this.reqId = reqId; } public String getTag() { return tag; } public void setTag(String tag) { this.tag = tag; } @Override public String toString() { return "RequestBean{" + "type='" + type + '\'' + ", channelId='" + channelId + '\'' + ", reqId='" + reqId + '\'' + ", params='" + params + '\'' + ", tag='" + tag + '\'' + '}'; } } src/main/java/com/xcong/excoin/netty/bean/ResponseBean.java
@@ -5,10 +5,7 @@ import java.util.List; import java.util.Map; /** * @author wzy * @date 2020-04-18 16:17 **/ public class ResponseBean implements Serializable { private static final long serialVersionUID = 1L; src/main/java/com/xcong/excoin/netty/common/ChannelManager.java
@@ -28,16 +28,31 @@ CHANNEL_MAP.put(channel.id().asShortText(), channel.id()); } public static void addWsChannel(Channel channel, Long memberId) { WEBSOCKET_GROUP.add(channel); CHANNEL_MAP.put(memberId.toString(), channel.id()); } public static void removeWebSocketChannel(Channel channel) { WEBSOCKET_GROUP.remove(channel); CHANNEL_MAP.remove(channel.id().asShortText()); } public static void removeWsChannel(Channel channel, Long memberId) { WEBSOCKET_GROUP.remove(channel); CHANNEL_MAP.remove(memberId.toString()); } public static Channel findWebSocketChannel(String id){ ChannelId channelId = CHANNEL_MAP.get(id); return WEBSOCKET_GROUP.find(channelId); } public static Channel findWsChannel(Long id){ ChannelId channelId = CHANNEL_MAP.get(id.toString()); return WEBSOCKET_GROUP.find(channelId); } public static ChannelGroup getWebSocketGroup() { return WEBSOCKET_GROUP; } src/main/java/com/xcong/excoin/netty/dispatch/MsgDispatch.java
@@ -27,10 +27,9 @@ private MsgLogic msgLogic; public void webSocketDispatch(ChannelHandlerContext ctx, String msg) { log.info("==========={}", 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); src/main/java/com/xcong/excoin/netty/logic/WebSocketLogic.java
@@ -18,24 +18,4 @@ public class WebSocketLogic { public void webReqConnection(RequestBean requestBean) { Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId()); channel.writeAndFlush(NettyTools.webSocketBytes("this is ok")); } public void reqHomeSymbols(RequestBean requestBean) { String params = requestBean.getParams(); JSONObject jsonObject = JSONObject.parseObject(params); String token = jsonObject.getString("token"); String type = jsonObject.getString("type"); ResponseBean responseBean = ResponseBean.ok(requestBean.getType(), null); Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId()); channel.writeAndFlush(NettyTools.webSocketBytes(JSONObject.toJSONString(responseBean))); } public void defaultReq(RequestBean requestBean) { Channel channel = ChannelManager.findWebSocketChannel(requestBean.getChannelId()); channel.writeAndFlush("this is error type"); } } src/main/java/com/xcong/excoin/netty/logic/impl/MsgLogicImpl.java
@@ -20,16 +20,16 @@ @Override public void webSocketMsgLogic(RequestBean requestBean) { switch (requestBean.getType()) { case Contans.WEB_REQ_CONNECTION : webSocketLogic.webReqConnection(requestBean); break; case Contans.HOME_SYMBOLS: webSocketLogic.reqHomeSymbols(requestBean); default: webSocketLogic.defaultReq(requestBean); break; } // switch (requestBean.getType()) { // case Contans.WEB_REQ_CONNECTION : // webSocketLogic.webReqConnection(requestBean); // break; // case Contans.HOME_SYMBOLS: // webSocketLogic.reqHomeSymbols(requestBean); // default: // webSocketLogic.defaultReq(requestBean); // break; // } } src/main/java/com/xcong/excoin/netty/server/WebSocketServer.java
@@ -19,7 +19,6 @@ @Component("webSocketServer") public class WebSocketServer implements ChatServer { private EventLoopGroup boss = new NioEventLoopGroup(); private EventLoopGroup work = new NioEventLoopGroup(); src/main/resources/application-app.yml
@@ -101,7 +101,7 @@ other-job: true loop-job: false rabbit-consumer: false block-job: false block-job: true otc-job: true system: name: otc