You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
max
bd3f8090ff
|
2 years ago | |
---|---|---|
.. | ||
LICENSE | 2 years ago | |
README.md | 2 years ago | |
browser.js | 2 years ago | |
index.d.ts | 2 years ago | |
node.js | 2 years ago | |
package.json | 2 years ago |
README.md
isomorphic-ws
Isomorphic implementation of WebSocket.
It uses:
- ws on Node
- global.WebSocket in browsers
Limitations
Before using this module you should know that
ws
is not perfectly API compatible with
WebSocket,
you should always test your code against both Node and browsers.
Some major differences:
- no
Server
implementation in browsers - no support for the constructor
options
argument in browsers
Usage
You need to install both this package and ws:
> npm i isomorphic-ws ws
Then just require this package:
const WebSocket = require('isomorphic-ws');
const ws = new WebSocket('wss://echo.websocket.org/');
ws.onopen = function open() {
console.log('connected');
ws.send(Date.now());
};
ws.onclose = function close() {
console.log('disconnected');
};
ws.onmessage = function incoming(data) {
console.log(`Roundtrip time: ${Date.now() - data.data} ms`);
setTimeout(function timeout() {
ws.send(Date.now());
}, 500);
};