Skip to content

Commit

Permalink
feat: add WebSockets client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Nov 28, 2018
1 parent 7302ee5 commit a391601
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 3 deletions.
33 changes: 30 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"dependencies": {
"express": "^4.16.4",
"express-jwt": "^5.3.1",
"express-ws": "^4.0.0",
"get-port": "^4.0.0",
"reflect-metadata": "^0.1.12"
},
Expand All @@ -39,6 +40,7 @@
"@semantic-release/npm": "^5.0.5",
"@semantic-release/release-notes-generator": "^7.1.1",
"@types/express": "^4.16.0",
"@types/express-ws": "^3.0.0",
"@types/get-port": "^4.0.0",
"@types/jest": "^23.3.7",
"del": "^3.0.0",
Expand Down
34 changes: 34 additions & 0 deletions src/comms/WebSocketClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Client from './Client'
import JsonRpcRequest from './JsonRpcRequest'

/**
* A `Client` using the WebSockets API for communication.
*/
export default class WebSocketClient extends Client {


/**
* The URL of the `WebSocketServer` server
*/
server: string

/**
* A [`WebSocket`](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket) instance
*/
socket: WebSocket

constructor (server: string) {
super()
this.server = server
this.socket = new WebSocket(server)
this.socket.addEventListener('message', (event: MessageEvent) => {
this.recieve(event.data)
})
}

// Overrides of `Client` methods

send (request: JsonRpcRequest) {
this.socket.send(JSON.stringify(request))
}
}
22 changes: 22 additions & 0 deletions src/comms/WebSocketServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import expressWs from 'express-ws'

import HttpServer from './HttpServer'

/**
* A `Server` using WebSockets for communication.
*/
export default class WebSocketServer extends HttpServer {

constructor () {
super()

expressWs(this.app)

// @ts-ignore
this.app.ws('/', (ws, req) => {
ws.on('message', (request: string) => {
ws.send(this.recieve(request))
})
})
}
}
3 changes: 3 additions & 0 deletions tests/comms/browserTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import HttpClient from '../../src/comms/HttpClient'
import WebSocketClient from '../../src/comms/WebSocketClient'
import WebWorkerClient from '../../src/comms/WebWorkerClient'

var httpClient = new HttpClient('http://127.0.0.1:2000')

var wsClient = new WebSocketClient('ws://127.0.0.1:2000')

var worker = new Worker('webWorkerServer.js')
var wwClient = new WebWorkerClient(worker)
6 changes: 6 additions & 0 deletions tests/comms/webSocketServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env node

import WebSocketServer from '../../src/comms/WebSocketServer'

const server = new WebSocketServer()
server.run()

0 comments on commit a391601

Please sign in to comment.