基于java做一个socket.io客户端
前言
最近公司这边让我去订阅一个第三方机构的websocket server,也是头疼,免不了和对方各种沟通,大家都很忙,收到回复很慢,开发方向也不知道。先是做了一个普通websocket的客户端,但后面了解到对方是基于socket.io做的一个server,又重新做了一个基于socket.io-client-java开源库的客户端。涉及到公司商业机密,所以做了一个demo,转自https://blog.csdn.net/q56231293811/article/details/84873776
<!--more-->
1. Client(Socket.IO Client Library for Java)
先上代码
package com.dasnnj.practice.share.socket;
import io.socket.client.IO;
import io.socket.client.Socket;
import java.util.Arrays;
/**
* Description <P> TODO : socket.io client端 <P>
*
* @author DASNNJ <a href="mailto:dasnnj@outlook.com"> dasnnj@outlook.com </a>
* @date 2019-04-27 18:32
*/
public class Client {
public static void main(String[] args) {
String url = "http://localhost:9999";
try {
IO.Options options = new IO.Options();
options.transports = new String[]{"websocket"};
//失败重试次数
options.reconnectionAttempts = 10;
//失败重连的时间间隔
options.reconnectionDelay = 1000;
//连接超时时间(ms)
options.timeout = 500;
final Socket socket = IO.socket(url, options);
//监听自定义msg事件
socket.on("msg", objects -> System.out.println("client: 收到msg->" + Arrays.toString(objects)));
//监听自定义订阅事件
socket.on("sub", objects -> System.out.println("client: " + "订阅成功,收到反馈->" + Arrays.toString(objects)));
socket.on(Socket.EVENT_CONNECT, objects -> {
socket.emit("sub", "我是訂閲對象");
System.out.println("client: " + "连接成功");
});
socket.on(Socket.EVENT_CONNECTING, objects -> System.out.println("client: " + "连接中"));
socket.on(Socket.EVENT_CONNECT_TIMEOUT, objects -> System.out.println("client: " + "连接超时"));
socket.on(Socket.EVENT_CONNECT_ERROR, objects -> System.out.println("client: " + "连接失败"));
socket.connect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
流程:
- 启动client会创建scoket,并将uri,options等参数set进去
- 监听一些事件(可自定义),也就是将event为key,回调为value,put 进callbacks(其为ConcurrentMap)
- connect 主动连接
- 当连接成功时候会触发
Socket.EVENT_CONNECT
事件,执行了socket.emit("sub", "我是訂閲對象");
订阅操作
2. Server(Socket.IO服务器的开源Java实现。基于Netty服务器框架。)
这边server只监听了sub事件(server监听client emit(提交)的sub事件,而client监听的是server emit(反馈)的sub事件)和断开连接事件,并循环发送广播消息(smg事件)
package com.dasnnj.practice.share.socket;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOServer;
import java.util.Map;
/**
* Description <P> TODO : socket.io server端 <P>
*
* @author DASNNJ <a href="mailto:dasnnj@outlook.com"> dasnnj@outlook.com </a>
* @date 2019-04-27 18:31
*/
public class Server {
public static void main(String[] args) {
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(9999);
SocketIOServer server = new SocketIOServer(config);
server.addConnectListener(client -> {
System.out.println("server: " + client.getRemoteAddress() + "客户端连接成功");
});
//添加sub事件
server.addEventListener("sub", String.class, (client, data, ackRequest) -> {
String c= client.getRemoteAddress().toString();
//获取客户端url参数
Map params = client.getHandshakeData().getUrlParams();
System.out.println("server: " + c+ ":客户端:订阅成功,订阅信息為->" + data);
//sub事件成功反馈
client.sendEvent("sub", c+ "客户端你好,我是服务端,你订阅成功了");
});
//添加客户端断开连接事件
server.addDisconnectListener(client -> {
String c= client.getRemoteAddress().toString();
//获取设备ip
String clientIp = c.substring(1, c.indexOf(":"));
System.out.println("server: " + clientIp + "-------------------------" + "客户端已断开连接");
});
server.start();
int i = 0;
while (true) {
try {
Thread.sleep(1500);
//广播消息
i++;
server.getBroadcastOperations().sendEvent("msg", "30", "hello" + i, "false");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
3. pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dasnnj.practice</groupId>
<artifactId>practice</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.socket</groupId>
<artifactId>socket.io-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>1.7.16</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.2</version>
</dependency>
</dependencies>
</project>
4. 再次感谢
转自 java netty-socketio 与 io.socket的使用,当时看了这位大佬的教程解决了问题
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。