-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
94 lines (88 loc) · 3.31 KB
/
socket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Output: SSE
const cors = require("cors")
const express = require("express")
const ip = require("ip")
const rvn = require("ravencore-lib")
const defaults = { port: 3001 }
const init = function(config) {
let app = (config.app ? config.app : express())
let connections = config.connections
app.use(cors())
app.use(function (req, res, next) {
res.sseSetup = function() {
res.writeHead(200, {
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
"Connection": "keep-alive",
})
res.sseSend({ type: "open", data: [] })
}
res.sseSend = function(data) {
res.write("data: " + JSON.stringify(data) + "\n\n")
}
next()
})
app.get("/s", async function(req, res) {
try {
let query = {
"v": 3, "q": { "find": {} }
}
// ravencoin address as fingerprint
const privateKey = new rvn.PrivateKey()
const fingerprint = privateKey.toAddress().toString()
res.$fingerprint = fingerprint
connections.pool[fingerprint] = { res: res, query: query }
console.log("## Opening connection from: " + fingerprint)
console.log(JSON.stringify(req.headers, null, 2))
req.on("close", function() {
console.log("## Closing connection from: " + res.$fingerprint)
console.log(JSON.stringify(req.headers, null, 2))
delete connections.pool[res.$fingerprint]
console.log(".. Pool size is now", Object.keys(connections.pool).length)
})
} catch (e) {
console.log(e)
}
})
app.get(/^\/s\/(.+)/, async function(req, res) {
try {
let b64 = req.params[0]
// ravencoin address as fingerprint
const privateKey = new rvn.PrivateKey()
const fingerprint = privateKey.toAddress().toString()
res.sseSetup()
let json = Buffer.from(b64, "base64").toString()
let query = JSON.parse(json)
res.$fingerprint = fingerprint
connections.pool[fingerprint] = { res: res, query: query }
console.log("## Opening connection from: " + fingerprint)
console.log(JSON.stringify(req.headers, null, 2))
req.on("close", function() {
console.log("## Closing connection from: " + res.$fingerprint)
console.log(JSON.stringify(req.headers, null, 2))
delete connections.pool[res.$fingerprint]
console.log(".. Pool size is now", Object.keys(connections.pool).length)
})
} catch (e) {
console.log(e)
}
})
// if no express app was passed in, need to bootstrap.
if (!config.app) {
let port = (config.port ? config.port : defaults.port)
app.listen(port , function () {
console.log("######################################################################################")
console.log("#")
console.log("# RVNSOCKET: Universal Programmable Ravencoin Push Notifications Network")
console.log("# Pushing Ravencoin in realtime through Server Sent Events...")
console.log("#")
console.log(`# API Endpoint: ${ip.address()}:${port}/s`)
console.log("#")
console.log("# Learn more at https://bitsocket.org") // https://developer.ravencoin.online/rvnsocket
console.log("#")
console.log("######################################################################################")
})
}
}
module.exports = { init: init }