我如何从 Java 控制台程序连接到 WebSocket?
How can i connect to a WebSocket from a Java console program?
我是 Java 的新人。我创建了一个简单的 WebSocket。
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
@ServerEndpoint("/RankixSocket")
public class RankixSocket {
@OnOpen
public void onOpen(Session session) {
try {
session.getBasicRemote().sendText("Connected");
} catch (IOException e) {
e.printStackTrace();
}
}
@OnMessage
public void onMessage(Session session, String message) {
System.out.println("New message : " + message);
try {
session.getBasicRemote().sendText("Message received -> :" + message);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void onClose() {
System.out.println("Closed");
}
}
现在我想从控制台应用程序连接到 WebSocket。我看过 example of connecting to the WebSocket
from Android Application
. In that example the author used an external library。我在谷歌上搜索了很多,但没有找到任何有用的东西:( .
- 是否可以从普通的 Java 控制台应用程序连接到
WebSocket
?那我该怎么做呢?有没有外部库?
如有任何帮助,我们将不胜感激。
回答问题 1...是!
客户端库:Jetty
示例:Stack Overflow Example 26452903
但是,我建议您重新考虑您可能正在使用的通信协议。您可以根据您正在使用的解决方案使用一些其他协议来提供最大的好处。仅供参考。
<a href="https://gist.github.com/TakahikoKawasaki/e79d36bf91bf9508ddd2" rel="nofollow noreferrer">EchoClient.java</a>
is a simple console application which connects to ws://echo.websocket.org
. The application uses nv-websocket-client library. What you should consider when selecting a WebSocket client library for Java are listed here.
我是 Java 的新人。我创建了一个简单的 WebSocket。
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
@ServerEndpoint("/RankixSocket")
public class RankixSocket {
@OnOpen
public void onOpen(Session session) {
try {
session.getBasicRemote().sendText("Connected");
} catch (IOException e) {
e.printStackTrace();
}
}
@OnMessage
public void onMessage(Session session, String message) {
System.out.println("New message : " + message);
try {
session.getBasicRemote().sendText("Message received -> :" + message);
} catch (IOException e) {
e.printStackTrace();
}
}
@OnClose
public void onClose() {
System.out.println("Closed");
}
}
现在我想从控制台应用程序连接到 WebSocket。我看过 example of connecting to the WebSocket
from Android Application
. In that example the author used an external library。我在谷歌上搜索了很多,但没有找到任何有用的东西:( .
- 是否可以从普通的 Java 控制台应用程序连接到
WebSocket
?那我该怎么做呢?有没有外部库?
如有任何帮助,我们将不胜感激。
回答问题 1...是!
客户端库:Jetty
示例:Stack Overflow Example 26452903
但是,我建议您重新考虑您可能正在使用的通信协议。您可以根据您正在使用的解决方案使用一些其他协议来提供最大的好处。仅供参考。
<a href="https://gist.github.com/TakahikoKawasaki/e79d36bf91bf9508ddd2" rel="nofollow noreferrer">EchoClient.java</a>
is a simple console application which connects to ws://echo.websocket.org
. The application uses nv-websocket-client library. What you should consider when selecting a WebSocket client library for Java are listed here.