NutzCN Logo
问答 WebSocket本地调试没问题,Jetty部署后可以连接但无法使用房间功能
发布于 2497天前 作者 FeatherZhong 2073 次浏览 复制 上一个帖子 下一个帖子
标签:

具体情况是这个web应用在本地调试没有任何问题,放在服务器里的话,虽然也可以连接的上,但是同样的请求却没有反应
比如在客户端发送指令

{"room":"22","action":"join"}

本地日志

18-01-14 10:39:29.577 DEBUG [qtp1052399527-105] session(id=oo497ncjrsio5qhc661oeullf8) join room(name=22)

服务器日志

2018-01-14 10:38:06.467:INFO:oejs.Server:main: Started @4833ms
18-01-14 10:41:53.829 DEBUG [qtp1451043227-11] Search mapping for [GET] path=/websocket : NOT Action match
18-01-14 10:41:53.861 DEBUG [qtp1451043227-11] Get 'webSocketModule'<class ycu.ktv.module.WebSocketModule>
18-01-14 10:41:53.861 DEBUG [qtp1451043227-11] Get '$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
18-01-14 10:41:53.861 DEBUG [qtp1451043227-11]   >> Load definition name=$aop_async
18-01-14 10:41:53.876 DEBUG [qtp1451043227-11] Found IocObject($aop_async) in AsyncAopIocLoader@1572394049
18-01-14 10:41:53.876 DEBUG [qtp1451043227-11]   >> Make...'$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
18-01-14 10:41:53.876 DEBUG [qtp1451043227-11] Save object '$aop_async' to [app]
18-01-14 10:41:53.876 DEBUG [qtp1451043227-11] Load AopConfigure for anno=org.nutz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
18-01-14 10:41:53.876 DEBUG [qtp1451043227-11]   >> Load definition name=webSocketModule
18-01-14 10:41:53.876 DEBUG [qtp1451043227-11] Found IocObject(webSocketModule)in AnnotationIocLoader(packages=[ycu.ktv.module])
18-01-14 10:41:53.892 DEBUG [qtp1451043227-11]   >> Make...'webSocketModule'<class ycu.ktv.module.WebSocketModule>
18-01-14 10:41:53.892 DEBUG [qtp1451043227-11] Load class ycu.ktv.module.WebSocketModule without AOP
18-01-14 10:41:53.892 DEBUG [qtp1451043227-11] Save object 'webSocketModule' to[app]

就是无论怎么发送都没反应,而且没效果

但是如果使用@onMessage注解就可以获取发送的消息

这是代码

package ycu.ktv.module;


import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Each;
import org.nutz.lang.util.NutMap;
import org.nutz.plugins.mvc.websocket.AbstractWsEndpoint;
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import org.nutz.plugins.mvc.websocket.WsHandler;
import org.nutz.plugins.mvc.websocket.handler.SimpleWsHandler;

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

// ServerEndpoint是websocket的必备注解, value是映射路径, configurator是配置类.
@ServerEndpoint(value = "/websocket", configurator = NutWsConfigurator.class)
@IocBean // 使用NutWsConfigurator的必备条件
public class WebSocketModule extends AbstractWsEndpoint {
    @Override
    public WsHandler createHandler(Session session, EndpointConfig config) {
        return new MySimpleWsHandler();
    }
    

    public class MySimpleWsHandler extends SimpleWsHandler {
        public MySimpleWsHandler() {
            super(""); // 覆盖默认前缀
        }

        public void msg2room(final NutMap req) {
            final String room = req.getString("room");
            if (room != null) {
                String _room = room;
                if (this.prefix.length() > 0 && !room.startsWith(this.prefix)) {
                    _room = this.prefix + room;
                }

                this.endpoint.each(_room, new Each<Session>() {
                    public void invoke(int index, Session ele, int length) {
                        if (!ele.getId().equals(MySimpleWsHandler.this.session.getId())) {
                            NutMap resp = new NutMap("action", "msg");
                            resp.setv("room", room);
                            resp.setv("timestamp", System.currentTimeMillis());
                            resp.setv("msg", req.get("msg"));
                            if (MySimpleWsHandler.this.nickname != null) {
                                resp.setv("nickname", MySimpleWsHandler.this.nickname);
                            }

                            MySimpleWsHandler.this.endpoint.sendJson(ele.getId(), resp);
                        }
                    }
                });
            }
        }

    }
}

------------
websocket插件使用版本 1.r.63.r5
本地jetty版本9.4.5.v20170502
服务器jetty版本9.4.5.v20170502

10 回复

覆盖一下OnMessage方法,加点日志看看

还有一种可能性是老版本的jar存在

复写了onMessage日志还是一样的效果
这是代码

package ycu.ktv.module;


import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Each;
import org.nutz.lang.util.NutMap;
import org.nutz.plugins.mvc.websocket.AbstractWsEndpoint;
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import org.nutz.plugins.mvc.websocket.WsHandler;
import org.nutz.plugins.mvc.websocket.handler.SimpleWsHandler;

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

// ServerEndpoint是websocket的必备注解, value是映射路径, configurator是配置类.
@ServerEndpoint(value = "/websocket", configurator = NutWsConfigurator.class)
@IocBean // 使用NutWsConfigurator的必备条件
public class WebSocketModule extends AbstractWsEndpoint {
    @Override
    public WsHandler createHandler(Session session, EndpointConfig config) {
        return new MySimpleWsHandler();
    }

    public class MySimpleWsHandler extends SimpleWsHandler {
        public MySimpleWsHandler() {
            super(""); // 覆盖默认前缀
        }

