Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

nginx配置socket.io,spring boot+vue前后端分离项目

云服务器中部署了一个项目,但是其中有一个socket.io需要持续发送,但是打开【检查-网络】查看的时候,连接都是fail掉的(fali,400,502等)
image.png
在spring boot的后台,打印出的消息是这样子的
image.png

nginx.conf的配置如下,上面的请求发送到nginx监听的80端口,根据socket.io匹配到8300的端口,应该没有问题吧
image.png

Spring boot启动类中,(config.setHostname("localhost")里面localhost是否不用改成公网IP呢?nginx会代理)

private static Logger logger = LoggerFactory.getLogger(UserRealm.class);

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MindTogetherApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);

        //scoket-io监听
        Configuration config = new Configuration();
        config.setHostname("localhost");
//        config.setHostname("公网IP");
        config.setPort(8300);
        //最大帧
        config.setMaxFramePayloadLength(1024 * 1024);
        config.setMaxHttpContentLength(1024 * 1024);

        final SocketIOServer server = new SocketIOServer(config);
        System.out.println("SocketIOServer Listening");
        
        server.addEventListener("report", SocketData.class, new DataListener<SocketData>() {
            @Override
            public void onData(SocketIOClient client, SocketData clientData, AckRequest ackRequest) {
                String sessionId =clientData.getSessionId();
                String password = clientData.getPassword();
                String data = clientData.getData();//后续需要后台验证id与密码的正确性

                clientData.setPassword("");//密码信息进行隐藏
                server.getBroadcastOperations().sendEvent("receive/"+sessionId,clientData);
            }
        });
        
        server.addEventListener("synchroToServer", SocketData.class, new DataListener<SocketData>() {
            @Override
            public void onData(SocketIOClient client, SocketData clientData, AckRequest ackRequest) {
                String sessionId =clientData.getSessionId();
                // logger.info("SocketIOServer 同步请求 SessionId:"+clientData.getSessionId());
                //排除发送者自己在前端验证
                server.getBroadcastOperations().sendEvent("synchroFromServer/"+sessionId,"");
            }
        });
        server.start();
        try {
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        server.stop();
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

image.png
图中的这三行应该删掉


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...