我有一个基于AngularJS的SPA,它可以通过websocket向服务器发送ping请求。我想从详细的响应中解析来自服务器的ping响应。即我想知道ping是成功还是失败,每次成功/失败的计数(我只想在下面的响应中以粗体显示信息)。还有一件事,我想要实现的是,如果失败,显示一些RED灯,其中包括失败的ping /已发送的总数,并且在成功的情况下显示绿灯,其中包含成功ping /已发送总数的数量。 请查看pingController.js和pingController.html。 这是我的插件。Send ping command to server
<div ng-repeat="message in messages track by $index ">
{{message}}
</div>
我相信在经过几个链接后我创建并尝试了angularJS的过滤器,我还没有想出正确的方法。
SmartCartApp.filter('parseResponse', function () {
// function to invoke by Angular each time
// Angular passes in the `items` which is our Array
return function (messages) {
// Create a new Array
var filtered = [];
// loop through existing Array
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
var res = message.match(/Success/g); //this filters messages which has Success string in it. Looks like I need to create a multiple of those to just get Success/Failure
if(res !==null)
{
filtered.push(message);
}
}
return filtered;
};
});
服务器根据SPA的成功/失败发送响应。 以下是回复的样子。
失败:
NIC [;] 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000 [,] 0,0,0,0,0,0 [,] 0.000000,0.000000,0.000000,0.000000,0.001479,0.000000 [,] 0 ,0,0,0,0,0 [/] 03/06/2017 17:25.0251 [;] Ping [;]状态[;] Ping到www.google.com :已解决的地址:216.239.50.80 已发送总数:22总计成功:0总错误:22 总超时:0最小RTT(ms):0最大RTT(ms):0平均RTT(ms):0.00 [/] 03/06/2017:28.0251 [;] Ping [;] Ping结果[;] www.google.com [,] False [,] 10 [,] 1 [,] 3 [,] 0 [,] 216.239.50.80 [,] TtlExpired [,]真[,] TtlExpired [/] 03/06/2017:28.0813 [;] Ping [;]运行[;] FALSE [/] 03/06/2017:28.1874 [;] Traffic [;] Total Traffic [;] 0.002960 [,] 0 [,] 0.003569 [,] 0 [,] 2.057E + 09 [,] ALL [/] < / p>
成功: 03/06/2017 17:48.2265 [;] Traffic [;] Traffic by NIC [;] 0.000000,0.000000,0.000000,0.000000,0.000000,0.000000 [,] 0,0,0,0,0,0 [, ] 0.000000,0.000000,0.000000,0.000000,0.000732,0.000000 [,] 0,0,0,0,0,0 [/] 03/06/2017 17:48:30 [[]] Ping [;]状态[;] Ping到www.ibm.com :已解决的地址:173.223.238.158 已发送总数:9总计成功:9总错误:0 总超时:0最小RTT(ms):8最大RTT(ms):14平均RTT(ms):10.33 [/] 03/06/2017 17:48:30 [[]] Ping [;] Ping结果[;] www.ibm.com [,] False [,] 10 [,] 1 [,] 3 [,] 9 [,] 173.223.238.158 [,]成功[,]假[,]无[/] 03/06/2017:46.2251 [;]流量[;]总流量[;] 0.002205 [,] 0 [,] 0.005805 [,] 0 [,] 2.057E + 09 [,]所有[/] 03/06/2017 17:48:46.2251 [;] Traffic [;] Traffic by NIC [;] 0.000000,0.000000,0.000000,0.000000,0.002205,0.000000 [,] 0,0,0,0,0,0 [, ] 0.000000,0.000000,0.000000,0.000000,0.005805,0.000000 [,] 0,0,0,0,0,0 [/] 03/06/2017 17:48:47.2304 [;] Traffic [;] Total Traffic [;] 0.000867 [,] 0 [,] 0.002704 [,] 0 [,] 2.057E + 09 [,] ALL [/] 03/06/2017 17:48:47.2304 [;]交通[;]交通
答案 0 :(得分:0)
我发现这可以通过多种方式完成。 1)最简单的是在JavaScript中使用字符串的match属性。 我还了解到这可以使用角度过滤器或工厂/服务来完成 这是我使用服务实现的。
myApp.service('ParseMessageService', function() {
this.myFunc = function(message) {
var res = message.match(/Success/g);
if (res !== null) {
//console.log(res);
TSent = message.match(/Total Sent: (\d+)/);
if (TSent) {
sent = TSent[1];
//parsedMsg.push(TSent[1]);
//console.log(TSent[1]); //replace with $scope and display on Gui
}
TSucc = message.match(/Total Success: (\d+)/);
if (TSucc) {
succ = TSucc[1];
//parsedMsg.push(TSucc[1]);
// console.log(TSucc[1]); //replace with $scope and display on Gui
}
TErr = message.match(/Total Errors: (\d+)/);
if (TErr) {
err = TErr[1];
//parsedMsg.push(TErr[1]);
//console.log(TErr[1]); //replace with $scope and display on Gui
}
}
}