Provide WsRouter to plugins

This commit is contained in:
Asher
2021-01-20 14:11:08 -06:00
parent fb37473e72
commit 055e0ef9ec
8 changed files with 101 additions and 34 deletions

View File

@ -1,7 +1,10 @@
import * as express from "express"
import * as http from "http"
import * as nodeFetch from "node-fetch"
import Websocket from "ws"
import * as util from "../src/common/util"
import { ensureAddress } from "../src/node/app"
import { handleUpgrade } from "../src/node/wsRouter"
// Perhaps an abstraction similar to this should be used in app.ts as well.
export class HttpServer {
@ -39,6 +42,13 @@ export class HttpServer {
})
}
/**
* Send upgrade requests to an Express app.
*/
public listenUpgrade(app: express.Express): void {
handleUpgrade(app, this.hs)
}
/**
* close cleans up the server.
*/
@ -62,6 +72,13 @@ export class HttpServer {
return nodeFetch.default(`${ensureAddress(this.hs)}${requestPath}`, opts)
}
/**
* Open a websocket against the requset path.
*/
public ws(requestPath: string): Websocket {
return new Websocket(`${ensureAddress(this.hs).replace("http:", "ws:")}${requestPath}`)
}
public port(): number {
const addr = this.hs.address()
if (addr && typeof addr === "object") {