zoukankan      html  css  js  c++  java
  • websocket通信1009错误,

    问题说明:

    springboot继承 WebSocketConfigurer实现websocket通信服务,服务器端报错,“The decoded text message was too big for the output buffer and the endpoint does not support partial messages”,浏览器端显示服务器上的该次会话已经关闭。1009错误,内容长度超限。

    问题解决

    在应用启动类中通过注解注入方式设置通信的文本和二进制消息的大小。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.scheduling.TaskScheduler;
    import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
    import org.springframework.web.socket.config.annotation.EnableWebSocket;
    import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
    import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
    import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
    
    @Configuration
    @EnableWebSocket
    public class WebSocketConfig implements WebSocketConfigurer {
    
        @Autowired
        private MyHandler myHandler;
    
        /**
         * sockJs 低版本浏览器不支持webSocket时使用
         * url结构:http://host:port/{endpoint}/{server-id}/{session-id}/websocket
         * 也可以: ws://host:port/{endpoint}/websocket
         * <p>
         * 不使用sockJs 访问时 url: ws://host:port/{endpoint}
         * <p>
         * setClientLibraryUrl 兼容客户端sockJs版本
         */
        @Override
        public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
     
            registry.addHandler(myHandler, "/myHandler").setAllowedOrigins("*");
            registry.addHandler(myHandler, "/myHandler").setAllowedOrigins("*").withSockJS()
                    .setTaskScheduler(sockJsScheduler()).setClientLibraryUrl("//cdn.jsdelivr.net/sockjs/1/sockjs.min.js");
        }
    
        @Bean
        public ServletServerContainerFactoryBean createWebSocketContainer() {
            ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
            // ws 传输数据的时候,数据过大有时候会接收不到,所以在此处设置bufferSize
            container.setMaxTextMessageBufferSize(512000);
            container.setMaxBinaryMessageBufferSize(512000);
            container.setMaxSessionIdleTimeout(15 * 60000L);
            return container;
        }
    
    }
    
  • 相关阅读:
    From MSI to WiX, Part 2
    From MSI to WiX, Part 1
    WIX Custom Action (immediate, deffered, rollback)
    SVN: revert all command
    HowTo: SVN undo add without reverting local changes
    “Assign Random Colors” is not working in 3ds Max 2015
    CruiseControl.NET : svnrevisionlabeller
    JSON parser error with double quotes
    How to: Extract files from a compiled setup.exe created by Inno setup
    虚拟账号ftp服务器
  • 原文地址:https://www.cnblogs.com/Pan-xi-yi/p/12003190.html
Copyright © 2011-2022 走看看