|
@@ -1,32 +1,119 @@
|
|
|
-// 创建 WebSocket 对象并连接到服务器
|
|
|
-var socket = new WebSocket("ws://127.0.0.1:16006/ws");
|
|
|
+function WebSocketWithHeartbeat(url) {
|
|
|
+ this.url = url;
|
|
|
+ this.websocket = null;
|
|
|
+ this.heartbeatInterval = 3000; // 心跳间隔时间(毫秒)
|
|
|
+ this.reconnectInterval = 1000; // 重新连接间隔时间(毫秒)
|
|
|
+ this.heartbeatTimer = null;
|
|
|
|
|
|
-// 当与服务器成功建立连接时触发该事件
|
|
|
-socket.onopen = function() {
|
|
|
- console.log("已经与服务器建立了连接。");
|
|
|
+ this.onOpen = null; // 连接打开时的回调函数
|
|
|
+ this.onMessage = null; // 收到消息时的回调函数
|
|
|
+ this.onClose = null; // 连接关闭时的回调函数
|
|
|
+ this.onError = null; // 发生错误时的回调函数
|
|
|
+
|
|
|
+ this.connect();
|
|
|
+}
|
|
|
+
|
|
|
+WebSocketWithHeartbeat.prototype.connect = function() {
|
|
|
+ this.websocket = new WebSocket(this.url);
|
|
|
+
|
|
|
+ this.websocket.onopen = () => {
|
|
|
+ console.log('WebSocket 连接已打开');
|
|
|
+
|
|
|
+ if (typeof this.onOpen === 'function') {
|
|
|
+ this.onOpen();
|
|
|
+ }
|
|
|
+
|
|
|
+ this.startHeartbeat();
|
|
|
+ };
|
|
|
+
|
|
|
+ this.websocket.onmessage = (event) => {
|
|
|
+ console.log('收到消息:', event.data);
|
|
|
+
|
|
|
+ if (typeof this.onMessage === 'function') {
|
|
|
+ this.onMessage(event.data);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ this.websocket.onclose = (event) => {
|
|
|
+ console.log('WebSocket 连接已关闭');
|
|
|
+
|
|
|
+ if (typeof this.onClose === 'function') {
|
|
|
+ this.onClose();
|
|
|
+ }
|
|
|
+
|
|
|
+ this.stopHeartbeat();
|
|
|
+
|
|
|
+ setTimeout(() => {
|
|
|
+ this.connect();
|
|
|
+ }, this.reconnectInterval);
|
|
|
+ };
|
|
|
+
|
|
|
+ this.websocket.onerror = (error) => {
|
|
|
+ console.error('WebSocket 错误:', error);
|
|
|
+
|
|
|
+ if (typeof this.onError === 'function') {
|
|
|
+ this.onError(error);
|
|
|
+ }
|
|
|
+ };
|
|
|
+};
|
|
|
+
|
|
|
+WebSocketWithHeartbeat.prototype.startHeartbeat = function() {
|
|
|
+ this.stopHeartbeat(); // 先停止之前的心跳计时器
|
|
|
+
|
|
|
+ this.heartbeatTimer = setInterval(() => {
|
|
|
+ this.sendHeartbeat();
|
|
|
+ }, this.heartbeatInterval);
|
|
|
};
|
|
|
|
|
|
-// 当收到来自服务器的消息时触发该事件
|
|
|
-socket.onmessage = function(event) {
|
|
|
- var message = event.data; // 获取从服务器传输过来的数据
|
|
|
- console.log("收到服务器的消息:", message);
|
|
|
+WebSocketWithHeartbeat.prototype.stopHeartbeat = function() {
|
|
|
+ if (this.heartbeatTimer) {
|
|
|
+ clearInterval(this.heartbeatTimer);
|
|
|
+ this.heartbeatTimer = null;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
-// 当与服务器断开连接时触发该事件
|
|
|
-socket.onclose = function() {
|
|
|
- console.log("与服务器的连接已关闭。");
|
|
|
+WebSocketWithHeartbeat.prototype.sendHeartbeat = function() {
|
|
|
+ var heartbeatMessage = 'heartbeat';
|
|
|
+
|
|
|
+ if (this.websocket.readyState === WebSocket.OPEN) {
|
|
|
+ this.websocket.send(heartbeatMessage);
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
-// 向服务器发送消息
|
|
|
-function sendMessageToServer(message) {
|
|
|
- if (socket.readyState === WebSocket.OPEN) {
|
|
|
- socket.send(message);
|
|
|
+WebSocketWithHeartbeat.prototype.send = function(message) {
|
|
|
+ if (this.websocket.readyState === WebSocket.OPEN) {
|
|
|
+ this.websocket.send(message);
|
|
|
} else {
|
|
|
- console.error("无法发送消息,因为与服务器的连接未打开或已关闭。");
|
|
|
+ console.error('WebSocket 连接未打开');
|
|
|
}
|
|
|
-}
|
|
|
+};
|
|
|
+
|
|
|
+WebSocketWithHeartbeat.prototype.close = function() {
|
|
|
+ if (this.websocket) {
|
|
|
+ this.websocket.close();
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 示例用法
|
|
|
+const socket = new WebSocketWithHeartbeat('ws://127.0.0.1:16006/ws');
|
|
|
+
|
|
|
+socket.onOpen = function() {
|
|
|
+ console.log('连接已打开');
|
|
|
+ // 可以在这里执行一些初始化操作或发送初始消息
|
|
|
+};
|
|
|
+
|
|
|
+socket.onMessage = function(data) {
|
|
|
+ console.log('收到消息:', data);
|
|
|
+ // 处理接收到的消息
|
|
|
+};
|
|
|
+
|
|
|
+socket.onClose = function() {
|
|
|
+ console.log('连接已关闭');
|
|
|
+};
|
|
|
+
|
|
|
+socket.onError = function(error) {
|
|
|
+ console.error('WebSocket 错误:', error);
|
|
|
+};
|
|
|
|
|
|
-// 关闭与服务器的连接
|
|
|
-function closeConnectionWithServer() {
|
|
|
- socket.close();
|
|
|
-}
|
|
|
+// 发送消息
|
|
|
+socket.send('Hello, WebSocket!');
|