Java中的简单Websocket客户端 - 连接问题

时间:2015-06-03 00:28:05

标签: java tomcat websocket

我正在尝试用Java编写一个简单的websocket客户端来连接第三方WebSocket服务器(我无法访问服务器)。

我可以在浏览器中使用javascript连接并与websocket服务器通信,但是当我尝试使用Java客户端做同样的事情时,它无法连接。

使用相同的Java客户端,我可以连接到我运行的tomcat websocket服务器,所以我认为它是客户端和服务器之间的某种兼容性问题。 (我正在使用Tomcat 7.0.56库作为websocket客户端)。

这是我得到的错误......

错误

javax.websocket.DeploymentException: The HTTP request to initiate the WebSocket connection failed
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:344)
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:166)
    at com.cvr.instant.ws.WebSocketClient.Connect(WebSocketClient.java:44)
    at com.cvr.instant.ws.TestWS.Start(TestWS.java:17)
    at com.cvr.instant.ws.TestWS.main(TestWS.java:37)
Caused by: java.io.EOFException
    at org.apache.tomcat.websocket.WsWebSocketContainer.processResponse(WsWebSocketContainer.java:595)
    at org.apache.tomcat.websocket.WsWebSocketContainer.connectToServer(WsWebSocketContainer.java:317)
... 4 more

WebSocketClient

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;

@ClientEndpoint
public class WebSocketClient {

    protected   WebSocketContainer container;
    protected   Session userSession = null;

    public WebSocketClient() {
        container = ContainerProvider.getWebSocketContainer();
    }

    public void Connect(String sServer) {

          try {
              userSession = container.connectToServer(this, new URI(sServer));

            } catch (DeploymentException | URISyntaxException | IOException e) {
                e.printStackTrace();
            }

    }

    public void SendMessage(String sMsg) throws IOException {
        userSession.getBasicRemote().sendText(sMsg);
    }

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("Connected");
    }

    @OnClose
    public void onClose(Session session, CloseReason closeReason) {
    }

    @OnMessage
    public void onMessage(Session session, String msg) {
        System.out.println(msg);
    }

    public void Disconnect() throws IOException {
        userSession.close();
    }
}

运行客户端

import java.io.IOException;

public class TestWS {

    public WebSocketClient wsc;
    public TestWS() {
    }

    public void Start() throws InterruptedException, IOException {

        wsc = new WebSocketClient();
        wsc.callback = this;
        wsc.Connect("ws://192.168.0.25:9000");
        Thread.sleep(1000);
        wsc.Disconnect();
    }


    public static void main(String[] args) throws InterruptedException, IOException 
    {
        TestWS t = new TestWS();
        t.Start();
        Thread.sleep(1000);
    }

}    

我对websocket客户端非常陌生(一般来说对于websockets也很新)。如果您有任何帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:2)

我发现了问题 - 这是ws地址要求' /'从Java客户端运行时,它就结束了。

<强>的JavaScript WS://192.168.0.25:9000

<强>爪哇 WS://192.168.0.25:9000 /

不确定为什么在Java中需要它 - 但它解决了这个问题......