WebSocket对象的“readyState”属性记录连接过程中的状态值websocket的两个属性:readyState和bufferedAmount。
根据readyState属性可以判断webSocket的连接状态,该属性的值可以是下⾯⼏种:
0 :对应常量CONNECTING (numeric value 0),
正在建⽴连接连接,还没有完成。The connection has not yet been established.
1 :对应常量OPEN (numeric value 1),
websocket和socket连接成功建⽴,可以进⾏通信。The WebSocket connection is established and communication is possible.
2 :对应常量CLOSING (numeric value 2)
连接正在进⾏关闭握⼿,即将关闭。The connection is going through the closing handshake.
3 : 对应常量CLOSED (numeric value 3)
连接已经关闭或者根本没有建⽴。The connection has been closed or could not be opened.
例:
var socket = new WebSocket(url);
adyState!=1){
alert("未连接。");
return;
}
根据bufferedAmount可以知道有多少字节的数据等待发送,若websocket已经调⽤了close⽅法则该属性将⼀直增长。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论