国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

springboot websocket 配置超時(shí)關(guān)閉連接

這篇具有很好參考價(jià)值的文章主要介紹了springboot websocket 配置超時(shí)關(guān)閉連接。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

客戶(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

方法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)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • SpringBoot WebSocket做客戶(hù)端

    常見(jiàn)的都是springboot應(yīng)用做服務(wù),前端頁(yè)面做客戶(hù)端,進(jìn)行websocket通信進(jìn)行數(shù)據(jù)傳輸交互。但其實(shí)springboot服務(wù)也能做客戶(hù)端去連接別的webSocket服務(wù)提供者。 剛好最近在項(xiàng)目中就使用到了,需求背景大概就是我們作為一個(gè)java段應(yīng)用需要和一個(gè)C語(yǔ)言應(yīng)用進(jìn)行通信。在項(xiàng)目需求及

    2024年02月11日
    瀏覽(23)
  • 快速搭建springboot websocket客戶(hù)端

    快速搭建springboot websocket客戶(hù)端

    WebSocket 是 HTML5 開(kāi)始提供的一種在單個(gè) TCP 連接上進(jìn)行全雙工通訊的協(xié)議。 HTML5 定義的 WebSocket 協(xié)議,能更好的節(jié)省服務(wù)器資源和帶寬,并且能夠更實(shí)時(shí)地進(jìn)行通訊。 HTML5 定義的 WebSocket 協(xié)議,能更好的節(jié)省服務(wù)器資源和帶寬,并且能夠更實(shí)時(shí)地進(jìn)行通訊。 瀏覽器通過(guò) JavaSc

    2024年02月06日
    瀏覽(24)
  • SpringBoot+WebSocket實(shí)現(xiàn)服務(wù)端、客戶(hù)端

    SpringBoot+WebSocket實(shí)現(xiàn)服務(wù)端、客戶(hù)端

    小編最近一直在使用springboot框架開(kāi)發(fā)項(xiàng)目,畢竟現(xiàn)在很多公司都在采用此框架,之后小編也會(huì)陸續(xù)寫(xiě)關(guān)于springboot開(kāi)發(fā)常用功能的文章。 什么場(chǎng)景下會(huì)要使用到websocket的呢? websocket主要功能就是實(shí)現(xiàn)網(wǎng)絡(luò)通訊,比如說(shuō)最經(jīng)典的客服聊天窗口、您有新的消息通知,或者是項(xiàng)目與

    2024年02月13日
    瀏覽(25)
  • 【go】gorilla/websocket如何判斷客戶(hù)端強(qiáng)制斷開(kāi)連接

    當(dāng)客戶(hù)端因?yàn)槟承﹩?wèn)題異常關(guān)閉連接時(shí),可以判斷關(guān)閉連接的異常類(lèi)型 通過(guò)調(diào)用websocket.IsCloseError或websocket.IsUnexpectedCloseError即可 其中g(shù)ithub源碼如下 異常類(lèi)型如下

    2024年02月16日
    瀏覽(56)
  • c++: websocket 客戶(hù)端與服務(wù)端之間的連接交互

    目錄 socket 頭文件 延遲時(shí)間 通信協(xié)議地址 TCP/IP 服務(wù)端 客戶(hù)端 編程步驟 服務(wù)端 客戶(hù)端 編程步驟 1. 初始化 WSAStartup 2. 創(chuàng)建 socket 2.1 協(xié)議族 2.2 socket 類(lèi)型 2.3 協(xié)議 3. 綁定 bind (服務(wù)端) 4. 監(jiān)聽(tīng) listen(服務(wù)端) 5. 請(qǐng)求連接 connect(客戶(hù)端) 6. 接收請(qǐng)求 accept(服務(wù)端) 7. 發(fā)送

    2024年02月14日
    瀏覽(49)
  • SpringBoot2.0集成WebSocket,多客戶(hù)端

    適用于單客戶(hù)端,一個(gè)賬號(hào)登陸一個(gè)客戶(hù)端,登陸多個(gè)客戶(hù)端會(huì)報(bào)錯(cuò) The remote endpoint was in state [TEXT_FULL_WRITING]? 這是因?yàn)榇藭r(shí)的session是不同的,只能鎖住一個(gè)session,解決此問(wèn)題的方法把全局靜態(tài)對(duì)象鎖住,因?yàn)橘~號(hào)是唯一的

    2024年02月10日
    瀏覽(23)
  • SpringBoot集成WebSocket實(shí)現(xiàn)客戶(hù)端與服務(wù)端通信

    SpringBoot集成WebSocket實(shí)現(xiàn)客戶(hù)端與服務(wù)端通信

    話不多說(shuō),直接上代碼看效果! 一、服務(wù)端: 1、引用依賴(lài) 2、添加配置文件 WebSocketConfig 3、編寫(xiě)WebSocket服務(wù)端接收、發(fā)送功能 ? 聲明接口代碼: ? 實(shí)現(xiàn)類(lèi)代碼: 4、如果不需要實(shí)現(xiàn)客戶(hù)端功能,此處可選擇前端調(diào)用,奉上代碼 二、客戶(hù)端: 1、引用依賴(lài) 2、自定義WebSocket客

    2024年01月23日
    瀏覽(24)
  • 如何將本地websocket服務(wù)端從本地暴露至公網(wǎng)實(shí)現(xiàn)客戶(hù)端遠(yuǎn)程連接

    如何將本地websocket服務(wù)端從本地暴露至公網(wǎng)實(shí)現(xiàn)客戶(hù)端遠(yuǎn)程連接

    1. Java 服務(wù)端demo環(huán)境 jdk1.8 框架:springboot+maven 工具IDEA 2. 在pom文件引入第三包封裝的netty框架maven坐標(biāo) 注意:pom文件里需注釋掉springbootweb啟動(dòng)器,web啟動(dòng)器默認(rèn)是tomcat服務(wù)啟動(dòng),會(huì)和netty服務(wù)沖突 3. 創(chuàng)建服務(wù)端,以接口模式調(diào)用,方便外部調(diào)用 4. 啟動(dòng)服務(wù),出現(xiàn)以下信息表示啟動(dòng)成功

    2024年04月10日
    瀏覽(29)
  • 【Java】SpringBoot快速整合WebSocket實(shí)現(xiàn)客戶(hù)端服務(wù)端相互推送信息

    【Java】SpringBoot快速整合WebSocket實(shí)現(xiàn)客戶(hù)端服務(wù)端相互推送信息

    目錄 什么是webSocket? webSocket可以用來(lái)做什么? WebSocket操作類(lèi) 一:測(cè)試客戶(hù)端向服務(wù)端推送消息 1.啟動(dòng)SpringBoot項(xiàng)目 2.打開(kāi)網(wǎng)站 3.進(jìn)行測(cè)試消息推送 4.后端進(jìn)行查看測(cè)試結(jié)果 二:測(cè)試服務(wù)端向客戶(hù)端推送消息 1.接口代碼 2.使用postman進(jìn)行調(diào)用 3.查看測(cè)試結(jié)果 ????????WebSocke

    2024年01月20日
    瀏覽(37)
  • Java:SpringBoot整合WebSocket實(shí)現(xiàn)服務(wù)端向客戶(hù)端推送消息

    Java:SpringBoot整合WebSocket實(shí)現(xiàn)服務(wù)端向客戶(hù)端推送消息

    思路: 后端通過(guò)websocket向前端推送消息,前端統(tǒng)一使用http協(xié)議接口向后端發(fā)送數(shù)據(jù) 本文僅放一部分重要的代碼,完整代碼可參看github倉(cāng)庫(kù) websocket 前端測(cè)試 :http://www.easyswoole.com/wstool.html 依賴(lài) 項(xiàng)目目錄 完整依賴(lài) 配置 WebSocketServer.java 前端頁(yè)面 websocket.html 前端邏輯 index.js 參

    2024年02月04日
    瀏覽(29)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包