Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
WebSocketEchoClient.java - WebSocket Echo Client
How to write a simple client program to connect to the WebSocket Echo Server?
✍: FYIcenter.com
I you want to write a simple client program to connect to the WebSocket Echo Server
running at wss://echo.websocket.org,
you can follow these suggestions:
1. Write a test Java class annotated as @ClientEndpoint:
@ClientEndpoint
public class WebSocketEchoClient {
2. Create a Session instance with the WebSocket Echo Server:
WebSocketContainer c = ContainerProvider.getWebSocketContainer();
URI u = new URI("wss://echo.websocket.org");
Session s = c.connectToServer(WebSocketClassInfo.class, u);
3. Send a text message to the server:
RemoteEndpoint.Basic r = s.getBasicRemote();
r.sendText("Hello there!");
4. Put the thread to sleep for sometime for the server to prcess the message:
Thread.sleep(1000*2);
s.close();
5. Write a method annotated as @OnMessage to receive the message echoed back from the server:
@OnMessage
public void OnMessage(String message) {
System.out.println("Message received: "+message);
}
Here is the entir test program, WebSocketClassInfo.java:
// Copyright (c) 2016 FYIcenter.com
import java.net.URI;
import javax.websocket.ClientEndpoint;
import javax.websocket.OnMessage;
import javax.websocket.ContainerProvider;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import javax.websocket.*;
@ClientEndpoint
public class WebSocketEchoClient {
public static void main(String[] args) throws Exception {
WebSocketContainer c = ContainerProvider.getWebSocketContainer();
URI u = new URI("wss://echo.websocket.org");
Session s = c.connectToServer(WebSocketEchoClient.class, u);
System.out.println("WebSocket session info:");
System.out.println(" Protocol version: "+s.getProtocolVersion());
System.out.println(" Request URI: "+s.getRequestURI());
RemoteEndpoint.Basic r = s.getBasicRemote();
r.sendText("Hello there!");
Thread.sleep(1000*2);
s.close();
}
@OnMessage
public void OnMessage(String message) {
System.out.println("Message received: "+message);
}
}
You can compile and run the above example in a command window with Java SE JDK 8 and the tyrus-standalone-client.jar from Java EE 7 SDK:
>\fyicenter\jdk-1.8.0\bin\javac -cp \fyicenter\glassfish4\mq\lib\tyrus-standalone-client.jar WebSocketEchoClient.java >\fyicenter\jdk-1.8.0\bin\java -cp .;\fyicenter\glassfish4\mq\lib\tyrus-standalone-client.jar WebSocketEchoClient WebSocket session info: Protocol version: 13 Request URI: wss://echo.websocket.org Message received: Hello there!
⇒ WebSocketOpenClose.java - Session Open and Close
⇐ WebSocket Client Class Object Information
2018-01-16, ∼2969🔥, 0💬
Popular Posts:
The Bouncy Castle Crypto package is a Java implementation of cryptographic algorithms, it was develo...
JDK 11 java.desktop.jmod is the JMOD file for JDK 11 Desktop module. JDK 11 Desktop module compiled ...
What Is HttpComponents commons-httpclient-3.1.j ar?HttpComponents commons-httpclient-3.1.j aris the ...
What Is poi-examples-5.2.3.jar? poi-examples-5.2.3.jar is one of the JAR files for Apache POI 5.2.3,...
The Web Services Description Language for Java Toolkit (WSDL4J), Release 1.6.2, allows the creation,...