Socket
Socket
Sign inDemoInstall

qcloud-iotexplorer-appdev-sdk

Package Overview
Dependencies
Maintainers
5
Versions
160
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qcloud-iotexplorer-appdev-sdk - npm Package Compare versions

Comparing version 2.3.9 to 2.3.10

2

lib/modules/IotWebsocket.js

@@ -1,2 +0,2 @@

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IotWebsocket=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram")),shortid_for_miniprogram_1=tslib_1.__importDefault(require("shortid-for-miniprogram")),utils_1=require("../utils"),logger_1=require("./logger"),defaultOptions={url:"wss://iot.cloud.tencent.com/ws/explorer",heartbeatInterval:5e4},IotWebsocket=function(e){function t(t,r){var s=e.call(this)||this;return s.sdk=t,s.requestHandlerMap=new Map,s.options=Object.assign({},defaultOptions,r),s._connected=!1,s._subscribeDeviceIdList=[],s._heartBeatTimer=null,s}return tslib_1.__extends(t,e),t.prototype.isConnected=function(){return!!this._connected},t.prototype.doConnectWs=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._doConnectWsPromise||(this._doConnectWsPromise=new Promise((function(t,r){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,s,i,n=this;return tslib_1.__generator(this,(function(o){e=shortid_for_miniprogram_1.default(),logger_1.wsLogger.updateGetReportData((function(){return{event:logger_1.WS_EVENT_PREFIX,context:{flowId:e}}})),s=function(e){r(e),n.emit("error",e),logger_1.wsLogger.error("connection","error",{error:e}),n.disconnect()};try{logger_1.wsLogger.info("connection","start"),i=this.options.url,this.ws=new utils_1.WebSocket(utils_1.appendParams(i,{uin:this.sdk._debug?this.sdk._defaultUin:this.sdk.loginManager.userId,cId:e})),this.ws.onOpen((function(){n._connected=!0,n.emit("connect"),t(),logger_1.wsLogger.info("connection","connected")})),this.ws.onError(s),this.ws.onMessage((function(e){var t=e.data;n.emit("message",t);try{t=JSON.parse(t)}catch(e){return void logger_1.wsLogger.error("message","parseFail",{data:t})}t.push?(n.emit("push",t),logger_1.wsLogger.info("message","pushData",{data:t})):void 0!==t.reqId&&n.requestHandlerMap.has(t.reqId)?n.requestHandlerMap.get(t.reqId)(null,t):logger_1.wsLogger.info("message","otherData",{data:t})})),this.ws.onClose((function(e){return tslib_1.__awaiter(n,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return logger_1.wsLogger.info("connection","close",{data:e}),this.disconnect(),this.emit("close",e),[2]}))}))}))}catch(e){s(e)}return[2]}))}))})))]}))}))},t.prototype.connect=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return e&&(this._subscribeDeviceIdList=e),[4,this.sdk.loginManager.checkLogin()];case 1:return t.sent(),this.isConnected()||!this._subscribeDeviceIdList.length?[3,3]:[4,this.doConnectWs()];case 2:t.sent(),t.label=3;case 3:return[2,this.activePush()]}}))}))},t.prototype.disconnect=function(){this.ws&&(logger_1.wsLogger.info("connection","disconnect"),this.ws.close(),this._connected=!1,this._doConnectWsPromise=null,this.ws=null,clearInterval(this._heartBeatTimer),this._heartBeatTimer=null)},t.prototype.send=function(e,t,r){void 0===t&&(t={});var s=(void 0===r?{}:r).reqId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var r,i,n,o,a,c=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:if(s||(s=shortid_for_miniprogram_1.default()),r="send."+e,logger_1.wsLogger.info(r,{message:"send start",data:{reqBody:utils_1.cutoffLong(JSON.stringify(t))},metric:{type:"start",spanId:s}}),!this.ws)return[3,6];i=Date.now(),this.ws.send({data:JSON.stringify({action:e,reqId:s,params:t})}),n={reqBody:utils_1.cutoffLong(JSON.stringify(t)),action:e,reqId:s,timeCost:null},o="success",u.label=1;case 1:return u.trys.push([1,3,4,5]),[4,Promise.race([new Promise((function(e,t){c.requestHandlerMap.set(s,(function(a,c){if(n.timeCost=Date.now()-i,!a)return c.data||!c.error&&!c.error_message?(logger_1.wsLogger.info(r,{message:"send "+o,metric:{type:o,spanId:s,timeCost:Date.now()-i},data:{resBody:utils_1.cutoffLong(JSON.stringify(c.data))}}),e(c.data)):(t({code:c.error,msg:c.error_message}),void(o="fail"));t(a),o="fail"}))})),new Promise((function(e,t){setTimeout((function(){t({code:"TIMEOUT"}),o="timeout"}),2e4)}))])];case 2:return[2,u.sent()];case 3:return a=u.sent(),logger_1.wsLogger.error(r,{message:"send "+o,metric:{type:o,spanId:s,timeCost:Date.now()-i},error:a}),[3,5];case 4:return this.requestHandlerMap.delete(s),[7];case 5:return[3,7];case 6:logger_1.wsLogger.error(r,{message:"no ws instance",metric:{type:"end",spanId:s}}),u.label=7;case 7:return[2]}}))}))},t.prototype.sendWsHeatBeat=function(){var e;if(null===(e=this._subscribeDeviceIdList)||void 0===e?void 0:e.length){var t=shortid_for_miniprogram_1.default(),r={Action:"AppDeviceTraceHeartBeat",ActionParams:Object.assign({},{Platform:this.sdk._apiPlatform,RequestId:t,AccessToken:this.sdk.loginManager.accessToken,DeviceIds:this._subscribeDeviceIdList})};return this.send("YunApi",r,{reqId:t})}},t.prototype.activePush=function(){var e=this,t=this.sdk.loginManager,r=t.isLogin,s=t.accessToken,i=t.appKey;r&&s&&this._subscribeDeviceIdList&&(this.send("ActivePush",{DeviceIds:this._subscribeDeviceIdList,Platform:"weapp",AccessToken:s,AppKey:i,RegionId:this.sdk._apiRegionId}),this.sendWsHeatBeat(),clearInterval(this._heartBeatTimer),this._heartBeatTimer=setInterval((function(){return e.sendWsHeatBeat()}),this.options.heartbeatInterval))},t}(event_emitter_for_miniprogram_1.default);exports.IotWebsocket=IotWebsocket;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.IotWebsocket=void 0;var tslib_1=require("tslib"),event_emitter_for_miniprogram_1=tslib_1.__importDefault(require("event-emitter-for-miniprogram")),shortid_for_miniprogram_1=tslib_1.__importDefault(require("shortid-for-miniprogram")),utils_1=require("../utils"),logger_1=require("./logger"),defaultOptions={url:"wss://iot.cloud.tencent.com/ws/explorer",heartbeatInterval:5e4},IotWebsocket=function(e){function t(t,r){var s=e.call(this)||this;return s.sdk=t,s.requestHandlerMap=new Map,s.options=Object.assign({},defaultOptions,r),s._connected=!1,s._subscribeDeviceIdList=[],s._heartBeatTimer=null,s}return tslib_1.__extends(t,e),t.prototype.isConnected=function(){return!!this._connected},t.prototype.doConnectWs=function(){return tslib_1.__awaiter(this,void 0,void 0,(function(){var e=this;return tslib_1.__generator(this,(function(t){return[2,this._doConnectWsPromise||(this._doConnectWsPromise=new Promise((function(t,r){return tslib_1.__awaiter(e,void 0,void 0,(function(){var e,s,i,n=this;return tslib_1.__generator(this,(function(o){e=shortid_for_miniprogram_1.default(),logger_1.wsLogger.updateGetReportData((function(){return{event:logger_1.WS_EVENT_PREFIX,context:{flowId:e}}})),s=function(e){r(e),logger_1.wsLogger.error("connection","error",{error:e}),n.disconnect(),n.emit("error",e)};try{logger_1.wsLogger.info("connection","start"),i=this.options.url,this.ws=new utils_1.WebSocket(utils_1.appendParams(i,{uin:this.sdk._debug?this.sdk._defaultUin:this.sdk.loginManager.userId,cId:e})),this.ws.onOpen((function(){n._connected=!0,n.emit("connect"),t(),logger_1.wsLogger.info("connection","connected")})),this.ws.onError(s),this.ws.onMessage((function(e){var t=e.data;n.emit("message",t);try{t=JSON.parse(t)}catch(e){return void logger_1.wsLogger.error("message","parseFail",{data:t})}t.push?(n.emit("push",t),logger_1.wsLogger.info("message","pushData",{data:t})):void 0!==t.reqId&&n.requestHandlerMap.has(t.reqId)?n.requestHandlerMap.get(t.reqId)(null,t):logger_1.wsLogger.info("message","otherData",{data:t})})),this.ws.onClose((function(e){return tslib_1.__awaiter(n,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){return logger_1.wsLogger.info("connection","close",{data:e}),this.disconnect(),this.emit("close",e),[2]}))}))}))}catch(e){s(e)}return[2]}))}))})))]}))}))},t.prototype.connect=function(e){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(t){switch(t.label){case 0:return e&&(this._subscribeDeviceIdList=e),[4,this.sdk.loginManager.checkLogin()];case 1:return t.sent(),this.isConnected()||!this._subscribeDeviceIdList.length?[3,3]:[4,this.doConnectWs()];case 2:t.sent(),t.label=3;case 3:return[2,this.activePush()]}}))}))},t.prototype.disconnect=function(){this.ws&&(logger_1.wsLogger.info("connection","disconnect"),this.ws.close(),this._connected=!1,this._doConnectWsPromise=null,this.ws=null,clearInterval(this._heartBeatTimer),this._heartBeatTimer=null)},t.prototype.send=function(e,t,r){void 0===t&&(t={});var s=(void 0===r?{}:r).reqId;return tslib_1.__awaiter(this,void 0,void 0,(function(){var r,i,n,o,a,c=this;return tslib_1.__generator(this,(function(u){switch(u.label){case 0:if(s||(s=shortid_for_miniprogram_1.default()),r="send."+e,logger_1.wsLogger.info(r,{message:"send start",data:{reqBody:utils_1.cutoffLong(JSON.stringify(t))},metric:{type:"start",spanId:s}}),!this.ws)return[3,6];i=Date.now(),this.ws.send({data:JSON.stringify({action:e,reqId:s,params:t})}),n={reqBody:utils_1.cutoffLong(JSON.stringify(t)),action:e,reqId:s,timeCost:null},o="success",u.label=1;case 1:return u.trys.push([1,3,4,5]),[4,Promise.race([new Promise((function(e,t){c.requestHandlerMap.set(s,(function(a,c){if(n.timeCost=Date.now()-i,!a)return c.data||!c.error&&!c.error_message?(logger_1.wsLogger.info(r,{message:"send "+o,metric:{type:o,spanId:s,timeCost:Date.now()-i},data:{resBody:utils_1.cutoffLong(JSON.stringify(c.data))}}),e(c.data)):(t({code:c.error,msg:c.error_message}),void(o="fail"));t(a),o="fail"}))})),new Promise((function(e,t){setTimeout((function(){t({code:"TIMEOUT"}),o="timeout"}),2e4)}))])];case 2:return[2,u.sent()];case 3:return a=u.sent(),logger_1.wsLogger.error(r,{message:"send "+o,metric:{type:o,spanId:s,timeCost:Date.now()-i},error:a}),[3,5];case 4:return this.requestHandlerMap.delete(s),[7];case 5:return[3,7];case 6:logger_1.wsLogger.error(r,{message:"no ws instance",metric:{type:"end",spanId:s}}),u.label=7;case 7:return[2]}}))}))},t.prototype.sendWsHeatBeat=function(){var e;if(null===(e=this._subscribeDeviceIdList)||void 0===e?void 0:e.length){var t=shortid_for_miniprogram_1.default(),r={Action:"AppDeviceTraceHeartBeat",ActionParams:Object.assign({},{Platform:this.sdk._apiPlatform,RequestId:t,AccessToken:this.sdk.loginManager.accessToken,DeviceIds:this._subscribeDeviceIdList})};return this.send("YunApi",r,{reqId:t})}},t.prototype.activePush=function(){var e=this,t=this.sdk.loginManager,r=t.isLogin,s=t.accessToken,i=t.appKey;r&&s&&this._subscribeDeviceIdList&&(this.send("ActivePush",{DeviceIds:this._subscribeDeviceIdList,Platform:"weapp",AccessToken:s,AppKey:i,RegionId:this.sdk._apiRegionId}),this.sendWsHeatBeat(),clearInterval(this._heartBeatTimer),this._heartBeatTimer=setInterval((function(){return e.sendWsHeatBeat()}),this.options.heartbeatInterval))},t}(event_emitter_for_miniprogram_1.default);exports.IotWebsocket=IotWebsocket;
//# sourceMappingURL=IotWebsocket.js.map
{
"name": "qcloud-iotexplorer-appdev-sdk",
"version": "2.3.9",
"version": "2.3.10",
"description": "",

@@ -17,5 +17,5 @@ "main": "lib/index.js",

"dependencies": {
"event-emitter-for-miniprogram": "^0.11.5",
"event-emitter-for-miniprogram": "^0.11.6",
"js-base64": "^3.2.4",
"qcloud-iotexplorer-logger": "^0.0.9",
"qcloud-iotexplorer-logger": "^0.0.10",
"query-string": "^6.9.0",

@@ -28,3 +28,3 @@ "shortid-for-miniprogram": "^2.2.15",

},
"gitHead": "71a6c2d455d06aa0c2f987b969deff52fbab85f9"
"gitHead": "4284f470b86457ab13a09b59ee682c7d33b43baa"
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc