Skip to content

Commit

Permalink
Subscribe to multiple rooms when connected to the socket server #36
Browse files Browse the repository at this point in the history
  • Loading branch information
slmnio committed Jun 28, 2021
1 parent 786395c commit de92cb3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ app.get("/things/:ids", cors({ origin: corsOrigins}), async (req, res) => {
});

function cleanID(id) {
console.log(id);
if (!id) return null;
if (typeof id !== "string") return id.id || null; // no real id oops
if (id.startsWith("rec") && id.length === 17) id = id.slice(3);
Expand All @@ -55,6 +54,12 @@ io.on("connection", (socket) => {
id = cleanID(id);
socket.leave(id);
});
socket.on("subscribe-multiple", (ids) => {
console.log(`[multiple] client rejoining ${ids.length} rooms`);
ids.map(id => cleanID(id)).forEach(id => {
socket.join(id);
});
});
});

http.listen(port, () => {
Expand Down
3 changes: 2 additions & 1 deletion website/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const app = new Vue({
store,
sockets: {
connect() {
console.log("[socket]", "connected");
console.log("[socket]", "connected", this.$store.state.subscribed_ids.length);
this.$socket.client.emit("subscribe-multiple", this.$store.state.subscribed_ids);
},
data_update(d) {
// handled by vuex
Expand Down
2 changes: 2 additions & 0 deletions website/src/thing-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ export default new Vuex.Store({
this.commit("push", { id, data });
},
subscribe(state, id) {
if (!id) return;
if (state.subscribed_ids.includes(id)) return;
this._vm.$socket.client.emit("subscribe", id);
// console.log("[socket]", "subscribed to", id);
state.subscribed_ids.push(id);
},
unsubscribe(state, id) {
if (!id) return;
if (!state.subscribed_ids.includes(id)) return;
this._vm.$socket.client.emit("unsubscribe", id);
// console.log("[socket]", "unsubscribed from", id);
Expand Down

0 comments on commit de92cb3

Please sign in to comment.