https://github.com/medooze/websocket-mockup
Mockup for websocket server
https://github.com/medooze/websocket-mockup
Last synced: about 1 month ago
JSON representation
Mockup for websocket server
- Host: GitHub
- URL: https://github.com/medooze/websocket-mockup
- Owner: medooze
- License: mit
- Created: 2018-12-10T09:01:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-01-21T17:44:26.000Z (over 3 years ago)
- Last Synced: 2025-04-21T02:05:18.391Z (about 1 month ago)
- Language: JavaScript
- Size: 36.1 KB
- Stars: 3
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# websocket-mockup
Mockup for websocket server and w3c websocket## Install
```
npm i --save-dev websocket-mockup
```
## API### WebSocketServerMockup
- constructor(params)
Create a new websocket server mockup. You can pass a `param.host` to be able to create different servers.
- Promise connect(url,protocols)
Will create a new websocket mockup in open state connected to this server
- Event: request
Event when a new websocket request is done, same API as the websocket server.
### Websocket
This is a shim of the W3C WebSocket intrface. It will connect to the WebSocketServerMockup which `host` matches the url host portion or to the default (`*`) one.
## Example```
const {WebSocketServer,WebSocket} = require ("websocket-mockup");const server = new WebSocketServer ({
host: "example.com"
});server.on ("request", (request) => {
//Get protocol
const protocol = request.requestedProtocols[0];//Accept the connection
const connection = request.accept (protocol, request.origin);console.log ((new Date ()) + ' Connection accepted.');
connection.on ('message', function (message) {
if (message.type === 'utf8')
{
console.log ('Received Message: ' + message.utf8Data);
connection.sendUTF (message.utf8Data);
} else if (message.type === 'binary') {
console.log ('Received Binary Message of ' + message.binaryData.length + ' bytes');
connection.sendBytes (message.binaryData);
}
});
connection.on ('close', function (reasonCode, description) {
console.log ((new Date ()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
});
});const ws = new WebSocket("wss://example.com");
ws.onopen = (event) => {
console.log("opened");
ws.send("hi!");
};
ws.onclosed = (event) => console.log("closed");
ws.onerror = (event) => console.log(event);
ws.onmessage = (event) => {
console.log("message: "+event.data);
ws.close();
};
```