我已经在websocket性能测试中使用过K6。而来自服务器的内容是压缩的,我在控制台中打印了"�0E�e�!�56���j0&��v!�{�:�9�^�“。我使用nodejs代码处理来自服务器的相同消息。我收到了正确的短信。
所以我的问题是如何在K6中解码压缩文本?
我的K6脚本是
socket.on('message', function (data) {
console.log(typeof (data));
console.log(data.length)
if (data.length < 200) {
// I got "�0E�e�!�56���j0&��v!�{�:�9�^�" in console
console.log(data);
}
// I tried to decode it got "incorrect header check"
let text = pako.inflate(data, { to: 'string' });
}
如果我使用下面的JS脚本,我可以获得正确的文本,将其膨胀为纯文本。
ws.on('message', function (data) {
console.log('-------- begin -------');
// I got <Buffer 1f 8b 08 00 00 00 00 00 00 00 2d 8b 4b 0a 80 20 14 45 f7 72 c7 21 f9 1b e4 6e 34 1f 14 12 49 5e 47 d1 de 33 68 7a 3e 37 f6 8c 80 c4 b5 b7 4c 4c 68 3d in console
console.log(data);
console.log('-------- end -------');
let text = pako.inflate(data, {
to: 'string'
});
// msg is " msg: {"id":"btcusdt","subbed":"market.btcusdt.depth.step0","ts":1525243319443,"status":"ok"} "
console.log('msg: ' + text);
})
发布于 2018-05-02 15:32:27
正如我在GitHub issue中提到的,当您在选项中指定{ to: 'string' }时,pako的browserify版本可能会有一些问题。相反,您应该能够这样做:
let binText = pako.inflate(data);
let strText = String.fromCharCode.apply(null, binText);
console.log(strText);https://stackoverflow.com/questions/50128949
复制相似问题