ws://127.0.0.1:9099/ws?aaa=bbbb&bbb=ccc @RequestParam MultiValueMap reqMap 接收不到
Closed this issue · 5 comments
ws://127.0.0.1:9099/ws?aaa=bbbb&bbb=ccc @RequestParam MultiValueMap reqMap 接收不到
package com.dahuang.dragon.netty;
import com.dahuang.dragon.RedisUtil;
import com.dahuang.dragon.annotation.*;
import com.dahuang.dragon.domain.NettySession;
import io.netty.channel.ChannelId;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.timeout.IdleStateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@WebSocketEndpoint(path = "/ws",port = "9099")
@component
public class MyWebSocket {
@Autowired
private RedisUtil redisUtil;
private static final AtomicInteger OnlineCount = new AtomicInteger(0);
/**
* 链接的NettySession,需要进行心跳检测 在分布式下,记录NettySession在哪个机器上
* 在需要发送的时候通过机器IP,或者其他唯一特征进行发送即可完成分布式
*/
private final static Map<ChannelId, NettySession> sessionMap = new HashMap<>();
@BeforeHandshake
public void handshake(NettySession nettySession, HttpHeaders headers, @RequestParam String req, @RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap) {
nettySession.setSubprotocols("stomp");
if (!"ok".equals(req)) {
System.out.println("Authentication failed!");
// nettySession.close();
}
}
@OnOpen
public void onOpen(NettySession nettySession, HttpHeaders headers, @RequestParam String req,@RequestParam MultiValueMap reqMap, @PathVariable String arg, @PathVariable Map pathMap) {
System.out.println("new connection");
ChannelId channelId = nettySession.id();
//这里最好先校验下是否登录
sessionMap.put(nettySession.id(), nettySession);
//扫码连接开始同步商品信息
redisUtil.hset(String.valueOf(reqMap.get("ableCodeId")),"1000","用户信息");
for (Map.Entry<ChannelId, NettySession> channelIdNettySessionEntry : sessionMap.entrySet()) {
ChannelId channelIdTemp = channelIdNettySessionEntry.getKey();
NettySession session = channelIdNettySessionEntry.getValue();
if (!channelIdTemp.equals(channelId)) {
session.sendText("找到你了");
} else {
session.sendText("成功连接服务");
}
}
}
@OnClose
public void onClose(NettySession nettySession) {
int cnt = OnlineCount.decrementAndGet();
System.out.println("有连接关闭,当前连接数为:" + cnt);
//这里要把Session从缓存中移除
System.out.println("one connection closed");
}
@OnError
public void onError(NettySession nettySession, Throwable throwable) {
throwable.printStackTrace();
}
@OnMessage
public void onMessage(NettySession nettySession, String message) {
System.out.println(message);
nettySession.sendText("Hello Netty!");
}
@OnBinary
public void onBinary(NettySession nettySession, byte[] bytes) {
for (byte b : bytes) {
System.out.println(b);
}
nettySession.sendBinary(bytes);
}
@OnEvent
public void onEvent(NettySession nettySession, Object evt) {
if (evt instanceof IdleStateEvent) {
IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
switch (idleStateEvent.state()) {
case READER_IDLE:
System.out.println("read idle");
break;
case WRITER_IDLE:
System.out.println("write idle");
break;
case ALL_IDLE:
System.out.println("all idle");
break;
default:
break;
}
}
}
}
额 奇怪我这始终是没有 我在调试下吧 感谢老哥 我测试完给你反馈