@trap_stevo/iotide
Version:
Provides the ultimate solution for real-time event management. Designed to make your applications dynamic and responsive, IoTide offers unparalleled flexibility in handling events on-the-fly. Effortlessly create interactive web applications, live data das
136 lines (135 loc) • 6.47 kB
JavaScript
;
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
var _require = require("@trap_stevo/legendarybuilderpronodejs-utilities"),
HUDUtilityManager = _require.HUDUtilityManager;
var currentRooms = {};
function joinRoom(socket, roomID, userID) {
var roomJoinedEventID = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "room joined";
var userJoinedEventID = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : "user joined";
var allUsersEventID = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : "all users";
var now = new Date();
var options = {
month: "2-digit",
day: "2-digit",
year: "numeric",
hour: "numeric",
minute: "2-digit",
hour12: true
};
if (!currentRooms[roomID]) {
currentRooms[roomID] = [];
}
if (!currentRooms[roomID].includes(userID)) {
currentRooms[roomID].push(userID);
socket.userID = userID;
socket.roomID = roomID;
HUDUtilityManager.outputGradient("Client ".concat(userID, " joined room ").concat(roomID, "!"), ["#00FFFF", "#8A2BE2"]);
socket.emit(roomJoinedEventID, userID, roomID);
socket.join(roomID);
socket.to(roomID).emit(userJoinedEventID, userID);
var otherUsers = currentRooms[roomID].filter(function (id) {
return id !== userID;
});
console.log("Other clients in room: ".concat(roomID, " [").concat(now.toLocaleString("en-us", options), "]: "), otherUsers);
socket.emit(allUsersEventID, otherUsers);
}
console.log("Online rooms: [".concat(now.toLocaleString("en-us", options), "]: "), currentRooms);
return;
}
;
function leaveRoom(socket, roomID, userID) {
var userLeftEventID = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "user left";
if (currentRooms[roomID]) {
currentRooms[roomID] = currentRooms[roomID].filter(function (id) {
return id !== userID;
});
if (currentRooms[roomID].length === 0) {
delete currentRooms[roomID];
}
HUDUtilityManager.outputGradient("Client ".concat(userID, " left room ").concat(roomID, "."), ["#00FFFF", "#8A2BE2"]);
socket.leave(roomID);
socket.to(roomID).emit(userLeftEventID, userID);
}
return;
}
;
function removeAllFromRoom(roomID, io) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var _options$removedEvent = options.removedEvent,
removedEvent = _options$removedEvent === void 0 ? "remove-from-room" : _options$removedEvent,
_options$notifyEvent = options.notifyEvent,
notifyEvent = _options$notifyEvent === void 0 ? "user remove" : _options$notifyEvent,
_options$notifyOthers = options.notifyOthers,
notifyOthers = _options$notifyOthers === void 0 ? false : _options$notifyOthers,
_options$emitRemoved = options.emitRemoved,
emitRemoved = _options$emitRemoved === void 0 ? false : _options$emitRemoved,
_options$disconnect = options.disconnect,
disconnect = _options$disconnect === void 0 ? true : _options$disconnect,
_options$socketIDs = options.socketIDs,
socketIDs = _options$socketIDs === void 0 ? null : _options$socketIDs,
_options$reason = options.reason,
reason = _options$reason === void 0 ? null : _options$reason;
if (!currentRooms[roomID]) {
return;
}
var allSocketIDs = io.sockets.adapter.rooms.get(roomID);
if (!allSocketIDs) {
return;
}
var targets = socketIDs ? new Set(socketIDs) : allSocketIDs;
var _iterator = _createForOfIteratorHelper(allSocketIDs),
_step;
try {
var _loop = function _loop() {
var socketID = _step.value;
if (!targets.has(socketID)) {
return 1; // continue
}
var socket = io.sockets.sockets.get(socketID);
if (socket) {
var userIDPresent = socket.userID !== null && socket.userID !== undefined;
var userID = userIDPresent ? socket.userID : "unknown";
HUDUtilityManager.outputGradient("Removing ".concat(userID, " from room ").concat(roomID, "..."), ["#8A2BE2"]);
if (emitRemoved) {
socket.emit(removedEvent, {
roomID: roomID,
reason: reason
});
}
socket.leave(roomID);
socket.roomID = null;
if (userIDPresent && currentRooms[roomID]) {
currentRooms[roomID] = currentRooms[roomID].filter(function (id) {
return id !== socket.userID;
});
HUDUtilityManager.outputGradient("Removed ".concat(userID, " from room ").concat(roomID, "."), ["#00FFFF", "#8A2BE2"]);
if (notifyOthers) {
socket.to(roomID).emit(notifyEvent, userID);
}
}
if (disconnect) {
socket.disconnect(true);
}
}
};
for (_iterator.s(); !(_step = _iterator.n()).done;) {
if (_loop()) continue;
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
if (currentRooms[roomID] && currentRooms[roomID].length === 0) {
delete currentRooms[roomID];
}
}
;
module.exports = {
removeAllFromRoom: removeAllFromRoom,
currentRooms: currentRooms,
leaveRoom: leaveRoom,
joinRoom: joinRoom
};