springboot 集成netty-socketio 在线聊天

首先引入jar


    com.corundumstudio.socketio
    netty-socketio
    1.7.7

配置文件

socketio:
  host: 0.0.0.0
  port: 999
  maxFramePayloadLength: 1048576
  maxHttpContentLength: 1048576
  bossCount: 1
  workCount: 100
  allowCustomRequests: true
  upgradeTimeout: 1000000
  pingTimeout: 6000000
  pingInterval: 25000

配置

@Configuration
public class SocketIOConfig {

    @Value("${socketio.host}")
    private String host;

    @Value("${socketio.port}")
    private Integer port;

    @Value("${socketio.bossCount}")
    private int bossCount;

    @Value("${socketio.workCount}")
    private int workCount;

    @Value("${socketio.allowCustomRequests}")
    private boolean allowCustomRequests;

    @Value("${socketio.upgradeTimeout}")
    private int upgradeTimeout;

    @Value("${socketio.pingTimeout}")
    private int pingTimeout;

    @Value("${socketio.pingInterval}")
    private int pingInterval;

    /**
     * 以下配置在上面的application.properties中已经注明
     * @return
     */
    @Bean
    public SocketIOServer socketIOServer() {
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setTcpNoDelay(true);
        socketConfig.setSoLinger(0);
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setSocketConfig(socketConfig);
        config.setHostname(host);
        config.setPort(port);
        config.setTransports(Transport.POLLING, Transport.WEBSOCKET);
        config.setBossThreads(bossCount);
        config.setWorkerThreads(workCount);
        config.setAllowCustomRequests(allowCustomRequests);
        config.setUpgradeTimeout(upgradeTimeout);
        config.setPingTimeout(pingTimeout);
        config.setPingInterval(pingInterval);
        return new SocketIOServer(config);
    }
}
创建接口

public interface  SocketIOService {
    //推送的事件
    public static final String PUSH_EVENT = "pushmessage";

    // 启动服务
    void start() throws Exception;

    // 停止服务
    void stop();

    // 推送信息
    void pushMessageToUser(PushMessage pushMessage);
}

 

@Service(value = "socketIOService")
public class SocketIOServiceImpl implements SocketIOService {
// 用来存已连接的客户端
private static Map clientMap = new ConcurrentHashMap<>();

@Autowired
private SocketIOServer socketIOServer;
@PostConstruct
private void autoStartup() throws Exception {
    start();
@Override
public void start() {
    // 监听客户端连接
    socketIOServer.addConnectListener(client -> {
      PushMessage push = new PushMessage();
     push.setContent(bu.getBusinessName()+"加入");
client .sendEvent(PUSH_EVENT, push);//如果给其他用户发。存到client中。然后循环

 

        }
    });

    // 监听客户端断开连接
    socketIOServer.addDisconnectListener(client -> {
       
        
 PushMessage push = new PushMessage();
     push.setContent(bu.getBusinessName()+"加入");
client .sendEvent(PUSH_EVENT, push);//如果给其他用户发。存到client中。然后循环
    });

    // 处理自定义的事件,与连接监听类似
    socketIOServer.addEventListener(PUSH_EVENT, PushMessage.class, (client, data, ackSender) -> {
       //收到的消息自行处理



    });
    socketIOServer.start();
}

@Override
public void stop() {
    if (socketIOServer != null) {
        socketIOServer.stop();
        socketIOServer = null;
    }
}

@Override
public void pushMessageToUser(PushMessage pushMessage) {
  //推送消息
}



/**
 * Spring IoC容器在销毁SocketIOServiceImpl Bean之前关闭,避免重启项目服务端口占用问题
 * @throws Exception
 */
@PreDestroy
private void autoStop() throws Exception  {
    stop();
}
}

PushMessage 只是一个简单的对象。可以跟进业务自己创建

你可能感兴趣的:(java)