在 Java Swing 应用程序中实现 WebSocket
Implementing WebSocket in Java Swing Application
我创建了一个小型 java swing 应用程序,我想使用 WebSocket 将数据从服务器传输到客户端。有人可以给我一步一步的指导吗?我正在使用 JBoss 应用服务器.
一种方法是使用 Jetty WebSocket 客户端 API,希望您已经实现了服务器端 Web Socket
Maven 依赖
org.eclipse.jetty.websocket
websocket 客户端
${project.version}
package examples;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
public class SimpleEchoClient {
public static void main(String[] args) {
String destUri = "ws://echo.websocket.org";
if (args.length > 0) {
destUri = args[0];
}
WebSocketClient client = new WebSocketClient();
SimpleEchoSocket socket = new SimpleEchoSocket();
try {
client.start();
URI echoUri = new URI(destUri);
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket, echoUri, request);
System.out.printf("Connecting to : %s%n", echoUri);
socket.awaitClose(5, TimeUnit.SECONDS);
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
client.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
我创建了一个小型 java swing 应用程序,我想使用 WebSocket 将数据从服务器传输到客户端。有人可以给我一步一步的指导吗?我正在使用 JBoss 应用服务器.
一种方法是使用 Jetty WebSocket 客户端 API,希望您已经实现了服务器端 Web Socket
Maven 依赖 org.eclipse.jetty.websocket websocket 客户端 ${project.version}
package examples;
import java.net.URI;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.client.ClientUpgradeRequest;
import org.eclipse.jetty.websocket.client.WebSocketClient;
public class SimpleEchoClient {
public static void main(String[] args) {
String destUri = "ws://echo.websocket.org";
if (args.length > 0) {
destUri = args[0];
}
WebSocketClient client = new WebSocketClient();
SimpleEchoSocket socket = new SimpleEchoSocket();
try {
client.start();
URI echoUri = new URI(destUri);
ClientUpgradeRequest request = new ClientUpgradeRequest();
client.connect(socket, echoUri, request);
System.out.printf("Connecting to : %s%n", echoUri);
socket.awaitClose(5, TimeUnit.SECONDS);
} catch (Throwable t) {
t.printStackTrace();
} finally {
try {
client.stop();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}