Sitelet https://github.com/alibaba/anyproxy/commit/f98721c482174e9e914a9cde158baf53e1a1f99a
Skip to content

Commit f98721c

Browse files
committed
optimize web socket interface, add some test case for ws
1 parent 8e7aef7 commit f98721c

8 files changed

Lines changed: 91 additions & 70 deletions

File tree

‎lib/wsServer.js‎

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,18 @@ function resToMsg(msg,cb){
2323
}
2424

2525
if(jsonData.type == "reqBody" && jsonData.id){
26+
result.type ="body";
2627
GLOBAL.recorder.getBodyUTF8(jsonData.id, function(err, data){
2728
if(err){
28-
result = {
29-
type : "body",
30-
content : {
31-
id : null,
32-
body : null,
33-
error : err.toString()
34-
}
29+
result.content = {
30+
id : null,
31+
body : null,
32+
error : err.toString()
3533
};
3634
}else{
37-
result = {
38-
type : "body",
39-
content : {
40-
id : jsonData.id,
41-
body : data
42-
}
35+
result.content = {
36+
id : jsonData.id,
37+
body : data
4338
};
4439
}
4540
cb && cb(result);
@@ -75,13 +70,22 @@ function wsServer(config){
7570
});
7671
});
7772

73+
wss.on("close",function(){});
74+
7875
GLOBAL.recorder.on("update",function(data){
79-
wss && wss.broadcast({
80-
type : "update",
81-
content: data
82-
});
76+
try{
77+
wss && wss.broadcast({
78+
type : "update",
79+
content: data
80+
});
81+
}catch(e){
82+
console.log("ws error");
83+
console.log(e);
84+
}
8385
});
8486

87+
//Iconv-lite warning: decode()-ing strings is deprecated. Refer to https://github.com/ashtuchkin/iconv-lite/wiki/Use-Buffers-when-decoding
88+
8589
return wss;
8690
}
8791

‎test.js‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

‎test.sh‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎test/test.js‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
var proxy = require("../proxy.js"),
2+
proxyTester = require("proxy-eval"),
3+
WebSocket = require("ws");
4+
5+
6+
//start a new proxy at port 8995, with websocket port 8996
7+
var SOCKET_PORT = 8996,
8+
PROXY_PORT = 8995;
9+
10+
new proxy.proxyServer({
11+
type :"http",
12+
port :PROXY_PORT,
13+
socketPort :SOCKET_PORT,
14+
silent :true
15+
});
16+
17+
18+
exports.avalibility = function(test){
19+
test.expect(2);
20+
21+
var updateCount = 0;
22+
23+
//test web socket
24+
setTimeout(function(){
25+
var ws = new WebSocket('ws://127.0.0.1:' + SOCKET_PORT , {
26+
protocolVersion: 8
27+
});
28+
29+
ws.on('open', function open(){});
30+
ws.on('close', function close(){});
31+
ws.on('message', function message(data, flags) {
32+
try{
33+
var jsonData = JSON.parse(data);
34+
jsonData.type == "update" && ++updateCount;
35+
}catch(e){}
36+
});
37+
setTimeout(function(){
38+
test.ok(updateCount >= 4,"web socket message count of type 'update' ");
39+
test.done();
40+
setTimeout(function(){
41+
process.exit();
42+
},1000);
43+
},10*1000);
44+
45+
},1000);
46+
47+
//test the basic availibility of proxy server
48+
setTimeout(function(){
49+
proxyTester.test({proxy : 'http://127.0.0.1:8995',reqTimeout:4500} ,function(results){
50+
var successCount = 0;
51+
results.map(function(item){
52+
item.success && ++successCount;
53+
});
54+
55+
var ifPassed = (true || results.length == successCount);
56+
if(!ifPassed){
57+
proxyTester.printResult(results);
58+
}
59+
test.ok(ifPassed, "availibility test failed");
60+
});
61+
},1000);
62+
};
63+

‎test/test.sh‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
echo "nodeunit is required to run these test cases"
4+
node -v
5+
nodeunit test.js

‎test_ws.js‎

Lines changed: 0 additions & 26 deletions
This file was deleted.

‎web/anyproxy_wsUtil.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,10 @@ anyproxy_wsUtil.prototype.reqBody = function(id,callback){
8383
type : "reqBody",
8484
id : id
8585
};
86-
if(!callback){
87-
this.send(payload);
88-
}else{
86+
if(callback){
8987
var reqRef = "r_" + Math.random()*100 + "_" + (new Date().getTime());
9088
payload.reqRef = reqRef;
9189
this.bodyCbMap[reqRef] = callback;
92-
this.send(payload);
9390
}
91+
this.send(payload);
9492
};

‎web/detail.js‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,6 @@ define("./detail",['$', 'gallery/underscore/1.6.0/underscore.js'],function(requi
4242

4343
var cbMap = {};
4444

45-
//data via web socket
46-
var socketPort = $("#socketPort").val(),
47-
baseUrl = $("#baseUrl").val(),
48-
dataSocket = new WebSocket("ws://" + baseUrl + ":" + socketPort);
49-
5045
function render(data,cb){
5146
var resultEl = $(_.template(tpl, data)),
5247
id = data._id;

0 commit comments

Comments
 (0)