当前位置:网站首页>Simple use of websocket
Simple use of websocket
2022-07-07 14:23:00 【LLAiden】
Introduce
WebSocket It's one in a single TCP Connect to carry on full duplex Communication protocol .WebSocket The communication protocol is 2011 By the IETF Set it as a standard RFC 6455, And by the RFC7936 Supplementary specifications .WebSocket API Also by W3C Set it as a standard .
WebSocket Makes it easier to exchange data between client and server , Allows the server to actively push data to the client . stay WebSocket API in , The browser and the server only need to complete a handshake , A persistent connection can be created directly between the two , And two-way data transmission
After the introduction, go directly to the code
Server side
public class WebSocketServerDemo {
static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
WebSocketServer webSocketServer = new WebSocketServer() {
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
System.out.println("welcome webSocket...");
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
System.out.println("close webSocket...");
}
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("received client message: " + message);
conn.send(simpleDateFormat.format(new Date()));
}
@Override
public void onError(WebSocket conn, Exception ex) {
ex.printStackTrace();
}
@Override
public void onStart() {
System.out.println("websocket start");
}
};
webSocketServer.start();
}
}client
public class WebSocketClientDemo {
public static void main(String[] args) throws URISyntaxException {
WebSocketClient webSocketClient = new WebSocketClient(new URI("ws://localhost:80")) {
@Override
public void onOpen(ServerHandshake handshakedata) {
String httpStatusMessage = handshakedata.getHttpStatusMessage();
System.out.println("httpStatusMessage: " + httpStatusMessage);
}
@Override
public void onMessage(String message) {
System.out.println("received server message: " + message);
}
@Override
public void onClose(int code, String reason, boolean remote) {
System.out.println("close code: " + code);
System.out.println("close reason: " + reason);
System.out.println("close remote: " + remote);
}
@Override
public void onError(Exception ex) {
ex.printStackTrace();
}
};
webSocketClient.connect();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int count = 0;
@Override
public void run() {
if (webSocketClient.isOpen()) {
webSocketClient.send(String.valueOf(count++));
}
}
}, 1000, 1000);
}okhttp Version client
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
Request request = new Request.Builder()
.url("ws://localhost:80")
.get()
.build();
WebSocket webSocket = okHttpClient.newWebSocket(request, new WebSocketListener() {
@Override
public void onFailure(@NotNull WebSocket webSocket, @NotNull Throwable t, @Nullable Response response) {
t.printStackTrace();
}
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull String text) {
System.out.println("received server message: " + text);
}
@Override
public void onMessage(@NotNull WebSocket webSocket, @NotNull ByteString bytes) {
String text = bytes.base64();
System.out.println("received server message: " + text);
}
@Override
public void onOpen(@NotNull WebSocket webSocket, @NotNull Response response) {
System.out.println(response);
}
});
Timer timer = new Timer();
timer.schedule(new TimerTask() {
int count = 0;
@Override
public void run() {
webSocket.send(String.valueOf(count++));
}
}, 1000, 1000);
}Running effect


Code address : https://github.com/LLAiden/webSocketDemo
Using open source libraries in code : 'org.java-websocket:Java-WebSocket:1.5.2'
Open source library address : https://github.com/TooTallNate/Java-WebSocket
https://github.com/TooTallNate/Java-WebSocket
边栏推荐
- STM32CubeMX,68套组件,遵循10条开源协议
- Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
- 请问指南针股票软件可靠吗?交易股票安全吗?
- 手里的闲钱是炒股票还是买理财产品好?
- Pert diagram (engineering network diagram)
- Flask session forged hctf admin
- Environment configuration
- XML文件的解析操作
- Excuse me, I have three partitions in Kafka, and the flinksql task has written the join operation. How can I give the join operation alone
- CSMA/CD 载波监听多点接入/碰撞检测协议
猜你喜欢
随机推荐
FCOS3D label assignment
Pert diagram (engineering network diagram)
FCOS3D label assignment
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
常用数字信号编码之反向不归零码码、曼彻斯特编码、差分曼彻斯特编码
docker部署oracle
Assign a dynamic value to the background color of DataGrid through ivalueconverter
Beginner XML
Use day JS let time (displayed as minutes, hours, days, months, and so on)
Common response status codes
Parsing of XML files
ndk初学习(一)
SAKT方法部分介绍
IP address home location query
Environment configuration of lavarel env
Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka
IP and long integer interchange
MRS离线数据分析:通过Flink作业处理OBS数据
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
3D Detection: 3D Box和点云 快速可视化





![SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1](/img/10/6de1ee8467b18ae03894a8d5ba95ff.png)


