From e3db4d2f295de731b3ce3678466ab572637cceaf Mon Sep 17 00:00:00 2001 From: satori Date: Mon, 31 Jan 2022 14:21:50 +0800 Subject: [PATCH] chat --- assets/js/socket.js | 45 ++++++++++++ assets/sass/default.sass | 6 +- components/DialogueList.vue | 50 +++++++++++++ layouts/default.vue | 138 ++++++++++++++++++++---------------- pages/chat/index.vue | 64 +++++++++++++++++ 5 files changed, 242 insertions(+), 61 deletions(-) create mode 100644 assets/js/socket.js create mode 100644 components/DialogueList.vue create mode 100644 pages/chat/index.vue diff --git a/assets/js/socket.js b/assets/js/socket.js new file mode 100644 index 0000000..bee508b --- /dev/null +++ b/assets/js/socket.js @@ -0,0 +1,45 @@ +const that = { + controller: new Map(), + connection: null, + onclose: null, + onerror: null, + onopen: null, + onmessage: null, + delay: 0, + init: (link) => { + that.connection = new WebSocket(link) + that.connection.onopen = (event) => { + that.delay = 0 + console.log("Open connection") + that.connection.send(JSON.stringify({ fm: "chat", data: `Link Start! ${that.delay}` })) + // 检查消息版本, 从最后时间向前查20条 + // 当向上滚动时, 从本地最早时间向前查询20条 + // 落日志与合并日志 + if (that.onopen) that.onopen(); + } + that.connection.onmessage = (event) => { + console.log("[message]", event.data) + let data = JSON.parse(event.data) + console.log(event.data, "xxxxxxxxxxxxxx") + let call = that.controller.get(data.fm) + if (call) call(data) + if (that.onmessage) that.onmessage(); + } + that.connection.onclose = (event) => { + if (event.wasClean) { + console.log(`[close] code=${event.code} reason=${event.reason}`); + } else { + that.delay++ + console.log(`[close] 连接中断${that.delay}第次, 尝试重连(延时${that.delay} s)`); + setTimeout(that.init(link), that.delay * 1000) + } + if (that.onclose) that.onclose() + }; + that.connection.onerror = (error) => { + console.log(`[error] ${error.message}`); + if (that.onerror) that.onerror() + }; + } +} + +export default that diff --git a/assets/sass/default.sass b/assets/sass/default.sass index eb0e865..14c52ba 100644 --- a/assets/sass/default.sass +++ b/assets/sass/default.sass @@ -21,7 +21,11 @@ button color: #ffffff border: none border-radius: .25rem - + cursor: pointer + padding-left: 1rem + padding-right: 1rem + min-height: 2rem + font-weight: 600 a text-decoration: none a:link //未访问的 diff --git a/components/DialogueList.vue b/components/DialogueList.vue new file mode 100644 index 0000000..4ed0c0f --- /dev/null +++ b/components/DialogueList.vue @@ -0,0 +1,50 @@ + + + + + diff --git a/layouts/default.vue b/layouts/default.vue index 0c3c203..a0ccb18 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -1,32 +1,56 @@ diff --git a/pages/chat/index.vue b/pages/chat/index.vue new file mode 100644 index 0000000..311d046 --- /dev/null +++ b/pages/chat/index.vue @@ -0,0 +1,64 @@ + + + + +