欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

NodeJs 模仿SIP話機(jī)注冊(cè)的方法

 更新時(shí)間:2019年06月21日 09:26:42   作者:賣姑娘的小蘑菇  
這篇文章主要介紹了NodeJs 模仿SIP話機(jī)注冊(cè)的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

本項(xiàng)目需要對(duì)應(yīng)的后端接口、信令環(huán)境才能正常運(yùn)行,本文章只涉及前端內(nèi)容。

項(xiàng)目依賴模塊:

  • NodeJs
  • readline:命令行輸入
  • ws:與服務(wù)端建立websocket連接
  • superagent:與服務(wù)端建立請(qǐng)求連接,效果類似ajax請(qǐng)求
  • tsk_md5:項(xiàng)目登錄密碼使用MD5加密

項(xiàng)目需求

模擬SIP話機(jī)頻繁向服務(wù)器發(fā)起注冊(cè)請(qǐng)求,以得到服務(wù)器最大SIP注冊(cè)數(shù)

項(xiàng)目實(shí)現(xiàn)概述

  1. 終端輸入連續(xù)注冊(cè)分機(jī)的開始分機(jī)號(hào)和結(jié)束分機(jī)號(hào)
  2. 終端輸入統(tǒng)一的SIP注冊(cè)密碼
  3. 終端輸入服務(wù)器地址
  4. 先進(jìn)行用戶登錄鑒權(quán),用戶登錄鑒權(quán)通過后再發(fā)起SIP注冊(cè)

代碼分析

1. 引入項(xiàng)目所需模塊

var WebSocket = require('ws'),
 superagent = require('superagent'),
 tskMD5 = require('./tsk_md5')
 const readline = require('readline');

2. 創(chuàng)建readline 接口實(shí)例

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: 'OHAI> '
 });

3. 定義所需變量

var obj = {}, httpHeader = {}, baseUrl ='', pass = '', ip = '', websocketUrl = ''
var keepWsAlive, readyState

4. 讀取readline 輸入信息函數(shù)

function getReadline() {
 const lines = []; // 用于保存所有輸入信息。
 console.log('Please input the range of extensions(eg: 1001,1010):\n')
 rl.on("line", function(line) {
  if (line ==='') {
   console.log('The input is empty, please input again:\n')
  } else {
   lines.push(line);
   if (lines.length === 1) {
    obj.extensionsArr = line.split(',');
    console.log('Please input the password(eg:1234aa):\n')
   } else if (lines.length === 2) {
    obj.password = line;
    pass = line;
    console.log('Please input the ip(eg:192.168.124.125):\n')
   } else if (lines.length === 3) {
    websocketUrl = 'ws://' + line + ':8089/ws';
    obj.websocketUrl = websocketUrl;
    obj.ip = line;
    ip = line;
    console.log('Starting register...\n');
    // 開始注冊(cè)事件
    loopRegister(obj) 
   }
  }
 });
 
 // close事件監(jiān)聽
 rl.on("close", function(){
  // 結(jié)束程序
  process.exit(0);
 });
 }

終端運(yùn)行截圖

 

5.注冊(cè)事件中包含幾個(gè)動(dòng)作

1)設(shè)置httpHeader:瀏覽器與服務(wù)器ajax請(qǐng)求有固定的請(qǐng)求頭信息,此處模擬瀏覽器的請(qǐng)求頭信息。

用于后續(xù)發(fā)送請(qǐng)求進(jìn)行用戶登錄鑒權(quán)。

function setHttpHeader(username) {
  httpHeader = {
   Accept:'application/json, text/javascript, */*; q=0.01',
   'Accept-Encoding': 'gzip, deflate',
   'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,pt;q=0.7',
   'Cache-Control': 'no-cache',
   Connection: 'keep-alive',
   'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
   Cookie: 'TRACKID='+trackid+'; session-identify=sid121076289-1520217430; username=admin; user_id=0',
   Host: ip +':8089',
   Origin: 'http://'+ip+':8089',
   Pragma: 'no-cache',
   Referer: 'http://'+ip+':8089/gswave/',
   'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36 SE 2.X MetaSr 1.0',
   'X-Requested-With':'XMLHttpRequest'
  }
  var accountData = {
   action:'challenge',
   user:username
  }
  baseUrl = 'http://'+ip+':8089/webrtccgi?';
  getChanllenge(accountData, username) // 用戶鑒權(quán)函數(shù)
 }

2)用戶登錄鑒權(quán)(本項(xiàng)目中與服務(wù)器交互需要使用,具體使用看服務(wù)器端設(shè)置)

<!--賬號(hào)密碼 鑒權(quán)-->
function getChanllenge(accountData, username) {
  console.log('start getChangllenge')
  var challenge = ''
  //獲取challenge
   superagent
    .post(baseUrl)
    .set(httpHeader)
    .type('form')
    .send(accountData)
    .redirects(0)
    .end(function(err, result) {
     if (typeof(result) == 'undefined') {
      console.error("get challenge is error, result is undefined");
     } else {
      var responce = result.body
      if(responce && responce.status === 0) {
       challenge = responce.response.challenge
       getCookie(challenge, username)
      } else {
       console.error('get challenge is error, result.body.status is not 0')
      }
     }
    });
 }

 <!--cookie 鑒權(quán)-->
 
 function getCookie(challenge, username) {
  var md5key = tskMD5.MD5.hexdigest(challenge + pass) // MD5加密用戶登錄密碼
  var subData={
   token: md5key,
   action: 'login',
   user: username
  }
  // 開始請(qǐng)求進(jìn)行用戶登錄密碼鑒權(quán),類似ajax請(qǐng)求
  superagent
   .post(baseUrl)
   .set(httpHeader)
   .type('form')
   .send(subData)
   .redirects(0)
   .end(function(err, res) {
    if (typeof(res) == 'undefined') {
     console.log("get cookie is error, result is undefined");
    } else {
     var responce = res.body
     if(responce && responce.status === 0) {
     // 登錄鑒權(quán)通過,開始執(zhí)行SIP注冊(cè)
      var cookie = responce.response.cookie
      // 注冊(cè)函數(shù)
      startSocket(username)
     } else {
      console.log('get cookie is error, result.body.status is not 0')
     }
    }
   })
 }

