NodeJs 模仿SIP話機(jī)注冊(cè)的方法
本項(xiàng)目需要對(duì)應(yīng)的后端接口、信令環(huán)境才能正常運(yùn)行,本文章只涉及前端內(nèi)容。
項(xiàng)目依賴(lài)模塊:
- NodeJs
- readline:命令行輸入
- ws:與服務(wù)端建立websocket連接
- superagent:與服務(wù)端建立請(qǐng)求連接,效果類(lèi)似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)概述
- 終端輸入連續(xù)注冊(cè)分機(jī)的開(kāi)始分機(jī)號(hào)和結(jié)束分機(jī)號(hào)
- 終端輸入統(tǒng)一的SIP注冊(cè)密碼
- 終端輸入服務(wù)器地址
- 先進(jìn)行用戶(hù)登錄鑒權(quán),用戶(hù)登錄鑒權(quán)通過(guò)后再發(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');
// 開(kāi)始注冊(cè)事件
loopRegister(obj)
}
}
});
// close事件監(jiān)聽(tīng)
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)行用戶(hù)登錄鑒權(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) // 用戶(hù)鑒權(quán)函數(shù)
}
2)用戶(hù)登錄鑒權(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加密用戶(hù)登錄密碼
var subData={
token: md5key,
action: 'login',
user: username
}
// 開(kāi)始請(qǐng)求進(jìn)行用戶(hù)登錄密碼鑒權(quán),類(lèi)似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)通過(guò),開(kāi)始執(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)目中信令交互信息均通過(guò)websocket發(fā)送
var ws = new WebSocket(websocketUrl, "sip"); # 注意建立的是sip類(lèi)型的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)文章
詳解Koa中更方便簡(jiǎn)單發(fā)送響應(yīng)的方式
這篇文章主要介紹了詳解Koa中更方便簡(jiǎn)單發(fā)送響應(yīng)的方式,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Node.js使用http模塊實(shí)現(xiàn)后臺(tái)服務(wù)器流程解析
這篇文章將會(huì)教會(huì)你前端工程師怎么搭建后臺(tái)服務(wù)器,做自己的后端開(kāi)發(fā),同時(shí),在這篇文章開(kāi)始你就開(kāi)始正式進(jìn)入全棧的道路咯!本片文章將細(xì)解http模塊,在開(kāi)始前我們將復(fù)習(xí)一點(diǎn)計(jì)算機(jī)網(wǎng)絡(luò)的知識(shí)2022-09-09
簡(jiǎn)單了解node npm cnpm的具體使用方法
這篇文章主要介紹了簡(jiǎn)單了解node npm cnpm的具體使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
如何用nodejs給C#寫(xiě)一個(gè)數(shù)據(jù)表的實(shí)體類(lèi)生成工具
這篇文章主要介紹了如何用nodejs給C#寫(xiě)一個(gè)數(shù)據(jù)表的實(shí)體類(lèi)生成工具,對(duì)nodejs感興趣的同學(xué),可以參考下2021-05-05
node中的__filename和__dirname的使用詳解
本文主要介紹了node中的__filename和__dirname的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
nodejs各種姿勢(shì)斷點(diǎn)調(diào)試的方法
這篇文章主要介紹了nodejs各種姿勢(shì)斷點(diǎn)調(diào)試的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06

