NutzCN Logo
问答 怎么对接webSocket
发布于 2477天前 作者 wx_8868jgbos3h956ejd54e 2109 次浏览 复制 上一个帖子 下一个帖子
标签:

需要通过webSocket实现一个收付款确认功能,消费者与商家需要建立长连接,但是由于第一次接触,对文档中的webSocket介绍不是很理解,请问谁有nutz实现webSocket的案例可以分享一下,非常感谢

19 回复

先按文档走一走

var socket = new WebSocket('ws://192.168.80.254:8080/SocketService/websocket');
我连接是后台报错,提示
根据错误提示应该是Ioc获取为空

java.lang.NullPointerException
	at com.wq.module.NutWsConfigurator.getEndpointInstance(NutWsConfigurator.java:22)
	at org.apache.tomcat.websocket.pojo.PojoEndpointServer.onOpen(PojoEndpointServer.java:44)
	at org.apache.tomcat.websocket.server.WsHttpUpgradeHandler.init(WsHttpUpgradeHandler.java:133)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:745)

tomcat什么版本?

web.xml里面没加 NutFilter?

添加了

<filter>
    <filter-name>nutz</filter-name>
    <filter-class>org.nutz.mvc.NutFilter</filter-class>
    <init-param>
      <param-name>modules</param-name>
      <param-value>com.wq.MainModule</param-value>
    </init-param>
  </filter>

换tomcat 8.5最新版吧

是不是你的com.wq.MainModule连@IocBy都没有的??

是不是缺少配置,这是代码

package com.wq;

import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.SetupBy;
import org.nutz.mvc.ioc.provider.ComboIocProvider;

@SetupBy(value=MainSetup.class)
@IocBy(type=ComboIocProvider.class, args={"*js", "ioc/",
        "*anno", "com.wq",
        "*tx"})
@Modules(scanPackage=true)
public class MainModule {
	
	
}

启动日志发一下看看

应该总能拿到ioc容器才对, debug看看

        Ioc ioc = Mvcs.getIoc();
        if (ioc == null)
            ioc = Mvcs.ctx().getDefaultIoc();

debugger了,依然是null

启动时候, 有没有nutz的日志先出来

总会有点日志吧???

涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Server version:        Apache Tomcat/8.5.28
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Server built:          Feb 6 2018 23:10:25 UTC
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Server number:         8.5.28.0
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: OS Name:               Mac OS X
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: OS Version:            10.12
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Architecture:          x86_64
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Java Home:             /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: JVM Version:           1.8.0_112-b16
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: JVM Vendor:            Oracle Corporation
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: CATALINA_BASE:         /tools/apache-tomcat-8.5.28
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: CATALINA_HOME:         /tools/apache-tomcat-8.5.28
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Command line argument: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:65308
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Command line argument: -Dcatalina.home=/tools/apache-tomcat-8.5.28
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Command line argument: -Dcatalina.base=/tools/apache-tomcat-8.5.28
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Command line argument: -Djava.endorsed.dirs=/tools/apache-tomcat-8.5.28/common/endorsed
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Command line argument: -Djava.io.tmpdir=/tools/apache-tomcat-8.5.28/temp
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Command line argument: -Djava.library.path=/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:/tools/apache-tomcat-8.5.28/bin
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.startup.VersionLoggerListener log
淇℃??: Command line argument: -Dsun.io.useCanonCaches=false
涓???? 13, 2018 4:12:24 涓???? org.apache.catalina.core.AprLifecycleListener lifecycleEvent
淇℃??: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin:/tools/apache-tomcat-8.5.28/bin]
涓???? 13, 2018 4:12:24 涓???? org.apache.coyote.AbstractProtocol init
淇℃??: Initializing ProtocolHandler ["http-nio-8080"]
涓???? 13, 2018 4:12:24 涓???? org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
淇℃??: Using a shared selector for servlet write/read
涓???? 13, 2018 4:12:24 涓???? org.apache.coyote.AbstractProtocol init
淇℃??: Initializing ProtocolHandler ["ajp-nio-8009"]
涓???? 13, 2018 4:12:25 涓???? org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
淇℃??: Using a shared selector for servlet write/read
涓???? 13, 2018 4:12:25 涓???? org.apache.catalina.startup.Catalina load
淇℃??: Initialization processed in 1341 ms
涓???? 13, 2018 4:12:25 涓???? org.apache.catalina.core.StandardService startInternal
淇℃??: Starting service [Catalina]
涓???? 13, 2018 4:12:25 涓???? org.apache.catalina.core.StandardEngine startInternal
淇℃??: Starting Servlet Engine: Apache Tomcat/8.5.28
涓???? 13, 2018 4:12:25 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deploying web application directory [/tools/apache-tomcat-8.5.28/webapps/docs]
涓???? 13, 2018 4:12:25 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deployment of web application directory [/tools/apache-tomcat-8.5.28/webapps/docs] has finished in [633] ms
涓???? 13, 2018 4:12:25 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deploying web application directory [/tools/apache-tomcat-8.5.28/webapps/examples]
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.core.ApplicationContext log
淇℃??: ContextListener: contextInitialized()
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.core.ApplicationContext log
淇℃??: SessionListener: contextInitialized()
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.core.ApplicationContext log
淇℃??: ContextListener: attributeAdded('StockTicker', 'async.Stockticker@77cd3b1d')
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deployment of web application directory [/tools/apache-tomcat-8.5.28/webapps/examples] has finished in [692] ms
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deploying web application directory [/tools/apache-tomcat-8.5.28/webapps/host-manager]
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deployment of web application directory [/tools/apache-tomcat-8.5.28/webapps/host-manager] has finished in [99] ms
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deploying web application directory [/tools/apache-tomcat-8.5.28/webapps/manager]
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deployment of web application directory [/tools/apache-tomcat-8.5.28/webapps/manager] has finished in [47] ms
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deploying web application directory [/tools/apache-tomcat-8.5.28/webapps/ROOT]
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deployment of web application directory [/tools/apache-tomcat-8.5.28/webapps/ROOT] has finished in [69] ms
涓???? 13, 2018 4:12:26 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deploying web application directory [/tools/apache-tomcat-8.5.28/webapps/SocketService]
涓???? 13, 2018 4:12:28 涓???? org.apache.jasper.servlet.TldScanner scanJars
淇℃??: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
涓???? 13, 2018 4:12:28 涓???? com.sun.faces.config.ConfigureListener contextInitialized
淇℃??: ???濮????涓?涓???? '/SocketService' ??? Mojarra 2.0.3 (FCS b03)
涓???? 13, 2018 4:12:29 涓???? com.sun.faces.spi.InjectionProviderFactory createInstance
淇℃??: JSF1048锛???? PostConstruct/PreDestroy 娉ㄩ???????????杩?浜?娉ㄩ????? ManagedBeans ??规??灏?琛ㄧず娉ㄩ??宸插????????
涓???? 13, 2018 4:12:30 涓???? org.apache.catalina.startup.HostConfig deployDirectory
淇℃??: Deployment of web application directory [/tools/apache-tomcat-8.5.28/webapps/SocketService] has finished in [3,491] ms
涓???? 13, 2018 4:12:30 涓???? org.apache.coyote.AbstractProtocol start
淇℃??: Starting ProtocolHandler ["http-nio-8080"]
涓???? 13, 2018 4:12:30 涓???? org.apache.coyote.AbstractProtocol start
淇℃??: Starting ProtocolHandler ["ajp-nio-8009"]
涓???? 13, 2018 4:12:30 涓???? org.apache.catalina.startup.Catalina start
淇℃??: Server startup in 5186 ms

额, 你不是在eclipse/idea/netbeans里面启动的???

我这是myeclipse,就是在工具中启动tomcat的

那日志呢... 你配了log4j??? 跑到日志文件里面了???

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