NutzCN Logo
问答 nutz-plugins-websocket无法调试
发布于 380天前 作者 SayingCode 595 次浏览 复制 上一个帖子 下一个帖子
标签:

按照git上的步骤在nutzwk里面集成了websocket插件;
集成方法如下:
1. 引入pom节点

org.nutz
nutz-plugins-websocket
1.r.60.r4

我的nutz主版本是 1.r.58 不会有问题吧??

2.实现自己的service类
@ServerEndpoint(value = "/websocket", configurator=NutWsConfigurator.class)
@IocBean
public class WebsocketService extends AbstractWsEndpoint {
}
3.在前段调用js初始化
websocket = new WebSocket("ws://localhost:8080/websocket");
websocket.onopen = function () {
setMessageInnerHTML("WebSocket连接成功");
}

目前遇到的问题:
web能够建立连接,同事输出日志"WebSocket连接成功";但是我在代码中
继承父类的方法
public void onOpen(Session session, EndpointConfig config) {
super.onOpen(session, config);
System.out.println("xxxxxxxxxxx 111111 ");
}
public void onClose(Session session, CloseReason closeReason) {
super.onClose(session, closeReason);
System.out.println("xxxxxxxxxxx 2222222 ");
}
....
里面所有的方法均未打印出日志,感觉是没进这些方法根本没有调用啊,不知道到底是启动还是没有启动,感觉是启动了,可是难道没有进入这些方法在后台启动的?

附日志:

[2017-04-11 03:44:46,587] Artifact CheKu:war exploded: Deploy took 12,061 milliseconds
2017-04-11 15:46:23,858 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>
2017-04-11 15:46:23,858 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=webSocketController
2017-04-11 15:46:23,858 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(webSocketController) in AnnotationIocLoader(packages=[cn.wizzer, com.renxin])
2017-04-11 15:46:23,859 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>
2017-04-11 15:46:23,860 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.renxin.modules.controllers.open.message.WebSocketController without AOP
2017-04-11 15:46:23,861 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'webSocketController' to [app]
xxxxxxxxxxxxuuuuu 0, 0, org.nutz.plugins.mvc.websocket.room.MemoryRoomProvider@1f556426
2017-04-11 15:46:53,168 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>

每次重新web端重新建立连接都会输出
2017-04-11 15:46:53,168 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>

bug没有看到我的 System.out.println("xxxxxxxxxxx 111111 "); 日志,好诡异。。。

16 回复

tomcat什么版本?

部署在根了??? 不然ws的地址起码有项目名称吧?

嗯 部署在ROOT下的

tomcat应该是可以的,因为本来是实现了一个websocket的,用的类没有继承 Endpoint 体系,而是直接在方法中
\@OnOpen
\@OnError
\@OnClose
这些注解来做的

websocket与nutz的版本号基本没关系.

噢, 是不是你的WebSocketController类占用了 /websocket这个路径哦?

没有哦,我又测试一个一次;

@ServerEndpoint(value = "/websocket_v2", configurator = NutWsConfigurator.class)
@IocBean
public class WebSocketController extends AbstractWsEndpoint {
    private final static Log log = Logs.get();

    public WebSocketController() {
        log.debug("对象初始化");
    }

    @Override
    public void onOpen(Session session, EndpointConfig config) {
        super.onOpen(session, config);
        log.debug("测试点一; session :" + session + ", config:" + config);
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        super.onClose(session, closeReason);
        log.debug("测试点二; session :" + session + ", closeReason:" + closeReason);
    }

    @Override
    public void onError(Session session, Throwable throwable) {
        super.onError(session, throwable);
        log.debug("测试点三; session :" + session + ", throwable:" + throwable);
    }
}

web端测试代码

//将消息显示在网页上
    function createWebSocket() {
        //判断当前浏览器是否支持WebSocket
        if ('WebSocket' in window) {
            websocket = new WebSocket("ws://localhost:8080/websocket_v2");
        }
        else {
            alert('当前浏览器 Not support websocket')
        }

        //连接发生错误的回调方法
        websocket.onerror = function () {
            setMessageInnerHTML("WebSocket连接发生错误");
        };

        //连接成功建立的回调方法
        websocket.onopen = function () {
            setMessageInnerHTML("WebSocket连接成功");
        }

        //接收到消息的回调方法
        websocket.onmessage = function (event) {
            setMessageInnerHTML(event.data);
        }

        //连接关闭的回调方法
        websocket.onclose = function () {
            setMessageInnerHTML("WebSocket连接关闭");
        }
    }

