Server Configuration

    每个底层 WebSocket 引擎都公开了控制运行时特性的配置属性,如消息缓冲区大小、空闲超时等。

    对于 Tomcat、WildFly 和 GlassFish,你可以将 ServletServerContainerFactoryBean 添加到你的 WebSocket Java config 中,如下例所示:

    1. @Configuration
    2. @EnableWebSocket
    3. public class WebSocketConfig implements WebSocketConfigurer {
    4. @Bean
    5. public ServletServerContainerFactoryBean createWebSocketContainer() {
    6. ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
    7. container.setMaxTextMessageBufferSize(8192);
    8. container.setMaxBinaryMessageBufferSize(8192);
    9. return container;
    10. }
    11. }

    等效的 XML 配置

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:websocket="http://www.springframework.org/schema/websocket"
    4. xsi:schemaLocation="
    5. http://www.springframework.org/schema/beans
    6. https://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/websocket
    8. https://www.springframework.org/schema/websocket/spring-websocket.xsd">
    9. <bean class="org.springframework...ServletServerContainerFactoryBean">
    10. <property name="maxTextMessageBufferSize" value="8192"/>
    11. <property name="maxBinaryMessageBufferSize" value="8192"/>
    12. </bean>
    13. </beans>

    :::info 对于客户端的 WebSocket 配置,你应该使用 WebSocketContainerFactoryBean(XML)或 ContainerProvider.getWebSocketContainer()(Java 配置)。 :::

    对于 Jetty,你需要提供一个预配置的 Jetty WebSocketServerFactory,并通过 WebSocket Java 配置将其插入 Spring 的 DefaultHandshakeHandler。下面的例子展示了如何做到这一点:

    1. @Configuration
    2. @EnableWebSocket
    3. public class WebSocketConfig implements WebSocketConfigurer {
    4. @Override
    5. public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    6. registry.addHandler(echoWebSocketHandler(),
    7. "/echo").setHandshakeHandler(handshakeHandler());
    8. }
    9. @Bean
    10. public DefaultHandshakeHandler handshakeHandler() {
    11. WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
    12. policy.setInputBufferSize(8192);
    13. policy.setIdleTimeout(600000);
    14. return new DefaultHandshakeHandler(
    15. new JettyRequestUpgradeStrategy(new WebSocketServerFactory(policy)));
    16. }
    17. }

    对应的 XML 配置如下

    1. <beans xmlns="http://www.springframework.org/schema/beans"
    2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:websocket="http://www.springframework.org/schema/websocket"
    4. xsi:schemaLocation="
    5. http://www.springframework.org/schema/beans
    6. https://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/websocket
    8. https://www.springframework.org/schema/websocket/spring-websocket.xsd">
    9. <websocket:handlers>
    10. <websocket:mapping path="/echo" handler="echoHandler"/>
    11. <websocket:handshake-handler ref="handshakeHandler"/>
    12. </websocket:handlers>
    13. <bean id="handshakeHandler" class="org.springframework...DefaultHandshakeHandler">
    14. <constructor-arg ref="upgradeStrategy"/>
    15. </bean>
    16. <bean id="upgradeStrategy" class="org.springframework...JettyRequestUpgradeStrategy">
    17. <constructor-arg ref="serverFactory"/>
    18. </bean>
    19. <bean id="serverFactory" class="org.eclipse.jetty...WebSocketServerFactory">
    20. <constructor-arg>
    21. <bean class="org.eclipse.jetty...WebSocketPolicy">
    22. <constructor-arg value="SERVER"/>
    23. <property name="inputBufferSize" value="8092"/>
    24. <property name="idleTimeout" value="600000"/>
    25. </bean>
    26. </constructor-arg>
    27. </bean>
    28. </beans>