Giter VIP home page Giter VIP logo

netty-websocket-spring-boot-starter's Issues

ws://127.0.0.1:9099/ws?aaa=bbbb&bbb=ccc @RequestParam MultiValueMap reqMap 接收不到

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;
        }
    }
}

}

多个入参如何使用

`@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();
}
System.out.println(arg);
// onlineSessionMap.put()
}

@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");
    System.out.println(req);
}`

这两个方法中@RequestParam String req 和 @PathVariable String arg 在什么情况下能收到值,我测试无论单个参数 还是多个参数都没有值

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.