ws-mock

0.1.0 • Public • Published

ws-mock

Node module for mocking Web Socket (server side). Can be very helpful if you want to test ws-dependent code.

Usage

First install via npm:

  • npm: npm install ws-mock

simple example:

  var WsServerMock = require('ws-mock').WsServer;
  // create ws server instance
  var wsServer = new WsServerMock();
  // bind events
  wsServer.on('connection', function (ws) {
   console.log("new connection...");
   ws.on('message', function (msg) {
   console.log("incoming message:", msg);
   });
   ws.on('close', function () {
   console.log("connection closed...");
   });
  });
  // create ws connection 
  var wsConn = wsServer.addConnection(); // "new connection..."
  // send message that will be received by wsServer  
  wsConn.sendMsgToServer("dummy message"); // "incoming message: dummy message"
  // close a ws connection
  wsConn.closeConnection(); // "connection closed..."
  
  // clear all current connections
  wsServer.clearConnections();

Package Sidebar

Install

npm i ws-mock

Weekly Downloads

34

Version

0.1.0

License

MIT

Unpacked Size

6.18 kB

Total Files

8

Last publish

Collaborators

  • amiry