Skip to content

Commit

Permalink
Update API.WebSocket.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Dafrok authored Dec 16, 2020
1 parent c78b76d commit 1c17b9b
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions API.WebSocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,16 @@ const decode = function(blob){
*/
let body = textDecoder.decode(pako.inflate(data));
if (body) {
result.body.push(JSON.parse(body.slice(body.indexOf("{"))));
// 同一条 message 中可能存在多条信息,用正则筛出来
const group = body.split(/[\u0000]*(?:[\u0000]|$)*/);
group.forEach(item => {
try {
result.body.push(JSON.parse(item));
}
catch(e) {
// 忽略非 JSON 字符串,通常情况下为分隔符
}
});
}

offset += packetLen;
Expand Down Expand Up @@ -338,4 +347,4 @@ const decode = function (blob) {
resolve(result)
});
}
```
```

0 comments on commit 1c17b9b

Please sign in to comment.