Tomcat WebSocket 连接关闭,代码:1006 问题
Posted
技术标签:
【中文标题】Tomcat WebSocket 连接关闭,代码:1006 问题【英文标题】:Tomcat WebSocket connection closed, Code: 1006 issue 【发布时间】:2015-01-21 14:55:16 【问题描述】:我正在尝试使用 websocket 的 Tomcat 服务器。这就是我所做的: 我创建了 3 个 java 文件,从 Tomcat 示例中复制。请看下面的代码。 然后我构建了一个war文件并将其放入webapps中。但后来我收到了这个错误信息: 信息:WebSocket 连接关闭,代码:1006
我是否错过了在 Tomcat 上创建 websocket 的任何步骤?
谢谢。
1. ExamplesConfig.java
import java.util.HashSet;
import java.util.Set;
import javax.websocket.Endpoint;
import javax.websocket.server.ServerApplicationConfig;
import javax.websocket.server.ServerEndpointConfig;
public class ExamplesConfig implements ServerApplicationConfig
@Override
public Set<ServerEndpointConfig> getEndpointConfigs(Set<Class<? extends Endpoint>> scanned)
Set<ServerEndpointConfig> result = new HashSet<ServerEndpointConfig>();
System.out.println("ExamplesConfig ==========> getEndpointConfigs");
if (scanned.contains(EchoEndpoint.class))
result.add(ServerEndpointConfig.Builder.create(EchoEndpoint.class, "/websocket/echoProgrammatic").build());
return result;
@Override
public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned)
// Deploy all WebSocket endpoints defined by annotations in the examples
// web application. Filter out all others to avoid issues when running
// tests on Gump
Set<Class<?>> results = new HashSet<Class<?>>();
for (Class<?> clazz : scanned)
if (clazz.getPackage().getName().startsWith("websocket."))
System.out.println("getAnnotatedEndpointClasses ===========>" + clazz);
results.add(clazz);
return results;
2. EchoEndpoint.java
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.RemoteEndpoint;
import javax.websocket.Session;
public class EchoEndpoint extends Endpoint
@Override
public void onOpen(Session session, EndpointConfig endpointConfig)
RemoteEndpoint.Basic remoteEndpointBasic = session.getBasicRemote();
session.addMessageHandler(new EchoMessageHandlerText(remoteEndpointBasic));
session.addMessageHandler(new EchoMessageHandlerBinary(remoteEndpointBasic));
private static class EchoMessageHandlerText
implements MessageHandler.Partial<String>
private final RemoteEndpoint.Basic remoteEndpointBasic;
private EchoMessageHandlerText(RemoteEndpoint.Basic remoteEndpointBasic)
this.remoteEndpointBasic = remoteEndpointBasic;
@Override
public void onMessage(String message, boolean last)
try
if (remoteEndpointBasic != null)
remoteEndpointBasic.sendText(message, last);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
private static class EchoMessageHandlerBinary
implements MessageHandler.Partial<ByteBuffer>
private final RemoteEndpoint.Basic remoteEndpointBasic;
private EchoMessageHandlerBinary(RemoteEndpoint.Basic remoteEndpointBasic)
this.remoteEndpointBasic = remoteEndpointBasic;
@Override
public void onMessage(ByteBuffer message, boolean last)
try
if (remoteEndpointBasic != null)
remoteEndpointBasic.sendBinary(message, last);
catch (IOException e)
// TODO Auto-generated catch block
e.printStackTrace();
3. EchoAnnotation.java
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.websocket.OnMessage;
import javax.websocket.PongMessage;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/websocket/echoAnnotation")
public class EchoAnnotation
@OnMessage
public void echoTextMessage(Session session, String msg, boolean last)
try
if (session.isOpen())
System.out.println("==========> this is my code");
session.getBasicRemote().sendText(msg, last);
catch (IOException e)
try
session.close();
catch (IOException e1)
// Ignore
@OnMessage
public void echoBinaryMessage(Session session, ByteBuffer bb,
boolean last)
try
if (session.isOpen())
session.getBasicRemote().sendBinary(bb, last);
catch (IOException e)
try
session.close();
catch (IOException e1)
// Ignore
/**
* Process a received pong. This is a NO-OP.
*
* @param pm Ignored.
*/
@OnMessage
public void echoPongMessage(PongMessage pm)
// NO-OP
【问题讨论】:
您能告诉我们您使用的是哪个 JVM 吗?你能显示你的 web.xml 文件吗? 【参考方案1】:如果您正在运行 JVM 版本 1.7 并且您的 web.xml 使用 ,则此示例适用于 Tomcat 7.0.x >Servlet 规范版本 3.0 根据Tomcat documentation。
您的 web.xml 文件应如下所示:
<web-app version="3.0" ... >
【讨论】:
以上是关于Tomcat WebSocket 连接关闭,代码:1006 问题的主要内容,如果未能解决你的问题,请参考以下文章