Skip to content

Commit

Permalink
submit
Browse files Browse the repository at this point in the history
  • Loading branch information
mingxing.ai committed Aug 17, 2023
1 parent 0dd3d65 commit c45b16d
Show file tree
Hide file tree
Showing 25 changed files with 218 additions and 504 deletions.
Binary file added .DS_Store
Binary file not shown.
6 changes: 3 additions & 3 deletions ruoyi-common/ruoyi-common-websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

<dependencies>
<dependency>
<groupId>org.yeauty</groupId>
<artifactId>netty-websocket-spring-boot-starter</artifactId>
<version>0.12.0</version>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>2.0.3</version>
</dependency>
<dependency>
<groupId>com.ruoyi</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.ruoyi.common.websocket;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketConfig;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.Transport;
import com.ruoyi.common.websocket.handler.SocketIOServerHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Slf4j
@Component
public class DefaultSocketIOServer implements InitializingBean {

@Resource
private SocketIOServerHandler socketIOServerHandler;

@Value("${socket.io.host:localhost}")
private String host;

@Value("${socket.io.port:9001}")
private Integer port;

@Value("${socket.io.bossCount:1}")
private int bossCount;

@Value("${socket.io.workCount:10}")
private int workCount;

@Value("${socket.io.allowCustomRequests:true}")
private boolean allowCustomRequests;

@Value("${socket.io.upgradeTimeout:100000}")
private int upgradeTimeout;

@Value("${socket.io.pingTimeout:600000}")
private int pingTimeout;

@Value("${socket.io.pingInterval:20000}")
private int pingInterval;

@Override
public void afterPropertiesSet() throws Exception {
SocketConfig socketConfig = new SocketConfig();
socketConfig.setReuseAddress(true);
socketConfig.setTcpNoDelay(true);
socketConfig.setSoLinger(0);

Configuration configuration = new Configuration();
configuration.setSocketConfig(socketConfig);
// host在本地测试可以设置为localhost或者本机IP,在Linux服务器跑可换成服务器IP
configuration.setHostname(host);
configuration.setPort(port);
// socket连接数大小(如只监听一个端口boss线程组为1即可)
configuration.setBossThreads(bossCount);
configuration.setWorkerThreads(workCount);
configuration.setAllowCustomRequests(allowCustomRequests);
// 协议升级超时时间(毫秒),默认10秒。HTTP握手升级为ws协议超时时间
configuration.setUpgradeTimeout(upgradeTimeout);
// Ping消息超时时间(毫秒),默认60秒,这个时间间隔内没有接收到心跳消息就会发送超时事件
configuration.setPingTimeout(pingTimeout);
// Ping消息间隔(毫秒),默认25秒。客户端向服务器发送一条心跳消息间隔
configuration.setPingInterval(pingInterval);
configuration.setTransports(Transport.POLLING, Transport.WEBSOCKET);
configuration.setOrigin(":*:");
SocketIOServer socketIOServer = new SocketIOServer(configuration);
socketIOServer.addListeners(socketIOServerHandler);
socketIOServer.start();
log.info("socket io server start successfully!");
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ruoyi.common.websocket.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages = "com.ruoyi.common.websocket")
public class SocketIOConfig {
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.ruoyi.common.websocket.handler;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.ruoyi.common.core.constant.TokenConstants;
import com.ruoyi.common.websocket.session.SocketIOSessionPool;
import com.ruoyi.common.websocket.session.model.SocketIOSession;
import com.sun.nio.sctp.MessageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestHeader;

import javax.annotation.Resource;
import java.util.HashMap;

@Slf4j
@Component
public class SocketIOServerHandler {

@Resource
private SocketIOSessionPool sessionPool;

@OnConnect
public void onConnect(SocketIOClient socketIOClient) {
socketIOClient.sendEvent("connect", "成功");
sessionPool.createSession(SocketIOSession.builder().client(socketIOClient).build());
}

@OnEvent("heartbeat")
public void onHeartbeat(SocketIOClient socketIOClient, AckRequest ackRequest) {
log.info("接受到心跳信息");
socketIOClient.sendEvent("heartbeat", "ok");
}

}

This file was deleted.

This file was deleted.

Loading

0 comments on commit c45b16d

Please sign in to comment.