@yume-chan/multiplex-websocket
v0.0.5
Published
A simple multiplex protocol for WebSocket
Downloads
4
Readme
Multiplex WebSocket
A simple multiplex protocol for WebSocket
Install
npm install @yume-chan/multiplex-websocket
Node.js
ws
is a peer dependency, you need to install it separately for Node.js.
npm install ws
Browsers
This package uses Node.js built-in modules (Buffer
, event
and stream
), so it needs polyfills to run in browsers.
Webpack should work, although not tested.
API
export default class MultiplexWebSocket {
static connect(url: string): Promise<MultiplexWebSocket>;
constructor(raw: WebSocket);
addChannel(): MultiplexWebSocketChannel;
close(): void;
on(type: 'channel', listener: (channel: MultiplexWebSocketChannel, head: Buffer) => void): this;
on(type: 'error', listener: (error: Error) => void): this;
on(type: 'close', listener: () => void): this;
off(type: 'channel', listener: (channel: MultiplexWebSocketChannel, head: Buffer) => void): this;
off(type: 'error', listener: (error: Error) => void): this;
off(type: 'close', listener: () => void): this;
}
export class MultiplexWebSocketChannel extends Duplex { }
MultiplexWebSocketChannel
is a duplex stream so you can use common stream operations (.read()
, .write()
, .pipe()
, .on('data')
) on it.
Development
This project uses pnpm (GitHub) to manage dependency packages.
Install dependencies
pnpm i
You may also use npm
, but the lockfile may become out of sync.
Testing
npm test
Coverage
npm run coverage
License
MIT