From 353139b8c101ba95faf63ca0ec029e01b85cade9 Mon Sep 17 00:00:00 2001 From: npmrun <1549469775@qq.com> Date: Thu, 16 Sep 2021 01:01:12 +0800 Subject: [PATCH] add --- package-lock.json | 13 ++++++++++--- package.json | 3 ++- pages/index/index.vue | 31 ++++++++++++++++++++++++++----- pages/index/socket.js | 20 ++++++++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 pages/index/socket.js diff --git a/package-lock.json b/package-lock.json index cc01a18..75dacf0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -375,6 +375,13 @@ "integrity": "sha1-KGlFnZqjviRf6P4sofRuLn9U1z8=", "requires": { "pako": "~1.0.5" + }, + "dependencies": { + "pako": { + "version": "1.0.11", + "resolved": "https://registry.nlark.com/pako/download/pako-1.0.11.tgz?cache=0&sync_timestamp=1627560125189&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fpako%2Fdownload%2Fpako-1.0.11.tgz", + "integrity": "sha1-bJWZ00DVTf05RjgCUqNXBaa5kr8=" + } } }, "buffer": { @@ -2013,9 +2020,9 @@ "integrity": "sha1-y8ec26+P1CKOE/Yh8rGiN8GyB7M=" }, "pako": { - "version": "1.0.11", - "resolved": "https://registry.nlark.com/pako/download/pako-1.0.11.tgz?cache=0&sync_timestamp=1627560125189&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fpako%2Fdownload%2Fpako-1.0.11.tgz", - "integrity": "sha1-bJWZ00DVTf05RjgCUqNXBaa5kr8=" + "version": "2.0.4", + "resolved": "https://registry.nlark.com/pako/download/pako-2.0.4.tgz?cache=0&sync_timestamp=1627560125189&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fpako%2Fdownload%2Fpako-2.0.4.tgz", + "integrity": "sha1-bOvEu7C2xzsNW41+hHbisvvqV20=" }, "parse-asn1": { "version": "5.1.6", diff --git a/package.json b/package.json index 9ec433f..aa5d098 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "ISC", "dependencies": { "hqchart": "^1.1.10351", - "jquery": "^3.6.0" + "jquery": "^3.6.0", + "pako": "^2.0.4" } } diff --git a/pages/index/index.vue b/pages/index/index.vue index 20d308d..0d4f79b 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -19,6 +19,14 @@ </template> <script> + import pako from "pako" + + function unzip(text) { + return pako.inflate(text, { + to: 'string' + }); + } + export default { data() { @@ -37,20 +45,33 @@ }); uni.onSocketOpen(function(res) { console.log('WebSocket连接已打开!'); + //pako.gzip(JSON.stringify({ "req": "topic to req", "id": "id generate by client" })) + uni.sendSocketMessage({ + data: JSON.stringify({ + "req": "market.btcusdt.kline.1min", + "id": "id10" + }) + }); }); uni.onSocketError(function(res) { console.log('WebSocket连接打开失败,请检查!'); }); uni.onSocketMessage(function(res) { - console.log('收到服务器内容:' + res.data); - }); - uni.onSocketClose(function(res) { - console.log('WebSocket 已关闭!'); + let data = JSON.parse(unzip(res.data)); + console.log(data); + if(data.ping){ + //pako.gzip(JSON.stringify({"pong": data.ping})) + uni.sendSocketMessage({ + data: JSON.stringify({"pong": data.ping}) + }); + } }); }, beforeDestroy() { uni.closeSocket(); - + uni.onSocketClose(function(res) { + console.log('WebSocket 已关闭!'); + }); }, onShow() { uni.getSystemInfo({ diff --git a/pages/index/socket.js b/pages/index/socket.js new file mode 100644 index 0000000..9536baa --- /dev/null +++ b/pages/index/socket.js @@ -0,0 +1,20 @@ +import pako from "pako" + +let bang = {}; +bang.isConnect = false; +bang.checkSocket = function checkSocket(){ + let time = 0; + function _send(success, fail){ + uni.sendSocketMessage({ + data: pako.gzip(JSON.stringify({"pong": data.ping})), + success() { + success&&success() + }, + fail() { + fail&&fail() + } + }); + } + _send() +} +