客户端与服务器在用websocket通信的时候,如果客户端突然关闭网络或者直接关机,此时路由与服务器之间的链接还存在
在服务器端输入查看
netstat -anp | grep 5007
tcp6 0 0 192.168.0.121:5007 119.119.0.0:60944 ESTABLISHED 23585/java
若不给该客户端发信息,除非路由器重启,否则这个链接会一直存在,服务器会一直认为该链接存在,后果就是随着大连无用的tcp连接积累,服务器会报socket too many open files错误导致服务挂掉。
解决方法:
要求websocket客户端定期发送PING,服务器返回PONG,客户端意外断开的时候服务器发现在一段时间内没交互信息关闭该session。
通过读spring-websocket\5.3.24的源码并参考
源码 springframework\spring-websocket\5.3.24\spring-websocket-5.3.24-sources\org\springframework\web\socket\server\standard\ServletServerContainerFactoryBean.java
/*
* Copyright 2002-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.socket.server.standard;
import javax.servlet.ServletContext;
import javax.websocket.WebSocketContainer;
import javax.websocket.server.ServerContainer;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.web.context.ServletContextAware;
/**
* A {@link FactoryBean} for configuring {@link javax.websocket.server.ServerContainer}.
* Since there is usually only one {@code ServerContainer} instance accessible under a
* well-known {@code javax.servlet.ServletContext} attribute, simply declaring this
* FactoryBean and using its setters allows for configuring the {@code ServerContainer}
* through Spring configuration.
*
* <p>This is useful even if the {@code ServerContainer} is not injected into any other
* bean within the Spring application context. For example, an application can configure
* a {@link org.springframework.web.socket.server.support.DefaultHandshakeHandler},
* a {@link org.springframework.web.socket.sockjs.SockJsService}, or
* {@link ServerEndpointExporter}, and separately declare this FactoryBean in order
* to customize the properties of the (one and only) {@code ServerContainer} instance.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 4.0
*/
public class ServletServerContainerFactoryBean
implements FactoryBean<WebSocketContainer>, ServletContextAware, InitializingBean {
@Nullable
private Long asyncSendTimeout;
@Nullable
private Long maxSessionIdleTimeout;
@Nullable
private Integer maxTextMessageBufferSize;
@Nullable
private Integer maxBinaryMessageBufferSize;
@Nullable
private ServletContext servletContext;
@Nullable
private ServerContainer serverContainer;
public void setAsyncSendTimeout(Long timeoutInMillis) {
this.asyncSendTimeout = timeoutInMillis;
}
@Nullable
public Long getAsyncSendTimeout() {
return this.asyncSendTimeout;
}
public void setMaxSessionIdleTimeout(Long timeoutInMillis) {
this.maxSessionIdleTimeout = timeoutInMillis;
}
@Nullable
public Long getMaxSessionIdleTimeout() {
return this.maxSessionIdleTimeout;
}
public void setMaxTextMessageBufferSize(Integer bufferSize) {
this.maxTextMessageBufferSize = bufferSize;
}
@Nullable
public Integer getMaxTextMessageBufferSize() {
return this.maxTextMessageBufferSize;
}
public void setMaxBinaryMessageBufferSize(Integer bufferSize) {
this.maxBinaryMessageBufferSize = bufferSize;
}
@Nullable
public Integer getMaxBinaryMessageBufferSize() {
return this.maxBinaryMessageBufferSize;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
@Override
public void afterPropertiesSet() {
Assert.state(this.servletContext != null,
"A ServletContext is required to access the javax.websocket.server.ServerContainer instance");
this.serverContainer = (ServerContainer) this.servletContext.getAttribute(
"javax.websocket.server.ServerContainer");
Assert.state(this.serverContainer != null,
"Attribute 'javax.websocket.server.ServerContainer' not found in ServletContext");
if (this.asyncSendTimeout != null) {
this.serverContainer.setAsyncSendTimeout(this.asyncSendTimeout);
}
if (this.maxSessionIdleTimeout != null) {
this.serverContainer.setDefaultMaxSessionIdleTimeout(this.maxSessionIdleTimeout);
}
if (this.maxTextMessageBufferSize != null) {
this.serverContainer.setDefaultMaxTextMessageBufferSize(this.maxTextMessageBufferSize);
}
if (this.maxBinaryMessageBufferSize != null) {
this.serverContainer.setDefaultMaxBinaryMessageBufferSize(this.maxBinaryMessageBufferSize);
}
}
@Override
@Nullable
public ServerContainer getObject() {
return this.serverContainer;
}
@Override
public Class<?> getObjectType() {
return (this.serverContainer != null ? this.serverContainer.getClass() : ServerContainer.class);
}
@Override
public boolean isSingleton() {
return true;
}
}
方法setMaxSessionIdleTimeout设置session超时时间。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
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.HandshakeInterceptor;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Bean
public ServletServerContainerFactoryBean createWebSocketContainer() {
ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
container.setMaxTextMessageBufferSize(512000);
container.setMaxBinaryMessageBufferSize(512000);
container.setMaxSessionIdleTimeout(5 * 1000L);
return container;
}
}
版权归原作者 fanruitian 所有, 如有侵权,请联系我们删除。