Java 中的简单 Websocket 客户端 - 连接问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30608841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Websocket Client in Java - Connection issue
提问by flinthart
I'm trying to write a simple websocket client in Java to connect to a 3rd party WebSocket server (I have no access to the server).
我正在尝试用 Java 编写一个简单的 websocket 客户端来连接到第 3 方 WebSocket 服务器(我无法访问该服务器)。
I can connect and communicate with the websocket server using javascript in a browser, but when I try and do the same thing using a Java client it fails to connect.
我可以在浏览器中使用 javascript 连接并与 websocket 服务器通信,但是当我尝试使用 Java 客户端执行相同的操作时,它无法连接。
With the same java client I can connect to a tomcat websocket server I have running, so I think it's some sort of compatibility issue between the client and server. (I'm using Tomcat 7.0.56 libraries for the websocket client).
使用相同的 java 客户端,我可以连接到我正在运行的 tomcat websocket 服务器,所以我认为这是客户端和服务器之间的某种兼容性问题。(我正在为 websocket 客户端使用 Tomcat 7.0.56 库)。
This is the error I get...
这是我得到的错误...
Error
错误
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();
}
}
Running the client
运行客户端
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);
}
}
I'm very new to websocket clients (and fairly new to websockets in general). Any help you can give would be appreciated!
我对 websocket 客户端很陌生(一般来说对 websockets 也很新)。您能提供的任何帮助将不胜感激!
回答by flinthart
I found the issue - it was that the ws address required a '/' on the end of it when being run from a java client.
我发现了这个问题 - 当从 Java 客户端运行时,ws 地址需要在它的末尾加上一个“/”。
JavaScriptws://192.168.0.25:9000
JavaScriptws://192.168.0.25:9000
Javaws://192.168.0.25:9000/
Javaws://192.168.0.25:9000/
Not sure why this is required in Java - but it solved the issue...
不知道为什么在 Java 中需要这样做 - 但它解决了这个问题......