我正在连接到Web Worker中的第三方Websocket连接。我收到的消息是base64编码和GZipped。请参阅示例消息
var receivedMsg = 'XYw7DoAgEAXv8uqVgHwiW4qtJgoWamy9BOHuop1OM81kMkYw1jg0fQogTODOak/YwUdG2sCKsICddVZ4I18IM1gKWeiTmNYIpX/JSYj1VXU9Kjc=';
最终输出应如下图所示
{
MarketName : string,
Nonce : int,
Buys:
[
{
Quantity : decimal,
Rate : decimal
}
],
Sells:
[
{
Quantity : decimal,
Rate : decimal
}
],
Fills:
[
{
Id : int,
TimeStamp : date,
Quantity : decimal,
Price : decimal,
Total : decimal,
FillType : string,
OrderType : string
}
]
}
现在,我尝试使用pako和atob库来模拟示例here,但出现错误
PS:请记住,网络工作者无法访问窗口,因此无法使用本机浏览器atob
// Decode base64 (convert ascii to binary)
const strData = new Buffer(receivedMsg , 'base64').toString('binary');
// Convert binary string to character-number array
const charData = strData.split('').map(function(x){return x.charCodeAt(0);});
// Turn number array into byte-array
var binData = new Uint8Array(charData);
// Pako magic
var data = pako.inflate(binData);
console.log(strData);
错误消息显示为 标头文字不正确
在尝试失败之后,我什至尝试了this,但这仍然无济于事
More References Third Party Websocket Background - not sure if this will help