客戶(hù)端與服務(wù)器在用websocket通信的時(shí)候,如果客戶(hù)端突然關(guān)閉網(wǎng)絡(luò)或者直接關(guān)機(jī),此時(shí)路由與服務(wù)器之間的鏈接還存在
在服務(wù)器端輸入查看
netstat -anp | grep 5007
tcp6 0 0 192.168.0.121:5007 119.119.0.0:60944 ESTABLISHED 23585/java
若不給該客戶(hù)端發(fā)信息,除非路由器重啟,否則這個(gè)鏈接會(huì)一直存在,服務(wù)器會(huì)一直認(rèn)為該鏈接存在,后果就是隨著大連無(wú)用的tcp連接積累,服務(wù)器會(huì)報(bào)socket too many open files錯(cuò)誤導(dǎo)致服務(wù)掛掉。
解決方法:
要求websocket客戶(hù)端定期發(fā)送PING,服務(wù)器返回PONG,客戶(hù)端意外斷開(kāi)的時(shí)候服務(wù)器發(fā)現(xiàn)在一段時(shí)間內(nèi)沒(méi)交互信息關(guān)閉該session。
通過(guò)讀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;
}
}
https://docs.spring.io/spring-framework/docs/5.3.24/reference/html/web.html#websocket-intro-architecture文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-501838.html
方法setMaxSessionIdleTimeout設(shè)置session超時(shí)時(shí)間。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-501838.html
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;
}
}
到了這里,關(guān)于springboot websocket 配置超時(shí)關(guān)閉連接的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!