-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add WebSocket functionality to server.
- Loading branch information
1 parent
e39e796
commit 5948741
Showing
7 changed files
with
140 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import type WebSocket from 'ws'; | ||
import { AsyncHandler } from '../util/AsyncHandler'; | ||
import type { HttpRequest } from './HttpRequest'; | ||
|
||
/** | ||
* A WebSocketHandler handles the communication with multiple WebSockets | ||
*/ | ||
export abstract class WebSocketHandler | ||
extends AsyncHandler<{ webSocket: WebSocket; upgradeRequest: HttpRequest }> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import type { Server } from 'http'; | ||
import type { Socket } from 'net'; | ||
import type WebSocket from 'ws'; | ||
import { Server as WebSocketServer } from 'ws'; | ||
import type { HttpRequest } from './HttpRequest'; | ||
import type { HttpServerFactory } from './HttpServerFactory'; | ||
import type { WebSocketHandler } from './WebSocketHandler'; | ||
|
||
/** | ||
* Factory that adds WebSocket functionality to an existing server | ||
*/ | ||
export class WebSocketServerFactory implements HttpServerFactory { | ||
private readonly baseServerFactory: HttpServerFactory; | ||
private readonly webSocketHandler: WebSocketHandler; | ||
|
||
public constructor(baseServerFactory: HttpServerFactory, webSocketHandler: WebSocketHandler) { | ||
this.baseServerFactory = baseServerFactory; | ||
this.webSocketHandler = webSocketHandler; | ||
} | ||
|
||
public startServer(port: number): Server { | ||
// Create WebSocket server | ||
const webSocketServer = new WebSocketServer({ noServer: true }); | ||
webSocketServer.on('connection', async(webSocket: WebSocket, upgradeRequest: HttpRequest): Promise<void> => { | ||
await this.webSocketHandler.handleSafe({ webSocket, upgradeRequest }); | ||
}); | ||
|
||
// Create base HTTP server | ||
const httpServer = this.baseServerFactory.startServer(port); | ||
httpServer.on('upgrade', (upgradeRequest: HttpRequest, socket: Socket, head: Buffer): void => { | ||
webSocketServer.handleUpgrade(upgradeRequest, socket, head, (webSocket: WebSocket): void => { | ||
webSocketServer.emit('connection', webSocket, upgradeRequest); | ||
}); | ||
}); | ||
return httpServer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { Server } from 'http'; | ||
import request from 'supertest'; | ||
import WebSocket from 'ws'; | ||
import { ExpressHttpServerFactory } from '../../../src/server/ExpressHttpServerFactory'; | ||
import { HttpHandler } from '../../../src/server/HttpHandler'; | ||
import type { HttpRequest } from '../../../src/server/HttpRequest'; | ||
import type { HttpResponse } from '../../../src/server/HttpResponse'; | ||
import { WebSocketHandler } from '../../../src/server/WebSocketHandler'; | ||
import { WebSocketServerFactory } from '../../../src/server/WebSocketServerFactory'; | ||
|
||
class SimpleHttpHandler extends HttpHandler { | ||
public async handle(input: { request: HttpRequest; response: HttpResponse }): Promise<void> { | ||
input.response.end('SimpleHttpHandler'); | ||
} | ||
} | ||
|
||
class SimpleWebSocketHandler extends WebSocketHandler { | ||
public host: any; | ||
|
||
public async handle(input: { webSocket: WebSocket; upgradeRequest: HttpRequest }): Promise<void> { | ||
input.webSocket.send('SimpleWebSocketHandler'); | ||
input.webSocket.close(); | ||
this.host = input.upgradeRequest.headers.host; | ||
} | ||
} | ||
|
||
describe('SimpleWebSocketHandler', (): void => { | ||
let webSocketHandler: SimpleWebSocketHandler; | ||
let server: Server; | ||
|
||
beforeAll(async(): Promise<void> => { | ||
const httpHandler = new SimpleHttpHandler(); | ||
webSocketHandler = new SimpleWebSocketHandler(); | ||
const httpServerFactory = new ExpressHttpServerFactory(httpHandler); | ||
const webSocketServerFactory = new WebSocketServerFactory(httpServerFactory, webSocketHandler); | ||
server = webSocketServerFactory.startServer(5555); | ||
}); | ||
|
||
afterAll(async(): Promise<void> => { | ||
server.close(); | ||
}); | ||
|
||
it('has a functioning HTTP interface.', async(): Promise<void> => { | ||
const result = await request(server).get('/').expect('SimpleHttpHandler'); | ||
expect(result).toBeDefined(); | ||
}); | ||
|
||
it('has a functioning WebSockets interface.', async(): Promise<void> => { | ||
const client = new WebSocket('ws://localhost:5555'); | ||
const text = await new Promise((resolve): any => client.on('message', resolve)); | ||
expect(text).toBe('SimpleWebSocketHandler'); | ||
expect(webSocketHandler.host).toBe('localhost:5555'); | ||
}); | ||
}); |