NutzCN Logo
问答 Nutz 和 Websocket 怎么跑起来?
发布于 2791天前 作者 qq_b21b7504 2330 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

参考了下nutz的过往 demo, 在 MainSetup 下

	ServerContainer sc = (ServerContainer) config.getServletContext().getAttribute(ServerContainer.class.getName());
			if (sc != null)
				try {
					sc.addEndpoint(MyWebsocket.class);
				} catch (Exception e) {
					e.printStackTrace();
				}

报错,java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer
各位,如何解决?

20 回复

赞!! 有新方式了

按照最新的配置

ws = new WebSocket("ws://"+WS_URL);   //这一行

F12 报错
WebSocket connection to 'ws://localhost:8001/sms%20/websocket' failed: Error during WebSocket handshake: Unexpected response code: 404

使用 websocket 在线测试 网页 连接 报错

org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) DEBUG - Search mapping for [GET] path=/websocket : NOT Action match

请求被MVC拦截了?

tomcat8才行的

@wendal 大神 确确实实在tomcat8跑得呀 Nutz的包 用的是1.r.60 nutz-plugins-websocket 使用的是 1.r.61.r2 这会有影响吗?

http://nutzam.com/core/mvc/websocket.html 按上面的做, 并不需要 sc.addEndpoint

@wendal 嗯 我确实是按照文档上面来的呀 只配置了 MyWebsocket 这一个类 其他的都清了 还是报错 404

启动日志发一下

@wendal 请指教 = =!

2017-05-02 17:00:05,592 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [GET] path=/ : IndexAction.index(IndexAction.java:44)
2017-05-02 17:00:05,597 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'indexAction'<class com.bsdkj.sms.modules.mvc.IndexAction>
2017-05-02 17:00:05,597 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=indexAction
2017-05-02 17:00:05,598 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(indexAction) in AnnotationIocLoader(packages=[com.bsdkj.sms])
2017-05-02 17:00:05,598 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'indexAction'<class com.bsdkj.sms.modules.mvc.IndexAction>
2017-05-02 17:00:05,605 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'indexAction' to [app] 
2017-05-02 17:00:05,610 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'userService'<class com.bsdkj.sms.modules.service.sys.UserService>
2017-05-02 17:00:05,611 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=userService
2017-05-02 17:00:05,611 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(userService) in AnnotationIocLoader(packages=[com.bsdkj.sms])
2017-05-02 17:00:05,611 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'userService'<class com.bsdkj.sms.modules.service.sys.UserService>
2017-05-02 17:00:05,612 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
2017-05-02 17:00:05,613 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
2017-05-02 17:00:05,614 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
2017-05-02 17:00:05,614 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'txREAD_COMMITTED'<interface org.nutz.aop.MethodInterceptor>
2017-05-02 17:00:05,617 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'userService' to [app] 
2017-05-02 17:00:05,617 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'dao'<>
2017-05-02 17:00:05,618 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'dao'<>
2017-05-02 17:00:05,628 org.nutz.service.EntityService.<init>(EntityService.java:41) DEBUG - Get TypeParams for self : com.bsdkj.sms.common.bean.sys.SysUser
2017-05-02 17:00:05,635 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) DEBUG - SELECT * FROM sys_menu  WHERE href=? LIMIT 0, 1 
    | 1 |
    |---|
    | / |
  For example:> "SELECT * FROM sys_menu  WHERE href='/' LIMIT 0, 1 "
