• 5
  • 新人请关照

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();
    }
阅读 181
评论
    1 个回答

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

      撰写回答

      登录后参与交流、获取后续更新提醒

      相似问题
      推荐文章