swss to readme

This commit is contained in:
Luca Conte 2024-08-21 19:45:20 +02:00
parent 0a5c0b9a64
commit 564e0f3935
1 changed files with 32 additions and 0 deletions

View File

@ -44,4 +44,36 @@ socket.on("open", (event) => {
// close socket and disable auto-reconnect // close socket and disable auto-reconnect
socket.close() socket.close()
```
## Server
The "SmartWebSocketServer" is far less sophisticated and simply introduces the events to the server side
```js
const { WebSocketServer } = require('ws');
// module only exports function to upgrade existing websocketserver
const upgradeWSS = require('../SmartWebSocketServer.js');
const wss = new WebSocketServer({ port: 8082 });
// very first thing to call after creating websocketserver
upgradeWSS(wss);
wss.on("connection", (ws) => {
console.log("connected");
// sending events to clients
ws.sendEvent("test", { "abc" : 123 });
// receiving events from clients
ws.on("test2", (data) => {
console.log(data);
});
});
wss.on("listening", () => {
console.log("listening...");
})
``` ```