纯java无前端实现命令行传输的WebSocket
程序员文章站
2022-05-19 17:15:27
...
背景介绍
项目需要服务端和数万个不可见IP地址的Agent传输信息,最初考虑用轮询,心跳单方面通信,每隔一段时间客服端向服务端发请求,带回数据。后考虑建立WebScoket实现客户端IP不可见情况下的即时通讯。
网上很多都是H5的WebScoket,少数采用java的跑不通,踩了不少坑,总算调通啦。
运行截图
项目结构
引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
引入依赖时注意,高层依赖会自动引入底层依赖,例如:spring-boot-starter-websocket
会直接引入spring-boot-starter-web
以及spring-boot-starter
注意不要重复引入。
踩过的坑
别的博客中引用的是org.springframework.boot
,会导致找不到ServerEndpointExporter
这个类,换用spring-boot-starter-websocket
后解决。
服务端
WebSocketConfig配置文件
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
MyWebSocketServer源码
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author aaa@qq.com
* @version 1.0
* @date 2020/7/13 17:40
*/
@Component
@ServerEndpoint(value = "/websocket")
public class MyWebSocketServer {
private static int onlineCount = 0;
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。
private static CopyOnWriteArraySet<MyWebSocketServer> webSocketSet = new CopyOnWriteArraySet<MyWebSocketServer>();
//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
/**
* 连接建立成功调用的方法*/
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this); //加入set中
addOnlineCount(); //在线数加1
System.out.println("有新连接加入!当前在线人数为" + getOnlineCount());
try {
sendMessage("当前在线人数为" + getOnlineCount());
} catch (IOException e) {
System.out.println("IO异常");
}
}
/**
* 连接关闭调用的方法
*/
@OnClose
public void onClose() {
webSocketSet.remove(this); //从set中删除
subOnlineCount(); //在线数减1
System.out.println("有一连接关闭!当前在线人数为" + getOnlineCount());
}
/**
* 收到客户端消息后调用的方法
*
* @param message 客户端发送过来的消息*/
@OnMessage
public void onMessage(String message, Session session) {
System.out.println("来自客户端的消息:" + message);
for (MyWebSocketServer item : webSocketSet) {
try {
item.sendMessage("服务端已经收到消息了,消息内容是"+message);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@OnError
public void onError(Session session, Throwable error) {
System.out.println("发生错误");
error.printStackTrace();
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
/**
* 群发自定义消息
* */
public static void sendInfo(String message) throws IOException {
for (MyWebSocketServer item : webSocketSet) {
try {
item.sendMessage(message);
} catch (IOException e) {
continue;
}
}
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
MyWebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
MyWebSocketServer.onlineCount--;
}
}
客户端
MyWebSocketClient源码
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
/**
* @author aaa@qq.com
* @version 1.0
* @date 2020/7/14 9:02
*/
public class MyWebSocketClient extends WebSocketClient {
public MyWebSocketClient(String url) throws URISyntaxException {
super(new URI(url));
}
@Override
public void onOpen(ServerHandshake shake) {
System.out.println("握手...");
for(Iterator<String> it = shake.iterateHttpFields(); it.hasNext();) {
String key = it.next();
System.out.println(key+":"+shake.getFieldValue(key));
}
}
@Override
public void onMessage(String paramString) {
System.out.println("客户端接收到消息:"+paramString);
}
@Override
public void onClose(int paramInt, String paramString, boolean paramBoolean) {
System.out.println("关闭...");
}
@Override
public void onError(Exception e) {
System.out.println("异常"+e);
}
public static void main(String[] args) {
try {
MyWebSocketClient client = new MyWebSocketClient("ws://127.0.0.1:8080/websocket");
client.connect();
while (!client.getReadyState().equals(WebSocket.READYSTATE.OPEN)) {
System.out.println("还没有打开");
}
System.out.println("建立websocket连接");
client.send("!!!!!!!!!!!!!");
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
上一篇: 婆媳的战国时代电视剧剧情
下一篇: python 变量命名规范