Java 中的简单 Websocket 客户端 - 连接问题
Simple Websocket Client in Java - Connection issue
我正在尝试在 Java 中编写一个简单的 websocket 客户端以连接到第 3 方 WebSocket 服务器(我无法访问该服务器)。
我可以在浏览器中使用 java 脚本连接并与 websocket 服务器通信,但是当我尝试使用 Java 客户端做同样的事情时,它无法连接。
使用相同的 java 客户端,我可以连接到 tomcat 我有 运行 的 websocket 服务器,所以我认为这是客户端和服务器之间的某种兼容性问题。 (我正在为 websocket 客户端使用 Tomcat 7.0.56 库)。
这是我得到的错误...
错误
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 相当陌生)。如果您能提供任何帮助,我们将不胜感激!
我发现了这个问题 - 当来自 java 客户端的 运行 时,ws 地址需要在其末尾添加一个“/”。
Java脚本
ws://192.168.0.25:9000
Java
ws://192.168.0.25:9000/
不确定为什么 Java 中需要这样做 - 但它解决了问题...
我正在尝试在 Java 中编写一个简单的 websocket 客户端以连接到第 3 方 WebSocket 服务器(我无法访问该服务器)。
我可以在浏览器中使用 java 脚本连接并与 websocket 服务器通信,但是当我尝试使用 Java 客户端做同样的事情时,它无法连接。
使用相同的 java 客户端,我可以连接到 tomcat 我有 运行 的 websocket 服务器,所以我认为这是客户端和服务器之间的某种兼容性问题。 (我正在为 websocket 客户端使用 Tomcat 7.0.56 库)。
这是我得到的错误...
错误
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 相当陌生)。如果您能提供任何帮助,我们将不胜感激!
我发现了这个问题 - 当来自 java 客户端的 运行 时,ws 地址需要在其末尾添加一个“/”。
Java脚本 ws://192.168.0.25:9000
Java ws://192.168.0.25:9000/
不确定为什么 Java 中需要这样做 - 但它解决了问题...