與服務(wù)器建立websocket連接

項(xiàng)目中信令交互信息均通過websocket發(fā)送

var ws = new WebSocket(websocketUrl, "sip"); # 注意建立的是sip類型的websocket

 ws.on('open', function open() {
  console.log('ws open message1' + message1)
  readyState = WebSocket.OPEN
  // 發(fā)送相關(guān)信息
  ws.send(message); 
 });
 
 ws.on('message', function incoming(data) {
  a++;
  var dataArr = data.split('\r\n')
  if (dataArr[0].indexOf('401') > -1 && a === 1) {
   // 發(fā)送注冊(cè)信令函數(shù)(其中發(fā)送信令信息,均參考瀏覽器的發(fā)送頭進(jìn)行拼接)
   startRegister(ws, dataArr, username)
  } else if (dataArr[0].indexOf('200')) {
   // ws.close()
   // console.log('register sucess...')
  } else {
  }
 });

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • node.js中的buffer.toString方法使用說明

    node.js中的buffer.toString方法使用說明

    這篇文章主要介紹了node.js中的buffer.toString方法使用說明,本文介紹了buffer.toString的方法說明、語法、接收參數(shù)、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • 使用Nodejs編寫一個(gè)腳本實(shí)現(xiàn)markdown轉(zhuǎn)pdf功能

    使用Nodejs編寫一個(gè)腳本實(shí)現(xiàn)markdown轉(zhuǎn)pdf功能

    Markdown?是一種輕量級(jí)的標(biāo)記語言,非常適合用來寫作和記錄,將?Markdown?轉(zhuǎn)換為?PDF?可以讓文檔在格式和樣式上更加統(tǒng)一,也方便在不同設(shè)備和平臺(tái)上查看和打印,在接下來的內(nèi)容中我們將講解如何使用?NodeJs?編寫一個(gè)?Markdown?轉(zhuǎn)?PDF?的腳本來實(shí)現(xiàn)我們這個(gè)想要的功能
    2024-05-05
  • node.js express安裝及示例網(wǎng)站搭建方法(分享)

    node.js express安裝及示例網(wǎng)站搭建方法(分享)

    下面小編就為大家?guī)硪黄猲ode.js express安裝及示例網(wǎng)站搭建方法(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • 淺談node.js中間件有哪些類型

    淺談node.js中間件有哪些類型

    這篇文章主要介紹了node.js中間件有哪些類型,對(duì)中間件感興趣的同學(xué),可以參考下
    2021-04-04
  • 輕松創(chuàng)建nodejs服務(wù)器(1):一個(gè)簡(jiǎn)單nodejs服務(wù)器例子

    輕松創(chuàng)建nodejs服務(wù)器(1):一個(gè)簡(jiǎn)單nodejs服務(wù)器例子

    這篇文章主要介紹了一個(gè)簡(jiǎn)單nodejs服務(wù)器例子,本文實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的hello world例子,并展示如何運(yùn)行這個(gè)服務(wù)器,需要的朋友可以參考下
    2014-12-12
  • Nodejs中使用puppeteer控制瀏覽器中視頻播放功能

    Nodejs中使用puppeteer控制瀏覽器中視頻播放功能

    本項(xiàng)目主要功能為在瀏覽器中自動(dòng)播放視頻,并且實(shí)現(xiàn)音量控制,快進(jìn)快退,全屏控制,播放暫??刂频裙δ?。對(duì)Nodejs中使用puppeteer控制瀏覽器中視頻播放功能感興趣的朋友跟隨小編一起看看吧
    2019-08-08
  • 使用?Node.js和Express搭建服務(wù)器的過程步驟詳解

    使用?Node.js和Express搭建服務(wù)器的過程步驟詳解

    Node.js?是一個(gè)開源、跨平臺(tái)的?JavaScript?運(yùn)行時(shí)環(huán)境,這篇文章主要介紹了如何使用?Node.js和Express搭建服務(wù)器,需要的朋友可以參考下
    2023-09-09
  • node.js中的path.normalize方法使用說明

    node.js中的path.normalize方法使用說明

    這篇文章主要介紹了node.js中的path.normalize方法使用說明,本文介紹了path.normalize的方法說明、語法、使用實(shí)例和實(shí)現(xiàn)源碼,需要的朋友可以參考下
    2014-12-12
  • Node.js 深度調(diào)試方法解析

    Node.js 深度調(diào)試方法解析

    這篇文章主要介紹了Node.js 深度調(diào)試方法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Node.js上傳文件功能之服務(wù)端如何獲取文件上傳進(jìn)度

    Node.js上傳文件功能之服務(wù)端如何獲取文件上傳進(jìn)度

    這篇文章主要介紹如何利用progress-stream獲取文件上傳進(jìn)度,以及該組件使用過程中的注意事項(xiàng)
    2018-02-02

最新評(píng)論