一尘不染

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

tomcat

我试图用Java编写一个简单的websocket客户端,以连接到第3方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

WebSocket客户端

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客户端非常陌生(一般来说对websocket相当陌生)。您能提供的任何帮助将不胜感激!


阅读 1280

收藏
2020-06-16

共1个答案

一尘不染

我发现了问题-从Java客户端运行时,ws地址的末尾需要一个’/’。

JavaScript ws://192.168.0.25:9000

Java ws://192.168.0.25:9000 /

不知道为什么在Java中需要这样做-但它解决了这个问题…

2020-06-16