Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

NtvAuthModule.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * 信令认证模块,负责对连接信令的用户进行认证和状态记录
  3. * 终端---->信令(加载本模块)---->业务系统
  4. * Created by WJ on 2018-12-06
  5. * @云视睿博 www.ruiboyun.com
  6. */
  7. const http = require('http');
  8. const qs = require('querystring');
  9. const url = require("url");
  10. const request = require('request');
  11. class NtvAuthModule {
  12. constructor(config) {
  13. this.config = config;
  14. }
  15. errorMsg(error){
  16. return {code:5,err_desc:error};
  17. }
  18. /**
  19. * 解析接口返回内容
  20. * 并回调,回调传输人的第二个参数是一个对象,格式同接口返回格式。
  21. * @param {*} data
  22. * @param {*} callback 参数为json对象,结构同ntv g3接口规范
  23. */
  24. parseData(data,callback) {
  25. if(typeof(callback)!="function"){
  26. console.log("auth module needs callback function");
  27. return;
  28. }
  29. var obj = null;
  30. try {
  31. obj = JSON.parse(data);
  32. if(!"code" in obj){
  33. obj.code = 5;
  34. obj.err_desc = "接口返回数据内容不可理解!";
  35. }
  36. } catch(error) {
  37. obj = this.errorMsg("解析接口返回数据失败!")
  38. }
  39. callback(obj);
  40. }
  41. requestCall(url,callback){
  42. var that = this;
  43. request(url, function(error, response, body) {
  44. if (!error && response.statusCode == 200) {
  45. that.parseData(body,callback);
  46. }else{
  47. callback(that.errorMsg('无法连接业务服务器!'));
  48. }
  49. });
  50. }
  51. /**
  52. * 请求远程接口
  53. * @param {接口的action} action
  54. * @param {参数} data 参数键值对 对象或字符串
  55. * @param {回调} callback
  56. */
  57. postCall(host,port,path,data,callback) {
  58. var returnData = '';
  59. var content = '';
  60. if(typeof(data)=="object"){
  61. content = qs.stringify(data);
  62. }else{
  63. content = data;
  64. }
  65. var options = {
  66. hostname: host,
  67. port: port,
  68. path: path,
  69. method: 'POST',
  70. timeout: 2000,
  71. headers: {
  72. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  73. }
  74. };
  75. var that = this;
  76. var req = http.request(options, function(res) {
  77. res.setEncoding('utf8');
  78. res.on('data', function(chunk) {
  79. returnData = returnData + chunk;
  80. });
  81. res.on('end', () => {
  82. that.parseData(returnData,callback);
  83. });
  84. });
  85. req.on('error', function(e) {
  86. if(typeof(callback)=="function"){
  87. callback(that.errorMsg('无法连接业务服务器!'));
  88. }
  89. });
  90. req.write(content);
  91. req.end();
  92. }
  93. auth(usrId,callback){
  94. var config = this.config;
  95. var path = "/api/sipMgr/";
  96. var paras= "action=sip_auth&id=" + usrId;
  97. var host = config.host;
  98. var port = config.port;
  99. this.postCall(host,port,path,paras,callback);
  100. }
  101. /**
  102. *
  103. * @param {*} usrId
  104. * @param {*} status 1 = online
  105. * @param {string} channels id=name|id=name
  106. * @param {*} callback
  107. */
  108. status(usrId,status,channels,callback){
  109. var config = this.config;
  110. var path = "/api/sipMgr/";
  111. var paras= "action=sip_status&id=" + usrId +"&status=" + status + "&chn=" + encodeURIComponent(channels);
  112. var host = config.host;
  113. var port = config.port;
  114. this.postCall(host,port,path,paras,callback);
  115. }
  116. /**
  117. *
  118. * @param {*} usrId
  119. * @param {*} status 1 = online
  120. * @param {*} callback
  121. */
  122. play_status(usrId,status,callback){
  123. var config = this.config;
  124. var path = "/api/sipMgr/";
  125. var paras= "action=play_status&id=" + usrId +"&status=" + status;
  126. var host = config.host;
  127. var port = config.port;
  128. this.postCall(host,port,path,paras,callback);
  129. }
  130. /**
  131. * 废弃
  132. * 调用RTP_SERVER接口,启动rtmp推流
  133. * http://__RTP_SERVER/index/api/addStreamPusherProxy?secret=035c73f7-bb6b-4889-a715-d9eb2d1925cc&schema=rtmp&vhost=__defaultVhost__&app=rtp
  134. * &stream=__STREAM&dst_url=rtmp://__RTMP_SERVER/show2/__STREAM
  135. */
  136. start_rtmp(api,stream,callback){
  137. stream = stream.toLocaleUpperCase();
  138. api = api.replace(/__STREAM/g, stream);
  139. console.log("query rtmp push api : "+ api);
  140. this.requestCall(api,callback);
  141. }
  142. }
  143. module.exports = NtvAuthModule;