From 992030a3a4621fddc1558308f2fbd69a07a4c0e8 Mon Sep 17 00:00:00 2001 From: obeTvR9XDbUwquA6JPQhmbgaCCaiFa2rvf Date: Sat, 28 Mar 2020 16:47:22 +0800 Subject: [PATCH] forward bulletin from local cache --- main.js | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index 12d7fd5..10b1d3a 100644 --- a/main.js +++ b/main.js @@ -184,6 +184,7 @@ function initDB() { sequence INTEGER NOT NULL, content TEXT NOT NULL, quote TEXT NOT NULL, + json TEXT NOT NULL, signed_at INTEGER NOT NULL, created_at INTEGER NOT NULL)`, err => { if (err) { @@ -200,10 +201,12 @@ function initDB() { BulletinCount = items.length PageCount = BulletinCount / PageSize + 1 PageLinks = '' + let PageLinkArray = [] if (PageCount > 1) { for (let i = 1; i <= PageCount; i++) { - PageLinks = PageLinks + `${i} ` + PageLinkArray.push(`${i}`) } + PageLinks = PageLinkArray.join(' ') } } }) @@ -213,7 +216,9 @@ initDB() function handleClientMessage(message, json) { if (json["To"] != null && ClientConns[json["To"]] != null && ClientConns[json["To"]].readyState == WebSocket.OPEN) { + //forward message ClientConns[json["To"]].send(message) + //cache bulletin if (json["Action"] == ActionCode["ObjectResponse"] && json["Object"]["ObjectType"] == ObjectType["Bulletin"]) { //console.log(`###################LOG################### Client Message:`) @@ -223,8 +228,8 @@ function handleClientMessage(message, json) { let hash = quarterSHA512(JSON.stringify(bulletin)) let address = oxoKeyPairs.deriveAddress(bulletin.PublicKey) //console.log(hash) - let SQL = `INSERT INTO BULLETINS (hash, address, sequence, content, signed_at, created_at, quote) - VALUES ('${hash}', '${address}', '${bulletin.Sequence}', '${bulletin.Content}', ${bulletin.Timestamp}, ${timestamp}, '${JSON.stringify(bulletin.Quote)}')` + let SQL = `INSERT INTO BULLETINS (hash, address, sequence, content, quote, json, signed_at, created_at) + VALUES ('${hash}', '${address}', '${bulletin.Sequence}', '${bulletin.Content}', '${JSON.stringify(bulletin.Quote)}', '${JSON.stringify(bulletin)}', ${bulletin.Timestamp}, ${timestamp})` DB.run(SQL, err => { if (err) { console.log(err) @@ -232,14 +237,30 @@ function handleClientMessage(message, json) { BulletinCount = BulletinCount + 1 PageCount = BulletinCount / PageSize + 1 PageLinks = '' + let PageLinkArray = [] if (PageCount > 1) { for (let i = 1; i <= PageCount; i++) { - PageLinks = PageLinks + `${i}` + PageLinkArray.push(`${i}`) } + PageLinks = PageLinkArray.join(' ') } } }) } + } else if (json["Action"] == ActionCode["BulletinRequest"]) { + //send cache bulletin + let SQL = `SELECT * FROM BULLETINS WHERE address = "${json["Address"]}" AND sequence = "${json["Sequence"]}"` + DB.get(SQL, (err, item) => { + if (err) { + console.log(err) + } else { + if (item != null) { + console.log(json) + let address = oxoKeyPairs.deriveAddress(json["PublicKey"]) + ClientConns[address].send(item.json) + } + } + }) } } @@ -362,7 +383,7 @@ http.createServer(function(request, response) { oxo-chat-server - +

客户端源码

服务端源码

在线账号

@@ -382,7 +403,7 @@ http.createServer(function(request, response) { oxo-chat-server - +

在线账号

缓存的公告

${accountList} @@ -414,7 +435,7 @@ http.createServer(function(request, response) { oxo-chat-server - +

在线账号

缓存的公告