文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

正确主动关闭websocket,异常关闭处理

2023-09-04 22:33

关注

CloseEvent.code

开发大型项目时需要正确主动关闭websocket,同时根据异常关闭正确的执行重连

关闭状态码简称原因
1000正常关闭连接成功地完成了创建它的目的。
1001离开端点消失了,可能是因为服务器故障,也可能是因为浏览器离开了打开连接的页面。
1002协议错误由于协议错误,端点正在终止连接。
1003不支持的数据由于端点接收到的数据类型无法接受,连接被终止。(例如,纯文本端点接收二进制数据
1004暂时保留保留。将来可能会定义一个含义。
1005No Status RcvdReserved. Indicates that no status code was provided even though one was expected.
1006Abnormal ClosureReserved. Indicates that a connection was closed abnormally (that is, with no close frame being sent) when a status code is expected.
1007Invalid frame payload dataThe endpoint is terminating the connection because a message was received that contained inconsistent data (e.g., non-UTF-8 data within a text message).
1008Policy ViolationThe endpoint is terminating the connection because it received a message that violates its policy. This is a generic status code, used when codes 1003 and 1009 are not suitable.
1009Message Too BigThe endpoint is terminating the connection because a data frame was received that is too large.
1010Mandatory Ext.The client is terminating the connection because it expected the server to negotiate one or more extension, but the server didn’t.
1011Internal ErrorThe server is terminating the connection because it encountered an unexpected condition that prevented it from fulfilling the request.
1012Service RestartThe server is terminating the connection because it is restarting.
1013Try Again LaterThe server is terminating the connection due to a temporary condition, e.g. it is overloaded and is casting off some of its clients.
1014Bad GatewayThe server was acting as a gateway or proxy and received an invalid response from the upstream server. This is similar to 502 HTTP Status Code.
1015TLS handshakeReserved. Indicates that the connection was closed due to a failure to perform a TLS handshake (e.g., the server certificate can’t be verified).
1016–2999For definition by future revisions of the WebSocket Protocol specification, and for definition by extension specifications.
3000–3999For use by libraries, frameworks, and applications. These status codes are registered directly with IANA. The interpretation of these codes is undefined by the WebSocket protocol.
4000–4999For private use, and thus can’t be registered. Such codes can be used by prior agreements between WebSocket applications. The interpretation of these codes is undefined by the WebSocket protocol.

后面的不想翻译,请读者自行翻译,帮你贴一个翻译传送门

在这里插入图片描述

关闭监听

TS

WebSocket.onclose = (event) => {  console.log(event.code);};

java

    @Override    public void onClosed(WebSocket webSocket, int code, String reason) {        super.onClosed(webSocket, code, reason);        // todo 根据状态码执行重连        if(code != 1000){                }    }

来源地址:https://blog.csdn.net/gao511147456/article/details/127887179

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-服务器
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