NodeJs 模仿SIP話機注冊的方法
本項目需要對應(yīng)的后端接口、信令環(huán)境才能正常運行,本文章只涉及前端內(nèi)容。
項目依賴模塊:
- NodeJs
- readline:命令行輸入
- ws:與服務(wù)端建立websocket連接
- superagent:與服務(wù)端建立請求連接,效果類似ajax請求
- tsk_md5:項目登錄密碼使用MD5加密
項目需求
模擬SIP話機頻繁向服務(wù)器發(fā)起注冊請求,以得到服務(wù)器最大SIP注冊數(shù)
項目實現(xiàn)概述
- 終端輸入連續(xù)注冊分機的開始分機號和結(jié)束分機號
- 終端輸入統(tǒng)一的SIP注冊密碼
- 終端輸入服務(wù)器地址
- 先進行用戶登錄鑒權(quán),用戶登錄鑒權(quán)通過后再發(fā)起SIP注冊
代碼分析
1. 引入項目所需模塊
var WebSocket = require('ws'),
superagent = require('superagent'),
tskMD5 = require('./tsk_md5')
const readline = require('readline');
2. 創(chuàng)建readline 接口實例
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');
// 開始注冊事件
loopRegister(obj)
}
}
});
// close事件監(jiān)聽
rl.on("close", function(){
// 結(jié)束程序
process.exit(0);
});
}
終端運行截圖
5.注冊事件中包含幾個動作
1)設(shè)置httpHeader:瀏覽器與服務(wù)器ajax請求有固定的請求頭信息,此處模擬瀏覽器的請求頭信息。
用于后續(xù)發(fā)送請求進行用戶登錄鑒權(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)(本項目中與服務(wù)器交互需要使用,具體使用看服務(wù)器端設(shè)置)
<!--賬號密碼 鑒權(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
}
// 開始請求進行用戶登錄密碼鑒權(quán),類似ajax請求
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注冊
var cookie = responce.response.cookie
// 注冊函數(shù)
startSocket(username)
} else {
console.log('get cookie is error, result.body.status is not 0')
}
}
})
}
與服務(wù)器建立websocket連接
項目中信令交互信息均通過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ā)送注冊信令函數(shù)(其中發(fā)送信令信息,均參考瀏覽器的發(fā)送頭進行拼接)
startRegister(ws, dataArr, username)
} else if (dataArr[0].indexOf('200')) {
// ws.close()
// console.log('register sucess...')
} else {
}
});
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Node.js使用http模塊實現(xiàn)后臺服務(wù)器流程解析
這篇文章將會教會你前端工程師怎么搭建后臺服務(wù)器,做自己的后端開發(fā),同時,在這篇文章開始你就開始正式進入全棧的道路咯!本片文章將細解http模塊,在開始前我們將復(fù)習一點計算機網(wǎng)絡(luò)的知識2022-09-09
如何用nodejs給C#寫一個數(shù)據(jù)表的實體類生成工具
這篇文章主要介紹了如何用nodejs給C#寫一個數(shù)據(jù)表的實體類生成工具,對nodejs感興趣的同學(xué),可以參考下2021-05-05
node中的__filename和__dirname的使用詳解
本文主要介紹了node中的__filename和__dirname的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-03-03

