websocket与后台连接用着用着就自动断开了:
原因分析:
- 1.首先再websocket的关闭回调中添加日志,观察输出日志
ws.onclose = function (e) {
console.log('websocket 断开:' + e.code + ' ' + e.reason + ' ' + e.wasClean)
console.log(e)
}
CloseEvent.code:错误码
CloseEvent.reason: 断开原因
CloseEvent.WasClean: 是否正常断开
关闭状态码表
| 状态码 | 名称 | 描述 |
|---|---|---|
| 0-999 | 保留字段 | |
| 1000 | CLOSE_NORMAL | 正常关闭 |
| 1001 | CLOSE_GOING_AWAY | 终端离开,服务器端错误,也有可能是浏览器从页面跳转离开 |
| 1002 | CLOSE_PROTOCOL_ERROR | 由于协议错误而终端连接 |
| …… |
- 2.观察原因得出是1000,正常断开连接。多次测试发现,原来是因为点击了页面的下载功能后,webscoket就自动断开连接。必现
- 3.查看下载功能的代码,发现是:
<a style='color:#a0b883;text-decoration:underline;cursor:pointer;'
download= '错误日志.xlsx'
href = 'http://ip:port/group1/M00/03/8C/CsQVW1yI1wqAVLDZAAAruXJmym45.xlsx'>错误日志下载</a>
- 4.根据上述a标签发现,原来是因为a标签下载导致页面离开了。
解决办法:
给a标签加上target属性就可以解决。
<a target='_blank'
style='color:#a0b883;text-decoration:underline;cursor:pointer;'
download= '错误日志.xlsx'
href = 'http://ip:port/group1/M00/03/8C/CsQVW1yI1wqAVLDZAAAruXJmym45.xlsx'>错误日志下载</a>