Create dedicated '/users' Socket.IO Namespace

This commit is contained in:
Patrick Jentsch
2024-11-06 13:04:30 +01:00
parent 2a28f19660
commit 289a551122
5 changed files with 97 additions and 145 deletions

View File

@ -4,7 +4,16 @@ nopaque.App = class App {
promises: {getUser: {}, subscribeUser: {}},
users: {},
};
this.socket = io({transports: ['websocket'], upgrade: false});
this.sockets = {};
this.sockets['/users'] = io(
'/users',
{
transports: ['websocket'],
upgrade: false
}
);
// this.socket = io({transports: ['websocket'], upgrade: false});
this.socket = this.sockets['/users'];
this.socket.on('PATCH', (patch) => {this.onPatch(patch);});
}
@ -14,7 +23,7 @@ nopaque.App = class App {
}
this.data.promises.getUser[userId] = new Promise((resolve, reject) => {
this.socket.emit('GET /users/<user_id>', userId, (response) => {
this.socket.emit('get_user', userId, (response) => {
if (response.status === 200) {
this.data.users[userId] = response.body;
resolve(this.data.users[userId]);
@ -33,7 +42,7 @@ nopaque.App = class App {
}
this.data.promises.subscribeUser[userId] = new Promise((resolve, reject) => {
this.socket.emit('SUBSCRIBE /users/<user_id>', userId, (response) => {
this.socket.emit('subscribe_user', userId, (response) => {
if (response.status !== 200) {
reject(response);
return;
@ -82,7 +91,7 @@ nopaque.App = class App {
let toastCloseActionElement = toast.el.querySelector('.action-button[data-action="close"]');
toastCloseActionElement.addEventListener('click', () => {toast.dismiss();});
}
onPatch(patch) {
// Filter Patch to only include operations on users that are initialized
let regExp = new RegExp(`^/users/(${Object.keys(this.data.users).join('|')})`);
@ -139,7 +148,7 @@ nopaque.App = class App {
/* Initialize Materialize Components */
// #region
// Automatically initialize Materialize Components that do not require
// additional configuration.
M.AutoInit();
@ -173,7 +182,7 @@ nopaque.App = class App {
/* Initialize nopaque Components */
// #region
// #region
nopaque.resource_displays.AutoInit();
nopaque.resource_lists.AutoInit();
nopaque.forms.AutoInit();