1.0.51 • Published 3 years ago
wsbasic v1.0.51
wsBasic is a simple hook-on utility for ws.
Examples:
Server:
const WebSocket = require("ws"); /// require ws
const wss = new WebSocket.Server({ port: 8080, clientTracking: true }); /// define ws server
require("wsbasic")(wss); /// enable wsbasic
On connection:
/// gives a uuid for each client
wss.on('connection', function connection(ws) {
console.log(ws.uuid); /// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
});
To specific UUID:
wss.on('connection', function connection(ws) {
wss.sendToUUID('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'hello');
});
To client with specific attribute:
wss.on('connection', function connection(ws) {
wss.sendToAttribute({"channel": "/"}, 'hello');
});
Channels:
wss.on('connection', function connection(ws) {
ws.channel = "hello";
wss.broadcast("hello", "hi"); // as you probably guessed, this will send a message to all clients in "hello"
});