From d2de40b544b5b069cd525143ff5a9e0d922b2c66 Mon Sep 17 00:00:00 2001
From: liujie <1793218484@qq.com>
Date: 星期五, 08 八月 2025 15:18:41 +0800
Subject: [PATCH] 我的订单顺风车
---
MessagePushTravel/src/main/java/com/sinata/push/util/applets/WebSocketHandler.java | 32 +++++++++-----------------------
1 files changed, 9 insertions(+), 23 deletions(-)
diff --git a/MessagePushTravel/src/main/java/com/sinata/push/util/applets/WebSocketHandler.java b/MessagePushTravel/src/main/java/com/sinata/push/util/applets/WebSocketHandler.java
index d4843ce..7283430 100644
--- a/MessagePushTravel/src/main/java/com/sinata/push/util/applets/WebSocketHandler.java
+++ b/MessagePushTravel/src/main/java/com/sinata/push/util/applets/WebSocketHandler.java
@@ -1,7 +1,6 @@
package com.sinata.push.util.applets;
import com.alibaba.fastjson.JSONObject;
-import com.sinata.push.util.SpringUtil;
import com.sinata.push.util.echo.Method;
import com.sinata.push.util.echo.NettyChannelMap;
import com.sinata.push.util.echo.NettyMsg;
@@ -16,7 +15,6 @@
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.util.CharsetUtil;
-import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.HashMap;
@@ -26,8 +24,6 @@
private WebSocketServerHandshaker handshaker;
private static final String WEB_SOCKET_URL = "ws://localhost:8888/websocket";
-
-// private NettyWebSocketController nettyWebSocketController = SpringUtil.getObject(NettyWebSocketController.class);
@@ -77,13 +73,8 @@
}
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
- HttpHeaders headers = req.headers();
- headers.names().forEach(name -> {
- System.out.println(name + ":" + headers.get(name));
- });
// Http解码失败,向服务器指定传输的协议为Upgrade:websocket
if(!req.getDecoderResult().isSuccess() || !("websocket").equals(req.headers().get("Upgrade"))){
- System.out.println("Http解码失败");
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.BAD_REQUEST));
return;
}
@@ -92,11 +83,9 @@
// 根据工厂类和HTTP请求创建握手类
handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
- System.out.println("不支持websocket");
// 不支持websocket
WebSocketServerHandshakerFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
} else {
- System.out.println("通过它构造握手响应消息返回给客户端");
// 通过它构造握手响应消息返回给客户端
handshaker.handshake(ctx.channel(), req);
}
@@ -123,21 +112,18 @@
//给连接的客户端返回数据
//返回心跳
-// JSONObject jsonObject = new JSONObject();
-// jsonObject.put("code", 200);
-// jsonObject.put("method", Method.ok);
-// jsonObject.put("msg", "SUCCESS");
-// jsonObject.put("data", new JSONObject());
-// TextWebSocketFrame tws = new TextWebSocketFrame(jsonObject.toJSONString());
+ JSONObject jsonObject = new JSONObject();
+ jsonObject.put("code", 200);
+ jsonObject.put("method", Method.ok);
+ jsonObject.put("msg", "SUCCESS");
+ jsonObject.put("data", new JSONObject());
+ TextWebSocketFrame tws = new TextWebSocketFrame(jsonObject.toJSONString());
// ctx.channel().writeAndFlush(tws);
- String s = NettyMsg.setMsg(Method.ok, new HashMap<String, Object>());
- ctx.writeAndFlush(Unpooled.copiedBuffer((s).getBytes()));
-
-
-// nettyWebSocketController.JudgeOperation(ctx,requestmsg);//小程序心跳处理
+
+ new NettyWebSocketController().JudgeOperation(ctx,requestmsg);//小程序心跳处理
// 群发服务端心跳响应
-// Global.group.writeAndFlush(new TextWebSocketFrame((tws).text()));
+ Global.group.writeAndFlush(new TextWebSocketFrame((tws).text()));
}
private void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
--
Gitblit v1.7.1