2017-05-02 17:00:05,640 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'permission'<class com.bsdkj.sms.common.shiro.velocity.Permission>
2017-05-02 17:00:05,640 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=permission
2017-05-02 17:00:05,640 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(permission) in AnnotationIocLoader(packages=[com.bsdkj.sms])
2017-05-02 17:00:05,641 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'permission'<class com.bsdkj.sms.common.shiro.velocity.Permission>
2017-05-02 17:00:05,641 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.bsdkj.sms.common.shiro.velocity.Permission without AOP
2017-05-02 17:00:05,641 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'permission' to [app] 
2017-05-02 17:00:05,654 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'cacheUtils'<class com.bsdkj.sms.common.util.CacheUtils>
2017-05-02 17:00:05,654 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=cacheUtils
2017-05-02 17:00:05,654 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(cacheUtils) in AnnotationIocLoader(packages=[com.bsdkj.sms])
2017-05-02 17:00:05,655 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'cacheUtils'<class com.bsdkj.sms.common.util.CacheUtils>
2017-05-02 17:00:05,655 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.bsdkj.sms.common.util.CacheUtils without AOP
2017-05-02 17:00:05,655 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'cacheUtils' to [app] 
2017-05-02 17:00:05,661 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'toolkit'<class com.bsdkj.sms.common.util.Toolkit>
2017-05-02 17:00:05,661 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=toolkit
2017-05-02 17:00:05,661 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(toolkit) in AnnotationIocLoader(packages=[com.bsdkj.sms])
2017-05-02 17:00:05,661 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'toolkit'<class com.bsdkj.sms.common.util.Toolkit>
2017-05-02 17:00:05,662 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.bsdkj.sms.common.util.Toolkit without AOP
2017-05-02 17:00:05,662 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'toolkit' to [app] 
2017-05-02 17:00:05,677 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'dateUtils'<class com.bsdkj.sms.common.util.DateUtils>
2017-05-02 17:00:05,677 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=dateUtils
2017-05-02 17:00:05,677 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(dateUtils) in AnnotationIocLoader(packages=[com.bsdkj.sms])
2017-05-02 17:00:05,677 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'dateUtils'<class com.bsdkj.sms.common.util.DateUtils>
2017-05-02 17:00:05,678 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.bsdkj.sms.common.util.DateUtils without AOP
2017-05-02 17:00:05,678 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'dateUtils' to [app] 
2017-05-02 17:00:05,689 org.apache.velocity.runtime.log.SimpleLog4JLogSystem.logVelocityMessage(SimpleLog4JLogSystem.java:155) DEBUG - ResourceManager : found /WEB-INF/template/private/index.html with loader org.apache.velocity.runtime.resource.loader.FileResourceLoader
2017-05-02 17:00:05,708 org.apache.velocity.runtime.log.SimpleLog4JLogSystem.logVelocityMessage(SimpleLog4JLogSystem.java:155) DEBUG - Right side ($!{shiro.getPrincipalProperty('loginTheme')}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /WEB-INF/template/common/globals_sys.html[line 55, column 73]
2017-05-02 17:00:05,709 org.apache.velocity.runtime.log.SimpleLog4JLogSystem.logVelocityMessage(SimpleLog4JLogSystem.java:155) DEBUG - Right side ($!{shiro.getPrincipalProperty('loginTheme')}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /WEB-INF/template/common/globals_sys.html[line 62, column 73]
2017-05-02 17:00:05,709 org.apache.velocity.runtime.log.SimpleLog4JLogSystem.logVelocityMessage(SimpleLog4JLogSystem.java:155) DEBUG - Right side ($!{shiro.getPrincipalProperty('loginTheme')}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /WEB-INF/template/common/globals_sys.html[line 69, column 74]
2017-05-02 17:00:05,830 org.apache.velocity.runtime.log.SimpleLog4JLogSystem.logVelocityMessage(SimpleLog4JLogSystem.java:155) DEBUG - Right side ($!{shiro.getPrincipalProperty('loginTheme')}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /WEB-INF/template/common/globals_sys.html[line 55, column 73]
2017-05-02 17:00:05,831 org.apache.velocity.runtime.log.SimpleLog4JLogSystem.logVelocityMessage(SimpleLog4JLogSystem.java:155) DEBUG - Right side ($!{shiro.getPrincipalProperty('loginTheme')}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /WEB-INF/template/common/globals_sys.html[line 62, column 73]
2017-05-02 17:00:05,831 org.apache.velocity.runtime.log.SimpleLog4JLogSystem.logVelocityMessage(SimpleLog4JLogSystem.java:155) DEBUG - Right side ($!{shiro.getPrincipalProperty('loginTheme')}) of '==' operation has null value. If it is a reference, it may not be in the context or its toString() returned null. /WEB-INF/template/common/globals_sys.html[line 69, column 74]
2017-05-02 17:00:05,846 com.bsdkj.sms.common.mvc.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [GET ]URI=/sms/ 251ms
2017-05-02 17:00:06,046 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) DEBUG - Found mapping for [GET] path=/private/avatar : LoginAction.avatar(LoginAction.java:152)
2017-05-02 17:00:06,047 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'loginAction'<class com.bsdkj.sms.modules.mvc.LoginAction>
2017-05-02 17:00:06,047 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) DEBUG - 	 >> Load definition name=loginAction
2017-05-02 17:00:06,047 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) DEBUG - Found IocObject(loginAction) in AnnotationIocLoader(packages=[com.bsdkj.sms])
2017-05-02 17:00:06,047 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) DEBUG - 	 >> Make...'loginAction'<class com.bsdkj.sms.modules.mvc.LoginAction>
2017-05-02 17:00:06,048 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) DEBUG - Load class com.bsdkj.sms.modules.mvc.LoginAction without AOP
2017-05-02 17:00:06,049 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) DEBUG - Save object 'loginAction' to [app] 
2017-05-02 17:00:06,056 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'userService'<class com.bsdkj.sms.modules.service.sys.UserService>
2017-05-02 17:00:06,056 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) DEBUG - Get 'sysLogService'<class com.bsdkj.sms.common.service.log.SysLogService>
2017-05-02 17:00:06,058 org.nutz.mvc.view.RawView.render(RawView.java:116) DEBUG - File downloading ... D:\Tomcat8\wtpwebapps\sms\include\img\man.png
2017-05-02 17:00:06,059 com.bsdkj.sms.common.mvc.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) DEBUG - [GET ]URI=/sms/private/avatar 13ms
2017-05-02 17:00:06,265 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) DEBUG - Search mapping for [GET] path=/websocket : NOT Action match

