From de9ca026899afbe7856b218b3d0e238c034e0743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Wed, 22 Mar 2023 15:28:49 +0800 Subject: [PATCH] =?UTF-8?q?websocket=20=E8=BF=9E=E6=8E=A5=20=E5=B0=8F?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../static/access/assets/js/websocket.js | 21 +++++---- test-code/.gitignore | 1 - test-code/package-lock.json | 44 ------------------ test-code/package.json | 14 ------ test-code/websocketTest.js | 45 ------------------- 6 files changed, 13 insertions(+), 114 deletions(-) delete mode 100644 test-code/.gitignore delete mode 100644 test-code/package-lock.json delete mode 100644 test-code/package.json delete mode 100644 test-code/websocketTest.js diff --git a/README.md b/README.md index 5da2e0b..2410137 100644 --- a/README.md +++ b/README.md @@ -258,7 +258,7 @@ window.wsUrl = 'ws://127.0.0.1:80/access/websocket/1'; #### nginx 代理配置(可选) -根据自己的实际情况来改 +配置文件在 `nginx-conf` 目录下(不能直接拿来用,需要根据自己的实际情况来改) ```conf server diff --git a/backend/microservice-provider-access-8002/src/main/resources/static/access/assets/js/websocket.js b/backend/microservice-provider-access-8002/src/main/resources/static/access/assets/js/websocket.js index 924eceb..92625c9 100644 --- a/backend/microservice-provider-access-8002/src/main/resources/static/access/assets/js/websocket.js +++ b/backend/microservice-provider-access-8002/src/main/resources/static/access/assets/js/websocket.js @@ -1,8 +1,8 @@ -// window.wsUrl = 'ws://epp.only4.work/access/websocket/1'; -window.wsUrl = 'ws://127.0.0.1:80/access/websocket/1'; -window.wsUrl = 'ws://127.0.0.1:8002/access/websocket/1'; -// window.wsUrl = 'ws://127.0.0.1:8000'; -window.ws = null; +window.wsUrl = 'wss://epp.only4.work/access/websocket/1'; +// window.wsUrl = 'ws://127.0.0.1:80/access/websocket/1'; +// window.wsUrl = 'ws://127.0.0.1:8002/access/websocket/1'; + +window.ws = null; // WebSocket 实例对象 (function () { if (!'WebSocket' in window) { @@ -11,7 +11,7 @@ window.ws = null; } function createConn() { - // 创建webscoket 对象 地址填入本地ip 端口是在搭建websocket服务器定义的端口 + // 创建webscoket 对象 const ws = new WebSocket(window.wsUrl) // 执行上面的语句之后,客户端就会与服务器进行连接 @@ -44,6 +44,10 @@ window.ws = null; ws.onclose = () => { console.log('onclose readyState', ws.readyState) console.log('onclose websocket连接关闭=======>') + console.log("等待 1s 尝试重新建立连接..."); + setTimeout(function () { + window.ws = createConn(); + }, 1000); } // 连接发生错误的回调方法 @@ -52,8 +56,7 @@ window.ws = null; console.log('onerror readyState', ws.readyState) console.log('onerror 发生错误==========>', error) } + return ws } - - createConn() - + window.ws = createConn() })() diff --git a/test-code/.gitignore b/test-code/.gitignore deleted file mode 100644 index 3c3629e..0000000 --- a/test-code/.gitignore +++ /dev/null @@ -1 +0,0 @@ -node_modules diff --git a/test-code/package-lock.json b/test-code/package-lock.json deleted file mode 100644 index 91a3a44..0000000 --- a/test-code/package-lock.json +++ /dev/null @@ -1,44 +0,0 @@ -{ - "name": "test-code", - "version": "1.0.0", - "lockfileVersion": 2, - "requires": true, - "packages": { - "": { - "name": "test-code", - "version": "1.0.0", - "license": "ISC", - "dependencies": { - "ws": "^8.11.0" - } - }, - "node_modules/ws": { - "version": "8.11.0", - "resolved": "https://registry.npmmirror.com/ws/-/ws-8.11.0.tgz", - "integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==", - "engines": { - "node": ">=10.0.0" - }, - "peerDependencies": { - "bufferutil": "^4.0.1", - "utf-8-validate": "^5.0.2" - }, - "peerDependenciesMeta": { - "bufferutil": { - "optional": true - }, - "utf-8-validate": { - "optional": true - } - } - } - }, - "dependencies": { - "ws": { - "version": "8.11.0", - "resolved": "https://registry.npmmirror.com/ws/-/ws-8.11.0.tgz", - "integrity": "sha512-HPG3wQd9sNQoT9xHyNCXoDUa+Xw/VevmY9FoHyQ+g+rrMn4j6FB4np7Z0OhdTgjx6MgQLK7jwSy1YecU1+4Asg==", - "requires": {} - } - } -} diff --git a/test-code/package.json b/test-code/package.json deleted file mode 100644 index 6f95043..0000000 --- a/test-code/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "test-code", - "version": "1.0.0", - "description": "", - "main": "websocketTest.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "", - "license": "ISC", - "dependencies": { - "ws": "^8.11.0" - } -} diff --git a/test-code/websocketTest.js b/test-code/websocketTest.js deleted file mode 100644 index 8ce01a7..0000000 --- a/test-code/websocketTest.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; - -const WebSocket = require('ws'); - -const webSocketUrl = 'ws://localhost:8002/websocket/1'; - -global.ws = null; // WebSocket 实例对象 - -/** - * 启动完毕,输出配置信息 - */ -console.log("Start running ...", "process.env", process.env); - -function createWebSocket() { - //申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头 - global.ws = new WebSocket(webSocketUrl); - - // 当WebSocket创建成功时,触发onopen事件 - global.ws.onopen = function () { - console.log("webhook is open."); - } - - // 当客户端收到服务端发送的关闭连接请求时,触发onclose事件 - global.ws.onclose = function (e) { - console.log("webhook is close."); - console.log("未知错误被关闭,等待 1s 尝试重新建立连接..."); - setTimeout(function () { - createWebSocket(); - }, 1000); - } - - // 如果出现连接、处理、接收、发送数据失败的时候触发onerror事件 - global.ws.onerror = function (e) { - console.log(e.error); - } - - // 当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据 - global.ws.onmessage = function (e) { - var data = JSON.parse(e.data); - console.log(data); - } -} - -// 创建连接 -createWebSocket();