当前位置:网站首页>【详细超简单】如何使用WebSocket链接
【详细超简单】如何使用WebSocket链接
2022-07-28 21:56:00 【沐卿゚】

created:
this.contactSocket()methods
contactSocket(){
let webSocket = null;
let socketOpen = false;
let that=this
if ("WebSocket" in window) {
webSocket = new WebSocket("ws://后台给的url");
webSocket.onopen = function (){
console.log("连接成功!");
webSocket.send(that.token)
socketOpen = true
};
webSocket.onmessage = function (evt) {
var received_msg = evt.data;
console.log("接受消息:" + received_msg);
};
webSocket.onclose = function () {
console.log("连接关闭!");
that.reconnect()
};
webSocket.onerror = function () {
console.log("连接异常!");
};
}
},
// 重连
reconnect(){
console.log('重新连接')
setInterval(this.contactSocket(), 10000)
},直接换好链接调用就行了,非常简单
边栏推荐
猜你喜欢
随机推荐
LabVIEW对VISA Write和Read函数的异步和同步
2022t elevator repair examination questions and simulation examination
Development of small programs ①
金仓数据库KingbaseES 客户端编程接口指南 - ODBC (2. 概述)
Combination of smart TV and applet
Blocking queue
酒店预订系统数据库房间库存设计思路
深度剖析集成学习GBDT
Mongodb index add, view, export, delete
互动滑轨屏在展厅中应用的制作步骤
CV实例分割模型小抄(1)
2022年G2电站锅炉司炉考试题库模拟考试平台操作
Zabbix 5.0 使用自带Redis模版监控
猿人学第二十题
Rhce第二天
【自】-刷题-集合
PHP 海报二维码合成
Runloop principle (II)
22 Niuke multi school Day1 I - Introduction to chiitoitsu DP
1314_ Serial port technology_ Basic information of RS232 communication