难道没扫描到??

	                ServerContainer sc = (ServerContainer) config.getServletContext().getAttribute(ServerContainer.class.getName());
			if (sc != null)
				try {
					sc.addEndpoint(MyWebsocket.class);
				} catch (Exception e) {
					e.printStackTrace();
				}

看看报啥

用的是maven吗? pom.xml贴一下看看

@wendal MainSetUp 添加

还是报错 转换异常

java.lang.ClassCastException: org.apache.tomcat.websocket.server.WsServerContainer cannot be cast to javax.websocket.server.ServerContainer

@wendal pom 奉上

<dependencies>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>javax.websocket</groupId>
			<artifactId>javax.websocket-api</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.6.3</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib-nodep</artifactId>
			<version>3.2.0</version>
		</dependency>
		<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>nutz</artifactId>
			<version>${nutz-version}</version>
		</dependency>
		 <dependency>
            <groupId>org.nutz</groupId>
            <artifactId>nutz-plugins-websocket</artifactId>
            <version>1.r.61.r2</version>
        </dependency>
		<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>nutz-integration-quartz</artifactId>
			<version>${nutz-version}</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz</artifactId>
			<version>2.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.quartz-scheduler</groupId>
			<artifactId>quartz-jobs</artifactId>
			<version>2.2.3</version>
		</dependency>
		<dependency>
			<groupId>org.nutz</groupId>
			<artifactId>nutz-integration-shiro</artifactId>
			<version>${nutz-version}</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.shiro</groupId>
					<artifactId>shiro-cor</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.apache.shiro</groupId>
					<artifactId>shiro-web</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-all</artifactId>
			<version>1.3.2</version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.shiro</groupId>
					<artifactId>shiro-guice</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- 处理JavaBeans -->
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils-core</artifactId>
			<version>1.8.3</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.25</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.25</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.8.1</version>
		</dependency>
		<!-- 验证码工具 -->
		<dependency>
			<groupId>cn.apiclub.tool</groupId>
			<artifactId>simplecaptcha</artifactId>
			<version>1.2.2</version>
		</dependency>
		<!-- 阿里巴巴连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.29</version>
			<exclusions>
				<exclusion>
					<groupId>com.alibaba</groupId>
					<artifactId>jconsole</artifactId>
				</exclusion>
				<exclusion>
					<groupId>com.alibaba</groupId>
					<artifactId>tools</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- JDBC驱动 -->
		<dependency>
			<groupId>net.sourceforge.jtds</groupId>
			<artifactId>jtds</artifactId>
			<version>1.3.1</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.39</version>
		</dependency>
		<!-- 二维码工具 -->
		<dependency>
			<groupId>com.google.zxing</groupId>
			<artifactId>javase</artifactId>
			<version>3.2.1</version>
		</dependency>
		<!-- 进程内缓存框架 -->
		<dependency>
			<groupId>net.sf.ehcache</groupId>
			<artifactId>ehcache</artifactId>
			<version>2.10.3</version>
		</dependency>
		<!-- 邮件工具 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-email</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>com.sun.mail</groupId>
			<artifactId>javax.mail</artifactId>
			<version>1.5.2</version>
		</dependency>
		<!-- java 工具类 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.5</version>
		</dependency>
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity</artifactId>
			<version>1.7</version>
		</dependency>
		<!-- 社交化登录类库 -->
		<dependency>
			<groupId>org.brickred</groupId>
			<artifactId>socialauth</artifactId>
			<version>4.14</version>
			<exclusions>
				<exclusion>
					<groupId>javax.servlet</groupId>
					<artifactId>servlet-api</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.json</groupId>
					<artifactId>json</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.openid4java</groupId>
					<artifactId>openid4java-consumer</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- EXCEL工具类 -->
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.15</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.15</version>
		</dependency>
		<!-- 汉字转拼音 -->
		<dependency>
			<groupId>com.belerweb</groupId>
			<artifactId>pinyin4j</artifactId>
			<version>2.5.1</version>
		</dependency>
	</dependencies>

然后是 启动日志里 , MyWebsocket启动了

2017-05-02 17:19:02,563 org.nutz.ioc.loader.annotation.AnnotationIocLoader.addClass(AnnotationIocLoader.java:74) DEBUG - Found @IocBean : class com.bsdkj.sms.modules.service.Message.MyWebsocket

加scope

		<dependency>
			<groupId>javax.websocket</groupId>
			<artifactId>javax.websocket-api</artifactId>
			<version>1.1</version>
                        <scope>provided</scope>
		</dependency>

@qq_b21b7504 另外,那不是启动,那是ioc的日志

@wendal 跑起来了
websocket这个类 ioc 注入了 根据文档 不就代表websocket能建立连接了吗?

@qq_b21b7504 不是哦,那只是提供ioc支持的日志

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