Skip to content

Instantly share code, notes, and snippets.

@wohugb
Forked from Globik/index.html
Last active April 5, 2022 02:36
Show Gist options
  • Save wohugb/2679528b2df1b144b54e4e98f885f950 to your computer and use it in GitHub Desktop.
Save wohugb/2679528b2df1b144b54e4e98f885f950 to your computer and use it in GitHub Desktop.
Websocket multi room based on ws.js
<html>
<body>
<h4>websocket</h4>
<h5>Group: <span id="group">darwin</span></h5>
<!-- a hardoced group name -->
<button onclick="bjoin();">join group</button><br />
<input id="text" type="text" />
<span id="out"></span>
<script>
var group = document.getElementById("group").textContent;
var ws = new WebSocket("ws://localhost:5000");
ws.onerror = function (e) {
out.innerHTML = e;
};
ws.onclose = function (e) {
out.innerHTML = "closed" + e;
};
ws.onopen = function () {
out.innerHTML = "connected ";
};
ws.onmessage = function (ms) {
out.innerHTML += ms.data + "<br>";
};
function send(msg) {
ws.send(JSON.stringify({ msg: msg }));
}
function broadcast(msg, room) {
ws.send(JSON.stringify({ room: room, msg: msg }));
}
function join(room) {
ws.send(JSON.stringify({ join: room }));
}
function bjoin() {
//alert(group);
join(group);
}
text.onchange = function (el) {
//alert(el.target.value);
broadcast(el.target.value, group);
};
</script>
</body>
</html>
const WebSocket = require('ws');
const http = require('http');
const express = require('express');
const app = express();
app.use(express.static('public'));
const bserver = http.createServer(app);
const webPort = 5000;
bserver.listen(webPort, function () {
console.log('Web server start. http://localhost:' + webPort);
});
const wss = new WebSocket.Server({ server: bserver });
wss.on('connection', ws => {
ws.room = [];
ws.send(JSON.stringify({ msg: "user joined" }));
console.log('connected');
ws.on('message', message => {
console.log('message: ', message);
//try{
var messag = JSON.parse(message);
//}catch(e){console.log(e)}
if (messag.join) { ws.room.push(messag.join) }
if (messag.room) { broadcast(message); }
if (messag.msg) { console.log('message: ', messag.msg) }
})
ws.on('error', e => console.log(e))
ws.on('close', (e) => console.log('websocket closed' + e))
})
function broadcast(message) {
wss.clients.forEach(client => {
if (client.room.indexOf(JSON.parse(message).room) > -1) {
client.send(message)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment