What I did here is creating a nodejs server, then whatever I receive a message I send it back to all the clients.
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const port = 6969;
const server = http.createServer(express);
const wss = new WebSocket.Server({ server })
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(data) {
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);
}
})
})
})
after this I create two pages admin.html and client.html in each one a websocket endpoint like this
ws = new WebSocket('ws://localhost:6969');
How could I change my code to make it send the messages just to the admin page?
thank u @user3094755 i did what u said and it works
i added ws.send("i am admin")
in the admin page
and change the code in the server like this
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(data) {
if (data=="i am admin")
ws.isadmin=true;
else ws.isadmin=false;
wss.clients.forEach(function each(client) {
if (client.isadmin=true && client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);}
})
})
})
i do not think that this is the best way to do it so if anyone have an idea pls do not hestate to share it with me