内容简介:模拟SIP话机频繁向服务器发起注册请求,以得到服务器最大SIP注册数终端运行截图1)设置httpHeader:浏览器与服务器ajax请求有固定的请求头信息,此处模拟浏览器的请求头信息。
- NodeJs
- readline:命令行输入
- ws:与服务端建立websocket连接
- superagent:与服务端建立请求连接,效果类似ajax请求
- tsk_md5:项目登录密码使用MD5加密
项目需求
模拟SIP话机频繁向服务器发起注册请求,以得到服务器最大SIP注册数
项目实现概述
- 终端输入连续注册分机的开始分机号和结束分机号
- 终端输入统一的SIP注册密码
- 终端输入服务器地址
- 先进行用户登录鉴权,用户登录鉴权通过后再发起SIP注册
代码分析
1. 引入项目所需模块
var WebSocket = require('ws'), superagent = require('superagent'), tskMD5 = require('./tsk_md5') const readline = require('readline'); 复制代码
2. 创建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 输入信息函数
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事件监听 rl.on("close", function(){ // 结束程序 process.exit(0); }); } 复制代码
终端运行截图
5.注册事件中包含几个动作
1)设置httpHeader:浏览器与服务器ajax请求有固定的请求头信息,此处模拟浏览器的请求头信息。
用于后续发送请求进行用户登录鉴权。
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) // 用户鉴权函数 } 复制代码
2)用户登录鉴权(本项目中与服务器交互需要使用,具体使用看服务器端设置)
<!--账号密码 鉴权--> 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 鉴权--> function getCookie(challenge, username) { var md5key = tskMD5.MD5.hexdigest(challenge + pass) // MD5加密用户登录密码 var subData={ token: md5key, action: 'login', user: username } // 开始请求进行用户登录密码鉴权,类似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) { // 登录鉴权通过,开始执行SIP注册 var cookie = responce.response.cookie // 注册函数 startSocket(username) } else { console.log('get cookie is error, result.body.status is not 0') } } }) } 复制代码
-
与服务器建立websocket连接
项目中信令交互信息均通过websocket发送
var ws = new WebSocket(websocketUrl, "sip"); # 注意建立的是sip类型的websocket ws.on('open', function open() { console.log('ws open message1' + message1) readyState = WebSocket.OPEN // 发送相关信息 ws.send(message); }); ws.on('message', function incoming(data) { a++; var dataArr = data.split('\r\n') if (dataArr[0].indexOf('401') > -1 && a === 1) { // 发送注册信令函数(其中发送信令信息,均参考浏览器的发送头进行拼接) startRegister(ws, dataArr, username) } else if (dataArr[0].indexOf('200')) { // ws.close() // console.log('register sucess...') } else { } }); 复制代码
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
C语言名题精选百则技巧篇
冼镜光 / 机械工业出版社 / 2005-7 / 44.00元
《C语言名题精选百则》(技巧篇)收集了100则C语言程序设计题,共分9类。第一类比较简单,主要希望读者了解到《C语言名题精选百则》(技巧篇)的题目、解法与其他书籍之间的差异;第二至六类分别是关于数字、组合数学或离散数学、查找、排序、字符串等方面的题目;第七类列出了一些不太容易归类的题目,如Buffon丢针问题、Dijkstra的三色旗问题等;第八类则收录了一些有趣的、娱乐性的题目,如魔方阵等;第九......一起来看看 《C语言名题精选百则技巧篇》 这本书的介绍吧!