在Tomcat 7中使用Web套接字 [英] Using web socket with Tomcat 7

查看:151
本文介绍了在Tomcat 7中使用Web套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Web套接字,我开始尝试使用tomcat API。我目前正在使用Tomcat 7.0.37。

I would like to use web socket, and I started looking to do so with tomcat API. I'm currently using Tomcat 7.0.37.

我试图关注tomcat中的示例代码文档

I tried to follow the example code from tomcat documentation.

我也在为项目使用maven,所以我把它添加到我的 pom.xml

I'm also using maven for the project, so I added this to my pom.xml :

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-catalina</artifactId>
    <version>7.0.39</version>
    <scope>provided</scope>
</dependency>

第一件事很奇怪,我在Eclipse中收到此行的错误消息:

First thing that's weird, I get an error message in Eclipse for this line :

private class MyMessageInbound extends MessageInbound

错误消息是 MyMessageInbound类型的层次结构不一致,但我仍然可以编译并启动webapp。

The error message is "The hierarchy of the type MyMessageInbound is inconsistent", but I'm still able to compile and launch the webapp.

我还在 web.xml 中添加了配置如下:

I also added the configuration in the web.xml as follow:

<servlet>
    <servlet-name>wsChat</servlet-name>
    <servlet-class>websocket.chat.ChatWebSocketServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>wsChat</servlet-name>
    <url-pattern>/websocket/chat</url-pattern>
</servlet-mapping>

所以,我可以启动webapp,但是当我进入聊天页面时,我在聊天板上收到错误消息 Info:WebSocket已关闭。。 Tomcat日志给我以下消息:

So, I'm able to launch the webapp, but when I go to the chat page, I get an error on the chat board saying "Info: WebSocket closed.". Tomcat log give me the following message:

SEVERE: Servlet.service() for servlet [wsChat] in context with path [/websocket-tuto] threw exception [L''exécution de la servlet a lancé une exception] with root cause
java.lang.NoSuchMethodError: websocket.chat.ChatWebSocketServlet$MyMessageInbound.<init>(Lwebsocket/chat/ChatWebSocketServlet;Luwebsocket/chat/ChatWebSocketServlet$MyMessageInbound;)V
at websocket.chat.ChatWebSocketServlet.createWebSocketInbound(ChatWebSocketServlet.java:34)
at org.apache.catalina.websocket.WebSocketServlet.doGet(WebSocketServlet.java:121)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

我认为这里唯一缺少的信息是聊天页面

I think the only missing information here is the source of the chat page.

任何人都能看到我做错了什么?

Anyone see what I'm doing wrong ??

谢谢

推荐答案

我解决了这个问题!

我试图在命令行上用Maven打包项目,然后得到此错误:

I tried to package the project with Maven on command line, and get this error:

[ERROR] class file for org.apache.coyote.http11.upgrade.UpgradeInbound not found

从这里,我刚刚将以下内容添加到 pom.xml

From this, I just added the following to the pom.xml:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-coyote</artifactId>
    <version>7.0.39</version>
    <scope>provided</scope>
</dependency>

我希望这可以帮助其他人解决同样的问题。

I hope this will help others with the same issue.

这篇关于在Tomcat 7中使用Web套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