swss to readme
This commit is contained in:
parent
0a5c0b9a64
commit
564e0f3935
32
README.md
32
README.md
|
@ -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...");
|
||||||
|
})
|
||||||
```
|
```
|
Reference in New Issue