"use strict"; class WsController { constructor(ws, wss) { this.ws = ws; this.wss = wss; this.codeMap = require('../conf/code.inc.js'); } actionBroadcast(args) { let text = args.text; // Broadcast to everyone else. var wss = this.wss; wss.clients.forEach(function each(client) { client.send(text); }); } /** * sucess * @param {Array|String|Number} data 返回的数据 * @param {String} msg 返回的msg * @param {String} debugMsg 调试的msg * @author benzhan */ success(data, msg, debugMsg) { var code = CODE_SUCCESS; this.debugMsg = debugMsg; msg = msg || this.codeMap[code]; if (global['DEBUG'] && debugMsg) { msg = msg + " 【调试信息:" + debugMsg + "】"; } var ret = { result: 1, code: code, msg: msg, data: data }; this.exitData(ret); } /** * error with code * @param {Number} code * @param {String} msg * @param {String} debugMsg * @param {String} extData * @author benzhan */ error(code, msg, debugMsg, extData) { this.debugMsg = debugMsg; msg = msg || this.codeMap[code]; if (global['DEBUG'] && debugMsg) { msg = msg + " 【调试信息:" + debugMsg + "】"; } var ret = { result: 0, code: code, msg: msg }; if (extData) { ret['data'] = extData; } this.exitData(ret); } exitData(ret) { var json = JSON.stringify(ret); this.exitMsg(json, ret['code']); } exitMsg(content, code) { var ws = this.ws; code = code || CODE_SUCCESS; //必须是字符串 if (typeof content != "string") { content = JSON.stringify(content); } // 记录访问日志 var objCallLog = new CallLog(this); objCallLog.logSelfCall(code, content); ws.send(content); } } module.exports = WsController;