netty-socketio端口占用问题

2020年02月21日 635点热度 0人点赞 0条评论

毕设中有一个聊天的功能,经过考虑最终采用netty-socketio来实现,但是重启总会出现端口占用的问题,很不方便,可以通过以下方法来解决。

方法一

重启服务前断开所有客户端的连接

方法二

添加配置socketConfig.setReuseAddress(true);

添加配置后完整的配置类

@Configuration
@EnableConfigurationProperties(SocketProperties.class)
public class SocketIOConfig {

    @Autowired
    private SocketProperties socketProperties;

    @Bean
    public SocketIOServer socketIOServer() {
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setTcpNoDelay(true);
        socketConfig.setSoLinger(0);
        socketConfig.setReuseAddress(true);
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setSocketConfig(socketConfig);
        config.setHostname(socketProperties.getHost());
        config.setPort(socketProperties.getPort());
        config.setBossThreads(socketProperties.getBossCount());
        config.setWorkerThreads(socketProperties.getWorkCount());
        config.setAllowCustomRequests(socketProperties.isAllowCustomRequests());
        config.setUpgradeTimeout(socketProperties.getUpgradeTimeout());
        config.setPingTimeout(socketProperties.getPingTimeout());
        config.setPingInterval(socketProperties.getPingInterval());
        return new SocketIOServer(config);
    }

    @Bean
    public SpringAnnotationScanner springAnnotationScanner() {
        return new SpringAnnotationScanner(socketIOServer());
    }
}

likole

人能常清静,天地悉皆归

文章评论