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



我正在尝试用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客户端非常陌生(一般来说对websocket相当陌生)。您能给予的任何帮助都将不胜感激!

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

JavaScript

ws://192.168.0.25:9000

Java ws://192.168.0.25:9000/

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

相关内容

  • 没有找到相关文章

最新更新