        @Override
        public void onMessage(String message) {
            super.onMessage(message);
            System.out.println("onMessage " + message);
        }

        public void msg2room(final NutMap req) {
            final String room = req.getString("room");
            if (room != null) {
                String _room = room;
                if (this.prefix.length() > 0 && !room.startsWith(this.prefix)) {
                    _room = this.prefix + room;
                }

                this.endpoint.each(_room, new Each<Session>() {
                    public void invoke(int index, Session ele, int length) {
                        if (!ele.getId().equals(MySimpleWsHandler.this.session.getId())) {
                            NutMap resp = new NutMap("action", "msg");
                            resp.setv("room", room);
                            resp.setv("timestamp", System.currentTimeMillis());
                            resp.setv("msg", req.get("msg"));
                            if (MySimpleWsHandler.this.nickname != null) {
                                resp.setv("nickname", MySimpleWsHandler.this.nickname);
                            }

                            MySimpleWsHandler.this.endpoint.sendJson(ele.getId(), resp);
                        }
                    }
                });
            }
        }

    }
}

这是日志

2018-01-14 11:05:37.403:INFO:oejs.Server:main: Started @4690ms
18-01-14 11:05:54.919 DEBUG [qtp1451043227-14] Search mapping for [GET] path=/we
bsocket : NOT Action match
18-01-14 11:05:54.950 DEBUG [qtp1451043227-14] Get 'webSocketModule'<class ycu.k
tv.module.WebSocketModule>
18-01-14 11:05:54.950 DEBUG [qtp1451043227-14] Get '$aop_async'<interface org.nu
tz.ioc.aop.config.AopConfigration>
18-01-14 11:05:54.950 DEBUG [qtp1451043227-14]   >> Load definition name=$aop_as
ync
18-01-14 11:05:54.950 DEBUG [qtp1451043227-14] Found IocObject($aop_async) in As
yncAopIocLoader@393469772
18-01-14 11:05:54.950 DEBUG [qtp1451043227-14]   >> Make...'$aop_async'<interfac
e org.nutz.ioc.aop.config.AopConfigration>
18-01-14 11:05:54.966 DEBUG [qtp1451043227-14] Save object '$aop_async' to [app]

18-01-14 11:05:54.966 DEBUG [qtp1451043227-14] Load AopConfigure for anno=org.nu
tz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
18-01-14 11:05:54.966 DEBUG [qtp1451043227-14]   >> Load definition name=webSock
etModule
18-01-14 11:05:54.966 DEBUG [qtp1451043227-14] Found IocObject(webSocketModule)
in AnnotationIocLoader(packages=[ycu.ktv.module])
18-01-14 11:05:54.966 DEBUG [qtp1451043227-14]   >> Make...'webSocketModule'<cla
ss ycu.ktv.module.WebSocketModule>
18-01-14 11:05:54.966 DEBUG [qtp1451043227-14] Load class ycu.ktv.module.WebSock
etModule without AOP
18-01-14 11:05:54.966 DEBUG [qtp1451043227-14] Save object 'webSocketModule' to
[app]

如果是有老版本的jar那该怎么处理呢,我这边是使用war包部署的

WebSocketModule的onMessage方法

还是和上面的日志一样没有反应
这是代码

package ycu.ktv.module;


import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Each;
import org.nutz.lang.util.NutMap;
import org.nutz.plugins.mvc.websocket.AbstractWsEndpoint;
import org.nutz.plugins.mvc.websocket.NutWsConfigurator;
import org.nutz.plugins.mvc.websocket.WsHandler;
import org.nutz.plugins.mvc.websocket.handler.SimpleWsHandler;

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

// ServerEndpoint是websocket的必备注解, value是映射路径, configurator是配置类.
@ServerEndpoint(value = "/websocket", configurator = NutWsConfigurator.class)
@IocBean // 使用NutWsConfigurator的必备条件
public class WebSocketModule extends AbstractWsEndpoint {
    @Override
    public void onMesssageString(Session session, String msg) {
        super.onMesssageString(session, msg);
        System.out.println("session" + session + "   " + "msg" + msg);
    }

    @Override
    public WsHandler createHandler(Session session, EndpointConfig config) {
        return new MySimpleWsHandler();
    }

    public class MySimpleWsHandler extends SimpleWsHandler {
        public MySimpleWsHandler() {
            super(""); // 覆盖默认前缀
        }

        public void msg2room(final NutMap req) {
            final String room = req.getString("room");
            if (room != null) {
                String _room = room;
                if (this.prefix.length() > 0 && !room.startsWith(this.prefix)) {
                    _room = this.prefix + room;
                }

                this.endpoint.each(_room, new Each<Session>() {
                    public void invoke(int index, Session ele, int length) {
                        if (!ele.getId().equals(MySimpleWsHandler.this.session.getId())) {
                            NutMap resp = new NutMap("action", "msg");
                            resp.setv("room", room);
                            resp.setv("timestamp", System.currentTimeMillis());
                            resp.setv("msg", req.get("msg"));
                            if (MySimpleWsHandler.this.nickname != null) {
                                resp.setv("nickname", MySimpleWsHandler.this.nickname);
                            }

                            MySimpleWsHandler.this.endpoint.sendJson(ele.getId(), resp);
                        }
                    }
                });
            }
        }

    }
}

本地也是war部署?

还原一下,把本地跟服务器的部署统一起来试试

刚刚测试了jetty服务器在本地可以运行,同样的文件夹复制到服务器里就不行了
最后换了tomcat服务器配置成功了,谢谢大佬!

好的, 这个websocket真的有点麻烦...

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