@dfeidao/send-msg
Version:
消息总线
91 lines (90 loc) • 3.3 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "../utils/mqtt"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.listen = exports.push = void 0;
const mqtt_1 = __importStar(require("../utils/mqtt"));
const SESSION_ID = 'feidao-sessionid';
const LOGIN = 'loginservice.userlogin';
const APPID = 'web-client';
const FD_TOPIC_SEND_MSG = '/request/clientmsg';
function uuid() {
const CHARS = '0123456789abcdef'.split('');
const chars = CHARS;
const id = [];
const rnd = Math.random;
id[8] = id[13] = id[18] = id[23] = '-';
id[14] = '4';
for (let i = 0; i < 36; i++) {
if (!id[i]) {
const r = 0 | rnd() * 16;
id[i] = chars[(i === 19) ? (r & 0x3) | 0x8 : r & 0xf];
}
}
return id.join(''); // .replace(/-/g, '');
}
/**
* Push message to any mqtt client
*/
function push(topic, msg) {
(0, mqtt_1.push)(topic, msg, console);
}
exports.push = push;
/**
* Listen mqtt message
*/
function listen(topic, callback) {
return (0, mqtt_1.listen)(topic, callback, console);
}
exports.listen = listen;
/**
* Send message to SOA
*/
async function send_msg(spaceid, msg_type, msg, timeout, type) {
try {
const sessionid = localStorage.getItem(SESSION_ID);
const actionid = uuid();
const id = uuid();
const appid = APPID;
const jsgroup = `${spaceid}-${type}`;
const data = await (0, mqtt_1.default)(FD_TOPIC_SEND_MSG, `/reply/${id}`, JSON.stringify({ id, appid, type: msg_type, sessionid, actionid, spaceid, msg, jsgroup }), timeout, console);
if (msg_type === LOGIN) {
localStorage.setItem(SESSION_ID, data.sessionID);
}
return data;
}
catch (e) {
if (msg_type === LOGIN) {
localStorage.removeItem(SESSION_ID);
}
throw e;
}
}
exports.default = send_msg;
});