NutzCN Logo
问答 websocket里如何重新onopen,onmessage等方法,原有的dome调用时没进入实现的OnMessage里面
发布于 1627天前 作者 hryc 3167 次浏览 复制 上一个帖子 下一个帖子
标签:
package cn.wizzer.app.web.commons.ext.websocket;

import org.nutz.integration.jedis.JedisAgent;
import org.nutz.integration.jedis.pubsub.PubSub;
import org.nutz.integration.jedis.pubsub.PubSubService;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.plugins.mvc.websocket.AbstractWsEndpoint;
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import org.nutz.plugins.mvc.websocket.WsHandler;
import redis.clients.jedis.Jedis;

import javax.websocket.EndpointConfig;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint(value = "/websocket", configurator = NutWsConfigurator.class)
@IocBean(create = "init") // 使用NutWsConfigurator的必备条件
public class WkWebSocket extends AbstractWsEndpoint implements PubSub {
    protected static final Log log = Logs.get();
    @Inject
    protected PubSubService pubSubService;
    @Inject
    protected JedisAgent jedisAgent;
    @Inject("refer:$ioc")
    protected Ioc ioc;

    public WsHandler createHandler(Session session, EndpointConfig config) {
        return ioc.get(WkWsHandler.class);
    }

    public void init() {
        roomPrefix = "wsroom:";
        roomProvider = new WkJedisRoomProvider(jedisAgent);
        try (Jedis jedis = jedisAgent.getResource()) {
            for (String key : jedis.keys(roomPrefix + "*")) {
                switch (jedis.type(key)) {
                    case "none":
                        break;
                    case "set":
                        break;
//                    default:
//                        jedis.del(key);
                }
            }
        }
        pubSubService.reg(roomPrefix + "*", this);
    }


    public void onMessage(String channel, String message) {
        if (log.isDebugEnabled())
            log.debugf("GET PubSub channel=%s msg=%s", channel, message);
        each(channel, (index, session, length) -> session.getAsyncRemote().sendText(message));
    }
}

websocket 连接发消息成功都进了AbstractWsEndpoint 里的方法 ,但是不进入WkWebSocket 里的onMessage方法

6 回复

自行看onMessage的方法参数,同名还得同参数才能覆写的

重写后参数一致可以了 不过重写的onOpen这个方法要怎么接收传过来的参数呢
这个是原版的写法

@ServerEndpoint("/open/api/pc/live/websocket/{uid}")
public class ApiPcLiveWebSocket {

    protected LiveCommentService liveCommentService = SpringUtil.getBean("liveCommentServiceImpl", LiveCommentService.class);
    protected AccountInfoService accountInfoService = SpringUtil.getBean("accountInfoServiceImpl", AccountInfoService.class);
    protected LiveService liveService = SpringUtil.getBean("liveServiceImpl", LiveService.class);
    protected LiveWatchService liveWatchService = SpringUtil.getBean("liveWatchServiceImpl", LiveWatchService.class);

    private static Map<String, ApiPcLiveWebSocket> clients = new ConcurrentHashMap<String, ApiPcLiveWebSocket>();
    private Session session;
    private String username;
    private String liveId;

    @OnOpen
    public void onOpen(@PathParam("uid")String uid , Session session) throws IOException {

        this.username = uid;
        this.session = session;
        if(clients.get(username) == null){
            clients.put(username, this);
        }
    }

nutzBoot版是这样的:

 @Override
    public void onOpen(Session session, EndpointConfig config) {

//        this.username = config.toString();
        this.session = session;
        if(clients.get(username) == null){
            clients.put(username, this);
        }
    }

我找到了 EndpointConfig config 这里有 pathParamMap

突然又发现EndpointConfig里的这个pathParamMap属性获取不到 @wendal

原版可以的话,用原版+configurer类就好啦

添加回复
请先登陆
回到顶部