一、客户端API
1、WebSocket构造函数
初始化实例后,客户端就会与服务器进行连接!实例对象的相关属性,查看官方API
const ws = new WebSocket('ws://localhost:8080');
2、readyState属性
readyState属性返回实例对象的连接状态。
- CONNECTING:值为0,表示正在连接。
- OPEN:值为1,表示连接成功,可以通信了。
- CLOSING:值为2,表示连接正在关闭。
- CLOSED:值为3,表示连接已经关闭,或者打开连接失败。
3、bufferedAmount属性
bufferedAmount属性返回当前还剩下多少二进制数据没有发送完成,可用来判断是否发送完毕。
4、onopen回调函数
连接成功后的回调函数。
ws.onopen = event => {
ws.send('服务器,我连接成功了!');
}
5、onclose回调函数
连接关闭后的回调函数。
ws.onclose = event => {
console.log(event.code)
console.log(event.reason)
console.log(event.wasClean)
}
6、onmessage回调函数
接收到服务器信息后的回调。
ws.onmessage= event => {
console.log(event.data)
}
7、send方法
向服务器发送消息的方法。
ws.send('String || blob || Arraybuffer');
8、onerror回调函数
发生错误时的回调。
ws.onerror = event => {
console.log(event)
}
二、客户端封装
1、封装代码
class Socket{
ws = null;
_alive = false;
_params = null;
_reconnect_timer = null;
_heart_timer = null;
_message_func = null;
heartBeat = 50000;
heartMsg = {msg: 'yd'};
reconnect = true;
reconnectTime = 5000;
reconnectTimes = 10;
constructor(params) {
this._params = params
this.init()
}
init() {
clearInterval(this._reconnect_timer);
clearInterval(this._heart_timer);
let params = this._params;
let {url, port} = params;
let global_params = ['heartBeat', 'heartMsg', 'reconnect', 'reconnectTime', 'reconnectTimes'];
Object.keys(params).forEach(key=>{
if (global_params.indexOf(key) !== -1) {
this[key] = params[key];
}
})
let ws_url = port ? url + ':' + port : url;
delete this.ws;
this.ws = new WebSocket(ws_url);
if(this._message_func) this.onmessage(this._message_func);
this.ws.onopen = () => {
this._alive = true;
clearInterval(this._reconnect_timer)
this.onheartbeat();
}
this.ws.onclose = () => {
this._alive = false;
clearInterval(this._heart_timer)
if (true == this.reconnect) {
this.onreconnect();
}
}
}
onheartbeat(func) {
if(true == this._alive) {
this._heart_timer = setInterval(()=>{
this.send(this.heartMsg)
func ? func(this) : false
}, this.heartBeat)
}
}
onreconnect(func) {
this._reconnect_timer = setInterval(()=>{
if(this.reconnectTimes <= 0){
clearInterval(this._reconnect_timer)
return;
}else{
this.reconnectTimes--;
}
this.init();
func ? func(this) : false
},this.reconnectTime)
}
send(text) {
if(true == this._alive){
text = typeof text == 'string' ? text : JSON.stringify(text);
this.ws.send(text);
console.log('发送心跳消息:', text)
}
}
close() {
if(true == this._alive){
this.ws.close();
}
}
onmessage(func, all = false) {
this.ws.onmessage = data => {
this._message_func = func;
func(!all ? data.data : data);
}
}
onopen(func) {
this.ws.onopen = event => {
this._alive = true;
func ? func(event) : false;
}
}
onclose(func) {
this.ws.onclose = event => {
this._alive = false;
clearInterval(this._heart_timer);
if(true == this.reconnect){
this.onreconnect();
}
func ? func(event) : false;
}
}
onerror(func) {
this.ws.onerror = event => {
func ? func(event) : false
}
}
}
export default Socket
2、用法
import Socket from '@/assets/js/webSocket'
ws = new Socket({
url: '服务器地址'
})
ws.onmessage((event) => {
})
注:爱护原创,转载请说明出处
|