Echo Client is a client-side JS library to test WebSocket with Echo Messages.
Download the echo.js file and add it to your project folder. Download here
<script src="echo.js"></script>
Use the package manager npm to install Echo Client.
npm install echo-client
You need to use Express Server to serve the file. Add this line to your Express Server.
app.use("/echo", express.static(__dirname + "/node_modules/echo-client"));
Next add it to your HTML.
<script src="/echo/echo.js"></script>
//Create a Echo providing the WebSocket Echo Server URL
//This one is good - "wss://echo.websocket.org"
let echo = new Echo("wss://echo.websocket.org");
//Set the Callback to execute when receiving a message from the server.
echo.whenReply((msg) => {
console.log(msg.data);
});
//generate(callback, n)
//This method send n messages with the callback that generate an object as content to Echo Server
//You will receive the same messages.
// IMPORTANT: The Callback must return an Object!
echo.generate(() => {return {hello: "Hello World!"}}, 2);
//generateEverySecond(callback, second, n)
//This method send n messages with the callback that generate an object as content to Echo Server
//and repeat this every setted second/s.
echo.generateEverySecond(() => {return {test: "I am looping every 1 second"}}, 1, 5);
//Stop the generateEverySecond method if loop is running.
echo.stop();
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
ISC