我试图在Java中创建一个简单的websocket客户端。我有一个使用springboot的嵌入式tomcat 8.0.3服务器。客户端正在尝试连接到另一台运行tomcat 7.0.53的机器上的服务器。然而,在尝试建立websocket后,我得到以下错误:
2014-07-02 11:13:56.861 INFO 13656 --- [nio-8085-exec-1] o.s.w.s.c.WebSocketConnectionManager : Connecting to WebSocket at ws://54.213.202.109:8880/wspoc/ws/10
2014-07-02 11:14:01.885 ERROR 13656 --- [nio-8085-exec-1] o.s.w.s.c.WebSocketConnectionManager : Failed to connect
javax.websocket.DeploymentException: The HTTP request to initiate the WebSocket connection failed
at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:351)
at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:125)
at org.springframework.web.socket.client.standard.StandardWebSocketClient$1.call(StandardWebSocketClient.java:122)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at org.springframework.web.socket.client.standard.StandardWebSocketClient.doHandshakeInternal(StandardWebSocketClient.java:135)
at org.springframework.web.socket.client.AbstractWebSocketClient.doHandshake(AbstractWebSocketClient.java:100)
at org.springframework.web.socket.client.WebSocketConnectionManager.openConnection(WebSocketConnectionManager.java:139)
at org.springframework.web.socket.client.ConnectionManagerSupport.startInternal(ConnectionManagerSupport.java:129)
at org.springframework.web.socket.client.WebSocketConnectionManager.startInternal(WebSocketConnectionManager.java:122)
at org.springframework.web.socket.client.ConnectionManagerSupport.start(ConnectionManagerSupport.java:118)
at com.hp.fucms.impl.TopoRestController.test(TopoRestController.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:676)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
这是我的客户机代码
WebSocketClient client = new StandardWebSocketClient();
TopoWebSocketHandler handler = new TopoWebSocketHandler();
String pathId = "ws://54.213.202.109:8880/wspoc/ws/10";
WebSocketConnectionManager manager = new WebSocketConnectionManager(client, handler,pathId);
manager.start();
和我的handler
类:
public class TopoWebSocketHandler extends AbstractWebSocketHandler {private WebSocketSession session;
@Override
public void handleTextMessage(WebSocketSession session,
TextMessage message) throws Exception{
System.out.println("Message "+message.getPayload());
}
@Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception{
session.sendMessage(new TextMessage("Hello"));
}
}
对于websockets
使用embedded server
和standalone server
是否存在问题?
我们将其解决为代理问题,该问题似乎与嵌入式服务器/独立服务器连接的性质无关。