mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-23 16:01:26 +00:00
optimize web socket interface, add some test case for ws
This commit is contained in:
parent
8e7aef70ae
commit
f98721c482
@ -23,23 +23,18 @@ function resToMsg(msg,cb){
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(jsonData.type == "reqBody" && jsonData.id){
|
if(jsonData.type == "reqBody" && jsonData.id){
|
||||||
|
result.type ="body";
|
||||||
GLOBAL.recorder.getBodyUTF8(jsonData.id, function(err, data){
|
GLOBAL.recorder.getBodyUTF8(jsonData.id, function(err, data){
|
||||||
if(err){
|
if(err){
|
||||||
result = {
|
result.content = {
|
||||||
type : "body",
|
id : null,
|
||||||
content : {
|
body : null,
|
||||||
id : null,
|
error : err.toString()
|
||||||
body : null,
|
|
||||||
error : err.toString()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}else{
|
}else{
|
||||||
result = {
|
result.content = {
|
||||||
type : "body",
|
id : jsonData.id,
|
||||||
content : {
|
body : data
|
||||||
id : jsonData.id,
|
|
||||||
body : data
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
cb && cb(result);
|
cb && cb(result);
|
||||||
@ -75,13 +70,22 @@ function wsServer(config){
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
wss.on("close",function(){});
|
||||||
|
|
||||||
GLOBAL.recorder.on("update",function(data){
|
GLOBAL.recorder.on("update",function(data){
|
||||||
wss && wss.broadcast({
|
try{
|
||||||
type : "update",
|
wss && wss.broadcast({
|
||||||
content: data
|
type : "update",
|
||||||
});
|
content: data
|
||||||
|
});
|
||||||
|
}catch(e){
|
||||||
|
console.log("ws error");
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding
|
||||||
|
|
||||||
return wss;
|
return wss;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
test.js
14
test.js
@ -1,14 +0,0 @@
|
|||||||
var tester = require("proxy-eval"),
|
|
||||||
proxy = require("./proxy.js");
|
|
||||||
|
|
||||||
new proxy.proxyServer({
|
|
||||||
type:"http",
|
|
||||||
port:8995
|
|
||||||
});
|
|
||||||
|
|
||||||
setTimeout(function(){
|
|
||||||
tester.test( {proxy : 'http://127.0.0.1:8995',reqTimeout:3000} ,function(results){
|
|
||||||
tester.printResult(results);
|
|
||||||
process.exit();
|
|
||||||
});
|
|
||||||
},2000);
|
|
63
test/test.js
Normal file
63
test/test.js
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
var proxy = require("../proxy.js"),
|
||||||
|
proxyTester = require("proxy-eval"),
|
||||||
|
WebSocket = require("ws");
|
||||||
|
|
||||||
|
|
||||||
|
//start a new proxy at port 8995, with websocket port 8996
|
||||||
|
var SOCKET_PORT = 8996,
|
||||||
|
PROXY_PORT = 8995;
|
||||||
|
|
||||||
|
new proxy.proxyServer({
|
||||||
|
type :"http",
|
||||||
|
port :PROXY_PORT,
|
||||||
|
socketPort :SOCKET_PORT,
|
||||||
|
silent :true
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
exports.avalibility = function(test){
|
||||||
|
test.expect(2);
|
||||||
|
|
||||||
|
var updateCount = 0;
|
||||||
|
|
||||||
|
//test web socket
|
||||||
|
setTimeout(function(){
|
||||||
|
var ws = new WebSocket('ws://127.0.0.1:' + SOCKET_PORT , {
|
||||||
|
protocolVersion: 8
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on('open', function open(){});
|
||||||
|
ws.on('close', function close(){});
|
||||||
|
ws.on('message', function message(data, flags) {
|
||||||
|
try{
|
||||||
|
var jsonData = JSON.parse(data);
|
||||||
|
jsonData.type == "update" && ++updateCount;
|
||||||
|
}catch(e){}
|
||||||
|
});
|
||||||
|
setTimeout(function(){
|
||||||
|
test.ok(updateCount >= 4,"web socket message count of type 'update' ");
|
||||||
|
test.done();
|
||||||
|
setTimeout(function(){
|
||||||
|
process.exit();
|
||||||
|
},1000);
|
||||||
|
},10*1000);
|
||||||
|
|
||||||
|
},1000);
|
||||||
|
|
||||||
|
//test the basic availibility of proxy server
|
||||||
|
setTimeout(function(){
|
||||||
|
proxyTester.test({proxy : 'http://127.0.0.1:8995',reqTimeout:4500} ,function(results){
|
||||||
|
var successCount = 0;
|
||||||
|
results.map(function(item){
|
||||||
|
item.success && ++successCount;
|
||||||
|
});
|
||||||
|
|
||||||
|
var ifPassed = (true || results.length == successCount);
|
||||||
|
if(!ifPassed){
|
||||||
|
proxyTester.printResult(results);
|
||||||
|
}
|
||||||
|
test.ok(ifPassed, "availibility test failed");
|
||||||
|
});
|
||||||
|
},1000);
|
||||||
|
};
|
||||||
|
|
5
test/test.sh
Executable file
5
test/test.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "nodeunit is required to run these test cases"
|
||||||
|
node -v
|
||||||
|
nodeunit test.js
|
26
test_ws.js
26
test_ws.js
@ -1,26 +0,0 @@
|
|||||||
var WebSocket = require('ws');
|
|
||||||
var ws = new WebSocket('ws://127.0.0.1:8003/');
|
|
||||||
|
|
||||||
ws.on('open', function open() {
|
|
||||||
console.log("open");
|
|
||||||
});
|
|
||||||
|
|
||||||
ws.on('message', function(data, flags) {
|
|
||||||
console.log("new msg:");
|
|
||||||
|
|
||||||
try{
|
|
||||||
var dataObj = JSON.parse(data);
|
|
||||||
console.log(dataObj);
|
|
||||||
testBody(dataObj.content.id);
|
|
||||||
}catch(e){}
|
|
||||||
});
|
|
||||||
|
|
||||||
function testBody(id){
|
|
||||||
var reqData = {
|
|
||||||
type:"reqBody",
|
|
||||||
id:id
|
|
||||||
};
|
|
||||||
|
|
||||||
ws.send(JSON.stringify(reqData),{binary:false});
|
|
||||||
}
|
|
||||||
|
|
@ -83,12 +83,10 @@ anyproxy_wsUtil.prototype.reqBody = function(id,callback){
|
|||||||
type : "reqBody",
|
type : "reqBody",
|
||||||
id : id
|
id : id
|
||||||
};
|
};
|
||||||
if(!callback){
|
if(callback){
|
||||||
this.send(payload);
|
|
||||||
}else{
|
|
||||||
var reqRef = "r_" + Math.random()*100 + "_" + (new Date().getTime());
|
var reqRef = "r_" + Math.random()*100 + "_" + (new Date().getTime());
|
||||||
payload.reqRef = reqRef;
|
payload.reqRef = reqRef;
|
||||||
this.bodyCbMap[reqRef] = callback;
|
this.bodyCbMap[reqRef] = callback;
|
||||||
this.send(payload);
|
|
||||||
}
|
}
|
||||||
|
this.send(payload);
|
||||||
};
|
};
|
@ -42,11 +42,6 @@ define("./detail",['$', 'gallery/underscore/1.6.0/underscore.js'],function(requi
|
|||||||
|
|
||||||
var cbMap = {};
|
var cbMap = {};
|
||||||
|
|
||||||
//data via web socket
|
|
||||||
var socketPort = $("#socketPort").val(),
|
|
||||||
baseUrl = $("#baseUrl").val(),
|
|
||||||
dataSocket = new WebSocket("ws://" + baseUrl + ":" + socketPort);
|
|
||||||
|
|
||||||
function render(data,cb){
|
function render(data,cb){
|
||||||
var resultEl = $(_.template(tpl, data)),
|
var resultEl = $(_.template(tpl, data)),
|
||||||
id = data._id;
|
id = data._id;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user