创建实例
let ws
= new WebSocket("url");
属性
ws.readyState
状态码状态名意义
0connecting正在连接1open连接成功,可以通信2closing连接正在关闭3closed连接已经关闭
事件
连接成功的回调 ws.onopen=function(){}
连接关闭的回调 ws.onclose=function(){}
收到数据的回调 ws.onmessage=function(event){}
报错的回调 ws.οnerrοr=function(){}
函数
发送数据 ws.send(data)