web端建立连接后提示信息
“WebSocket连接成功”
没有提示任何错误

然后tomcat的日志:

[2017-04-11 04:47:16,071] Artifact CheKu:war: Artifact is deployed successfully
[2017-04-11 04:47:16,071] Artifact CheKu:war: Deploy took 13,526 milliseconds
2017-04-11 16:47:28,520 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>
2017-04-11 16:47:28,521 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - >> Load definition name=webSocketController
2017-04-11 16:47:28,521 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:169) DEBUG - Found IocObject(webSocketController) in AnnotationIocLoader(packages=[cn.wizzer, com.renxin])
2017-04-11 16:47:28,521 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - >> Make...'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>
2017-04-11 16:47:28,522 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.renxin.modules.controllers.open.message.WebSocketController without AOP
2017-04-11 16:47:28,522 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'webSocketController' to [app]
2017-04-11 16:47:28,524 com.renxin.modules.controllers.open.message.WebSocketController.(WebSocketController.java:23) DEBUG - 对象初始化

我又执行了两次关闭连接,两次连接socket;感觉关闭tomcat就没日志输出;点击建立连接的时候会有日志输出;
2017-04-11 16:47:28,524 com.renxin.modules.controllers.open.message.WebSocketController.(WebSocketController.java:23) DEBUG - 对象初始化
2017-04-11 16:51:48,315 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>
2017-04-11 16:51:52,850 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>

额, WebsocketService 名字改了??

嗯 我改了个名字;
是不是这个问题哦?没有写aop的什么注解?
2017-04-11 16:47:28,522 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.renxin.modules.controllers.open.message.WebSocketController without AOP

跟aop没关系

要不把他删了,看看啥变化

看来多少和ioc有些关系,出错了。。

[2017-04-11 05:00:07,626] Artifact CheKu:war: Artifact is deployed successfully
[2017-04-11 05:00:07,626] Artifact CheKu:war: Deploy took 13,011 milliseconds
2017-04-11 17:00:14,035 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'webSocketController'<class com.renxin.modules.controllers.open.message.WebSocketController>
2017-04-11 17:00:14,035 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=webSocketController
2017-04-11 17:00:14.036:WARN:oejs.ServletHandler:qtp1749186397-25: /websocket_v2
org.nutz.ioc.IocException: [webSocketController] # For object [webSocketController] - type:[class com.renxin.modules.controllers.open.message.WebSocketController]
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:218)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	at org.nutz.plugins.mvc.websocket.NutWsConfigurator.getEndpointInstance(NutWsConfigurator.java:19)
	at org.eclipse.jetty.websocket.jsr356.server.JsrCreator.createWebSocket(JsrCreator.java:153)
	at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:166)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
	at org.eclipse.jetty.server.Server.handle(Server.java:499)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
	at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
	at java.lang.Thread.run(Thread.java:745)
Caused by: 
org.nutz.ioc.ObjectLoadException: Object 'webSocketController' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:174)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:180)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:239)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:146)
	at org.nutz.plugins.mvc.websocket.NutWsConfigurator.getEndpointInstance(NutWsConfigurator.java:19)
	at org.eclipse.jetty.websocket.jsr356.server.JsrCreator.createWebSocket(JsrCreator.java:153)
	at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:166)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1652)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:585)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
	at org.eclipse.jetty.server.Server.handle(Server.java:499)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
	at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
	at java.lang.Thread.run(Thread.java:745)

好神奇哦, 类都删, 为啥还能找到这个类名呢??

哦,我没删这个类,只是把上面的@iocbean注释了

... 那证明是找到这个类,但为啥不走呢... 加上@IocBean和@OnOpen试试

可以了,果然是要加上@onopen这些注解才可以;

但是这里加上注解有个编译的红色下波浪线,提示信息
A class extending javax.websocket.Endpoint cannot have @OnOpen methods less... (⌘F1)
@ServerEndpoint errors

或许把这些注解写到插件的父类?有点意思。。

额, 我之前测jetty和tomcat的时候, 貌似见过, 最后继承Endpoint解决了...

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