Skip to content

Commit 2dd4975

Browse files
authored
主动缓存公告
1 parent e4bfd88 commit 2dd4975

File tree

1 file changed

+78
-23
lines changed

1 file changed

+78
-23
lines changed

main.js

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,62 @@ function initDB() {
214214

215215
initDB()
216216

217+
////////hard copy from client<<<<<<<<
218+
const crypto = require("crypto")
219+
220+
const Seed = "xrUC4rWbXPizJVmusxb31LcexnkT2"
221+
const keypair = oxoKeyPairs.deriveKeypair(Seed)
222+
const Address = oxoKeyPairs.deriveAddress(keypair.publicKey)
223+
const PublicKey = keypair.publicKey
224+
const PrivateKey = keypair.privateKey
225+
226+
function sign(msg, sk) {
227+
let msgHexStr = strToHex(msg);
228+
let sig = oxoKeyPairs.sign(msgHexStr, sk);
229+
return sig;
230+
}
231+
232+
function GenBulletinRequest(address, sequence, to) {
233+
let json = {
234+
"Action": ActionCode["BulletinRequest"],
235+
"Address": address,
236+
"Sequence": sequence,
237+
"To": to,
238+
"Timestamp": Date.now(),
239+
"PublicKey": PublicKey
240+
}
241+
let sig = sign(JSON.stringify(json), PrivateKey)
242+
json.Signature = sig
243+
let strJson = JSON.stringify(json)
244+
return strJson
245+
}
246+
////////hard copy from client>>>>>>>>
247+
248+
function CacheBulletin(bulletin) {
249+
let timestamp = Date.now()
250+
let hash = quarterSHA512(JSON.stringify(bulletin))
251+
let address = oxoKeyPairs.deriveAddress(bulletin.PublicKey)
252+
//console.log(hash)
253+
let SQL = `INSERT INTO BULLETINS (hash, address, sequence, content, quote, json, signed_at, created_at)
254+
VALUES ('${hash}', '${address}', '${bulletin.Sequence}', '${bulletin.Content}', '${JSON.stringify(bulletin.Quote)}', '${JSON.stringify(bulletin)}', ${bulletin.Timestamp}, ${timestamp})`
255+
DB.run(SQL, err => {
256+
if (err) {
257+
console.log(err)
258+
} else {
259+
BulletinCount = BulletinCount + 1
260+
PageCount = BulletinCount / PageSize + 1
261+
PageLinks = ''
262+
let PageLinkArray = []
263+
if (PageCount > 1) {
264+
for (let i = 1; i <= PageCount; i++) {
265+
PageLinkArray.push(`<a href="/bulletins?page=${i}">${i}</a>`)
266+
}
267+
PageLinks = PageLinkArray.join(' ')
268+
}
269+
}
270+
})
271+
}
272+
217273
function handleClientMessage(message, json) {
218274
if (json["To"] != null && ClientConns[json["To"]] != null && ClientConns[json["To"]].readyState == WebSocket.OPEN) {
219275
//forward message
@@ -223,29 +279,7 @@ function handleClientMessage(message, json) {
223279
if (json["Action"] == ActionCode["ObjectResponse"] && json["Object"]["ObjectType"] == ObjectType["Bulletin"]) {
224280
//console.log(`###################LOG################### Client Message:`)
225281
//console.log(message)
226-
let timestamp = Date.now()
227-
let bulletin = json["Object"]
228-
let hash = quarterSHA512(JSON.stringify(bulletin))
229-
let address = oxoKeyPairs.deriveAddress(bulletin.PublicKey)
230-
//console.log(hash)
231-
let SQL = `INSERT INTO BULLETINS (hash, address, sequence, content, quote, json, signed_at, created_at)
232-
VALUES ('${hash}', '${address}', '${bulletin.Sequence}', '${bulletin.Content}', '${JSON.stringify(bulletin.Quote)}', '${JSON.stringify(bulletin)}', ${bulletin.Timestamp}, ${timestamp})`
233-
DB.run(SQL, err => {
234-
if (err) {
235-
console.log(err)
236-
} else {
237-
BulletinCount = BulletinCount + 1
238-
PageCount = BulletinCount / PageSize + 1
239-
PageLinks = ''
240-
let PageLinkArray = []
241-
if (PageCount > 1) {
242-
for (let i = 1; i <= PageCount; i++) {
243-
PageLinkArray.push(`<a href="/bulletins?page=${i}">${i}</a>`)
244-
}
245-
PageLinks = PageLinkArray.join(' ')
246-
}
247-
}
248-
})
282+
CacheBulletin(json["Object"])
249283
}
250284
} else if (json["Action"] == ActionCode["BulletinRequest"]) {
251285
//send cache bulletin
@@ -260,6 +294,14 @@ function handleClientMessage(message, json) {
260294
}
261295
}
262296
})
297+
} else if (json["To"] == Address && json["Action"] == ActionCode["ObjectResponse"] && json["Object"]["ObjectType"] == ObjectType["Bulletin"]) {
298+
CacheBulletin(json["Object"])
299+
//fetch more bulletin
300+
let address = oxoKeyPairs.deriveAddress(json["Object"].PublicKey)
301+
if (ClientConns[address] != null && ClientConns[address].readyState == WebSocket.OPEN) {
302+
let msg = GenBulletinRequest(address, json["Object"].Sequence + 1, address)
303+
ClientConns[address].send(msg)
304+
}
263305
}
264306
}
265307

@@ -305,6 +347,19 @@ function checkClientMessage(ws, message) {
305347
ClientConns[address] = ws
306348
updateAccountList()
307349
//handleClientMessage(message, json)
350+
let SQL = `SELECT * FROM BULLETINS WHERE address = "${address}" ORDER BY sequence DESC`
351+
DB.get(SQL, (err, item) => {
352+
if (err) {
353+
console.log(err)
354+
} else {
355+
let sequence = 1
356+
if (item != null) {
357+
sequence = item.sequence + 1
358+
}
359+
let msg = GenBulletinRequest(address, sequence, address)
360+
ClientConns[address].send(msg)
361+
}
362+
})
308363
} else if (ClientConns[address] != ws && ClientConns[address].readyState == WebSocket.OPEN) {
309364
//new connection kick old conection with same address
310365
//当前地址有对应连接,断开旧连接,当前地址对应到当前连接

0 commit comments

Comments
 (0)