netty-scoket.io路径配置

1、服务端代码

java 复制代码
package com.yh.service.socket;


import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

@Component
@Order(1)
public class SocketIOStart implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(SocketIOStart.class);

    private SocketIOServer server;

    private RedissonStoreFactory redissonStoreFactory;


    @Autowired
    public SocketIOStart(SocketIOServer server, RedissonStoreFactory redissonStoreFactory) {
        this.server = server;
        this.redissonStoreFactory = redissonStoreFactory;
    }

    @Override
    public void run(String... strings) throws Exception {
        logger.info("SocketIO Server starting...");
        server.start();
    }

    @PreDestroy
    public void destory() {
        redissonStoreFactory.shutdown();
        server.stop();
    }

}
java 复制代码
package com.yh.service.socket;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.corundumstudio.socketio.listener.DefaultExceptionListener;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import com.corundumstudio.socketio.store.pubsub.PubSubStore;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SocketImConfig {

    private PubSubStore pubSubStore;

    private SocketIOServer server;

    private static final int imPort = 28888;


    @Bean
    public RedissonStoreFactory redissonStoreFactory(RedissonClient redissonClient) {
        return new RedissonStoreFactory(redissonClient);
    }

    @Bean
    public SocketIOServer socketIOServer(RedissonStoreFactory redissonStoreFactory) {
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setPort(imPort);
        config.setStoreFactory(redissonStoreFactory);
        config.setExceptionListener(new DefaultExceptionListener());
        config.getSocketConfig().setReuseAddress(true);
        config.getSocketConfig().setSoLinger(0);
        config.getSocketConfig().setTcpNoDelay(true);
        config.getSocketConfig().setTcpKeepAlive(true);
        // 设置自定义路径  默认是 socket.io
        config.setContext("/socket.io");
        //前端使用
//        io.connect(url,{ transports: ["websocket"],path:"/aaaa"});
        ////解决跨域问题
        config.setOrigin(null);
        server = new SocketIOServer(config);
        pubSubStore = server.getConfiguration().getStoreFactory().pubSubStore();
        return server;
    }

    @Bean
    public PubSubStore pubSubStore() {
        return pubSubStore;
    }

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


}

2、没有nginx代理时客户端代码

html 复制代码
      var url = 'http://172.16.50.21:28888';
              //path:"/socket.io"  对应服务端设置的   config.setContext("/socket.io");
		var socket =  io.connect(url,{ transports: ["websocket"],path:"/socket.io"});
		//var socket =  io.connect(url,{ transports: ["websocket"]});

3、展示

4、nginx代理后,代理配置

html 复制代码
		     location ^~/socket.io/ { 
            proxy_pass http://172.16.50.21:28888;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;##此处Upgrade注意大小写
            proxy_set_header Connection "Upgrade";
            proxy_set_header Remote_addr $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_connect_timeout 15s;
            proxy_send_timeout 3600s;
            proxy_read_timeout 3600s;

        }
        

5、代理后前端

html 复制代码
        var url = 'http://172.16.50.21';
         //path:"/socket.io"  对应服务端设置的   config.setContext("/socket.io");
		var socket =  io.connect(url,{ transports: ["websocket"],path:"/socket.io"});
		//var socket =  io.connect(url,{ transports: ["websocket"]});

6、变更路径 ,服务段代码

java 复制代码
package com.yh.service.socket;


import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.PreDestroy;

@Component
@Order(1)
public class SocketIOStart implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(SocketIOStart.class);

    private SocketIOServer server;

    private RedissonStoreFactory redissonStoreFactory;


    @Autowired
    public SocketIOStart(SocketIOServer server, RedissonStoreFactory redissonStoreFactory) {
        this.server = server;
        this.redissonStoreFactory = redissonStoreFactory;
    }

    @Override
    public void run(String... strings) throws Exception {
        logger.info("SocketIO Server starting...");
        server.start();
    }

    @PreDestroy
    public void destory() {
        redissonStoreFactory.shutdown();
        server.stop();
    }

}
java 复制代码
package com.yh.service.socket;

import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.corundumstudio.socketio.listener.DefaultExceptionListener;
import com.corundumstudio.socketio.store.RedissonStoreFactory;
import com.corundumstudio.socketio.store.pubsub.PubSubStore;
import org.redisson.api.RedissonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SocketImConfig {

    private PubSubStore pubSubStore;

    private SocketIOServer server;

    private static final int imPort = 28888;


    @Bean
    public RedissonStoreFactory redissonStoreFactory(RedissonClient redissonClient) {
        return new RedissonStoreFactory(redissonClient);
    }

    @Bean
    public SocketIOServer socketIOServer(RedissonStoreFactory redissonStoreFactory) {
        com.corundumstudio.socketio.Configuration config = new com.corundumstudio.socketio.Configuration();
        config.setPort(imPort);
        config.setStoreFactory(redissonStoreFactory);
        config.setExceptionListener(new DefaultExceptionListener());
        config.getSocketConfig().setReuseAddress(true);
        config.getSocketConfig().setSoLinger(0);
        config.getSocketConfig().setTcpNoDelay(true);
        config.getSocketConfig().setTcpKeepAlive(true);
        // 设置自定义路径  默认是 socket.io
        config.setContext("/aaaa");
        //前端使用
//        io.connect(url,{ transports: ["websocket"],path:"/aaaa"});
        ////解决跨域问题
        config.setOrigin(null);
        server = new SocketIOServer(config);
        pubSubStore = server.getConfiguration().getStoreFactory().pubSubStore();
        return server;
    }

    @Bean
    public PubSubStore pubSubStore() {
        return pubSubStore;
    }

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


}

7、没有nginx 代理,前端代码

html 复制代码
                var url = 'http://172.16.50.21:28888';
              
		var socket =  io.connect(url,{ transports: ["websocket"],path:"/aaaa"});
	

8、前端展示

9、设置路径 nginx代理 根据官方文档配置不可行

文档 https://socket.io/zh-CN/docs/v4/reverse-proxy/

相关推荐
fish_xk6 分钟前
map和set
java·开发语言
李崧正21 分钟前
Java技术分享:Lambda表达式与函数式编程
java·开发语言·python
老了,不知天命23 分钟前
鳶尾花項目JAVA
java·开发语言·机器学习
遇见火星28 分钟前
Nginx限流配置:防止接口被刷,服务器稳如泰山
运维·服务器·nginx
二哈赛车手30 分钟前
新人笔记---实现简易版的rag的bm25检索(利用ES),以及RAG上传时的ES与向量数据库双写
java·数据库·笔记·spring·elasticsearch·ai
winner888133 分钟前
从零吃透C++命名空间、std、#include、string、vector
java·开发语言·c++
爱上好庆祝37 分钟前
学习js的第五天
前端·css·学习·html·css3·js
计算机安禾40 分钟前
【Linux从入门到精通】第49篇:服务器故障排查终极指南——思路决定出路
linux·运维·服务器
AI人工智能+电脑小能手41 分钟前
【大白话说Java面试题】【Java基础篇】第26题:Java的抽象类和接口有哪些区别
java·开发语言·面试
古月-一个C++方向的小白42 分钟前
Linux——初识文件
linux·运维·服务器