-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add WebSockets client and server
- Loading branch information
Showing
6 changed files
with
97 additions
and
3 deletions.
There are no files selected for viewing
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,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)) | ||
} | ||
} |
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,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)) | ||
}) | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -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) |
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,6 @@ | ||
#!/usr/bin/env node | ||
|
||
import WebSocketServer from '../../src/comms/WebSocketServer' | ||
|
||
const server = new WebSocketServer() | ||
server.run() |