Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | 1x 1x 1x 1x 1x 231x 231x 231x 224x 224x 224x 231x 1x 1x 230x 1x 1x 229x 2x 2x 227x 2x 2x 225x 1x 225x 225x 225x 225x 225x 225x 225x 225x 225x 15x 15x 15x 15x 14x 1x 14x 14x 14x 14x 14x 14x 30x 14x 14x 14x 14x 14x 14x 14x 249x 37x 28x 28x 28x 28x 31x 29x 29x 29x 15x 16x 1x 1x 1x | import { EventEmitter } from "events"
import url from "url"
import WS from "ws"
/**
*
* @param remote
* @param opts
* @constructor
*/
class Server extends EventEmitter {
public static domainRE = /^(?=.{1,255}$)[0-9A-Za-z](?:(?:[0-9A-Za-z]|[-_]){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|[-_]){0,61}[0-9A-Za-z])?)*\.?$/
public static onlineStates = [
"syncing",
"tracking",
"proposing",
"validating",
"full",
"connected"
]
public opts: any
public opts_host: any
public port: any
private _opts
private _url
private _remote
private _ws
private _connected
private _opened
private _state
private _id
private _timer
constructor(remote, opts) {
super()
this.setMaxListeners(0)
if (typeof opts === "string") {
const parsed = url.parse(opts)
const secure = parsed.protocol === "wss:"
opts = {
host: parsed.hostname,
port: parsed.port ? Number(parsed.port) : secure ? 443 : 80,
secure,
path: parsed.path
}
}
if (opts === null || typeof opts !== "object") {
this.opts = new TypeError("server options not supplied")
return this
}
if (!Server.domainRE.test(opts.host)) {
this.opts_host = new TypeError("server host incorrect")
return this
}
if (Number.isNaN(opts.port) || !Number.isFinite(opts.port)) {
this.port = new TypeError("server port not a number")
return this
}
if (opts.port < 1 || opts.port > 65535) {
this.port = new TypeError("server port out of range")
return this
}
if (typeof opts.secure !== "boolean") {
opts.secure = false
}
this._opts = opts
this._url =
(this._opts.secure ? "wss://" : "ws://") +
this._opts.host +
":" +
this._opts.port +
(this._opts.path ? this._opts.path : "")
this._remote = remote
this._ws = null
this._connected = false
this._opened = false
this._state = "offline"
this._id = 0
this._timer = 0
}
public connect(callback) {
Iif (this._connected) return
Iif (this._ws) this._ws.close()
try {
this._ws = new WS(this._url)
Iif ("open" in this._ws) {
this._ws.open()
}
} catch (e) {
return callback(e)
}
this._ws.on("open", () => {
this._opened = true
const req = this._remote.subscribe(["ledger", "server", "transactions"])
req.submit(callback)
})
Iif ("open" in this._ws) {
this._ws.on("message", (socket, data) => {
this._remote._handleMessage(data)
socket === null
})
} else {
this._ws.on("message", data => {
this._remote._handleMessage(data)
})
}
this._ws.on("close", () => {
this._handleClose()
})
Iif ("open" in this._ws) {
this._ws.on("error", (socket, err) => {
callback(err)
socket === null
})
} else {
this._ws.on("error", err => callback(err))
}
}
public async connectPromise() {
return new Promise((resolve, reject) => {
if (this._connected) {
resolve(`this._server is connected already`)
}
if (this._ws) this._ws.close()
try {
this._ws = new WS(this._url)
if ("open" in this._ws) {
this._ws.open()
}
} catch (e) {
reject(e)
}
this._ws.on("open", () => {
this._opened = true
const req = this._remote.subscribe(["ledger", "server", "transactions"])
req
.submitPromise()
.then(result => resolve(result))
.catch(error => reject(error))
})
if ("open" in this._ws) {
this._ws.on("message", (socket, data) => {
this._remote._handleMessage(data)
socket === null
})
} else {
this._ws.on("message", data => {
this._remote._handleMessage(data)
})
}
this._ws.on("close", () => {
this._handleClose()
})
if ("open" in this._ws) {
this._ws.on("error", (socket, err) => {
reject(err)
socket === null
})
} else {
this._ws.on("error", err => reject(err))
}
})
}
/**
* close manual, not close connection until new connection
*/
public disconnect() {
this._ws.close()
this._ws = null
this._setState("offline")
}
public isConnected() {
return this._connected
}
/**
* refuse to send msg if connection blows out
* @param message
*/
public sendMessage(command, data) {
if (!this._opened) return
const req_id = this._id++
const msg = Object.assign(
{},
{
id: req_id,
command
},
data
)
this._ws.send(JSON.stringify(msg))
return req_id
}
public _setState(state) {
if (state === this._state) return
this._state = state
this._connected = state === "online"
if (!this._connected) {
this._opened = false
}
}
/**
* handle close and error exception
* and should re-connect server after 3 seconds
*/
private _handleClose() {
if (this._state === "offline") return
this._setState("offline")
Eif (this._timer !== 0) return
this._remote.emit("disconnect")
this._timer = setInterval(() => {
this.connect((err, ret) => {
if (err) {
ret === 0
} else {
clearInterval(this._timer)
this._timer = 0
this._remote.emit("reconnect")
}
})
}, 3000)
}
}
export { Server }
|