NutzCN Logo
问答 使用Websocket插件,客户端连接时服务端报空指针异常,且无返回信息。
发布于 2538天前 作者 FeatherZhong 2499 次浏览 复制 上一个帖子 下一个帖子
标签:

这是异样日志

[INFO] Started Jetty Server
18-01-10 19:51:13.392 DEBUG [qtp1300576734-16] Search mapping for [GET] path=/websockets : NOT Action match
[WARNING] /websockets
java.lang.NullPointerException
	at org.nutz.plugins.mvc.websocket.NutWsConfigurator.getEndpointInstance(NutWsConfigurator.java:22)
	at org.eclipse.jetty.websocket.jsr356.server.JsrCreator.createWebSocket(JsrCreator.java:156)
	at org.eclipse.jetty.websocket.server.WebSocketServerFactory.acceptWebSocket(WebSocketServerFactory.java:209)
	at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:238)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637)
	at org.nutz.mvc.NutFilter.nextChain(NutFilter.java:230)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:205)
	at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1629)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
	at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:219)
	at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
	at org.eclipse.jetty.server.Server.handle(Server.java:564)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
	at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
	at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
	at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
	at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
	at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
	at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:126)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:672)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:590)
	at java.lang.Thread.run(Thread.java:745)

这是源代码

package ycu.ktv.module;


import org.nutz.ioc.loader.annotation.IocBean;
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.Session;
import javax.websocket.server.ServerEndpoint;

// ServerEndpoint是websocket的必备注解, value是映射路径, configurator是配置类.
@ServerEndpoint(value = "/websockets", configurator=NutWsConfigurator.class)
@IocBean // 使用NutWsConfigurator的必备条件
public class WebSocketModule extends AbstractWsEndpoint {

}

看了一下日志感觉是Ioc报的什么问题,是我Ioc没有配置好吗?

9 回复

MainModule没配@IocBy??

谢谢,的确是Ioc的问题,然后我试了一下连接依然没有返回服务端提示

Search mapping for [GET] path=/websockets : NOT Action match

请问有什么办法能够打印出客户端请求发送的内容来调试呢?

覆盖onMesssageString方法

谢谢大佬现在复写了onMesssage方法,但是依然还是没有触发onMessage或者onActionError方法,这是咋回事呢
这是日志

[INFO] Started Jetty Server
18-01-10 22:28:10.886 DEBUG [qtp1300576734-14] Search mapping for [GET] path=/websockets : NOT Action match
18-01-10 22:28:10.899 DEBUG [qtp1300576734-14] Get 'webSocketModule'<class ycu.ktv.module.WebSocketModule>
18-01-10 22:28:10.899 DEBUG [qtp1300576734-14] Get '$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
18-01-10 22:28:10.899 DEBUG [qtp1300576734-14] 	 >> Load definition name=$aop_async
18-01-10 22:28:10.899 DEBUG [qtp1300576734-14] Found IocObject($aop_async) in AsyncAopIocLoader@522323334
18-01-10 22:28:10.899 DEBUG [qtp1300576734-14] 	 >> Make...'$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
18-01-10 22:28:10.899 DEBUG [qtp1300576734-14] Save object '$aop_async' to [app] 
18-01-10 22:28:10.902 DEBUG [qtp1300576734-14] Load AopConfigure for anno=org.nutz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
18-01-10 22:28:10.903 DEBUG [qtp1300576734-14] 	 >> Load definition name=webSocketModule
18-01-10 22:28:10.903 DEBUG [qtp1300576734-14] Found IocObject(webSocketModule) in AnnotationIocLoader(packages=[ycu.ktv.module])
18-01-10 22:28:10.903 DEBUG [qtp1300576734-14] 	 >> Make...'webSocketModule'<class ycu.ktv.module.WebSocketModule>
18-01-10 22:28:10.903 DEBUG [qtp1300576734-14] Load class ycu.ktv.module.WebSocketModule without AOP
18-01-10 22:28:10.903 DEBUG [qtp1300576734-14] Save object 'webSocketModule' to [app] 

这是代码

package ycu.ktv.module;


import org.nutz.ioc.loader.annotation.IocBean;
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.Session;
import javax.websocket.server.ServerEndpoint;

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


}

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


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

    @Override
    public void onActionError(NutMap msg, Throwable e) {
        super.onActionError(msg, e);
        System.out.println("onError");
    }
}

用什么来连的???

插件版本是多少, jetty版本是多少?

谢谢大佬,使用的是Android客户端和Web页配合测试连接 插件版本1.r.63 jetty版本9.4.5.v20170502
目前有新的问题在程序中使用@onMessage注解可以打印出客户端发送的内容但是无论使不使用注解都不能有房间的效果
这是使用@onMessage注解的日志

[INFO] Started Jetty Server
18-01-10 22:53:50.538 DEBUG [qtp1300576734-20] Search mapping for [GET] path=/websockets : NOT Action match
18-01-10 22:53:50.545 DEBUG [qtp1300576734-20] Get 'webSocketModule'<class ycu.ktv.module.WebSocketModule>
18-01-10 22:53:50.546 DEBUG [qtp1300576734-20] Get '$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
18-01-10 22:53:50.546 DEBUG [qtp1300576734-20] 	 >> Load definition name=$aop_async
18-01-10 22:53:50.546 DEBUG [qtp1300576734-20] Found IocObject($aop_async) in AsyncAopIocLoader@834025596
18-01-10 22:53:50.546 DEBUG [qtp1300576734-20] 	 >> Make...'$aop_async'<interface org.nutz.ioc.aop.config.AopConfigration>
18-01-10 22:53:50.546 DEBUG [qtp1300576734-20] Save object '$aop_async' to [app] 
18-01-10 22:53:50.547 DEBUG [qtp1300576734-20] Load AopConfigure for anno=org.nutz.ioc.aop.Aop by type=org.nutz.ioc.aop.config.impl.AnnotationAopConfigration
18-01-10 22:53:50.547 DEBUG [qtp1300576734-20] 	 >> Load definition name=webSocketModule
18-01-10 22:53:50.547 DEBUG [qtp1300576734-20] Found IocObject(webSocketModule) in AnnotationIocLoader(packages=[ycu.ktv.module])
18-01-10 22:53:50.547 DEBUG [qtp1300576734-20] 	 >> Make...'webSocketModule'<class ycu.ktv.module.WebSocketModule>
18-01-10 22:53:50.548 DEBUG [qtp1300576734-20] Load class ycu.ktv.module.WebSocketModule without AOP
18-01-10 22:53:50.548 DEBUG [qtp1300576734-20] Save object 'webSocketModule' to [app] 
{"room":"home","action":"join"}
18-01-10 22:53:57.686 DEBUG [qtp1300576734-15] Search mapping for [GET] path=/websockets : NOT Action match
18-01-10 22:53:57.686 DEBUG [qtp1300576734-15] Get 'webSocketModule'<class ycu.ktv.module.WebSocketModule>
{"room":"home","action":"join"}
{"room":"home","action":"msg2room","msg" : "大家好!!"}

这是代码

package ycu.ktv.module;


import org.nutz.ioc.loader.annotation.IocBean;
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 = "/websockets", configurator = NutWsConfigurator.class)
@IocBean // 使用NutWsConfigurator的必备条件
public class WebSocketModule extends AbstractWsEndpoint {


    @OnMessage
    public void print(String message){
        System.out.println(message);
    }

}

用最新的1.r.63.r5 试试,不需要加@OnMessage

可以了可以了,谢谢大佬,谢谢大佬,谢谢大佬,谢谢大佬。

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