Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Blob type messages #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion Plugins/WebSocket.jslib
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,31 @@ var LibraryWebSocket = {
if (webSocketState.onMessage === null)
return;

if (ev.data instanceof ArrayBuffer) {
if (ev.data instanceof Blob)
{
var blob = event.data;

//通过FileReader读取数据
var reader = new FileReader();

//以下这两种方式我都可以解析出来,因为Blob对象的数据可以按文本或二进制的格式进行读取
reader.readAsArrayBuffer(blob);
//reader.readAsText(blob, 'utf8');

reader.onload = function () {
var receive_data = this.result;//这个就是解析出来的数据
console.log("[JSLIB WebSocket] message type: ", typeof receive_data, receive_data)
var dataBuffer = new Uint8Array(receive_data);
var buffer = _malloc(dataBuffer.length);
HEAPU8.set(dataBuffer, buffer);
try {
Module.dynCall_viii(webSocketManager.onMessage, instanceId, buffer, dataBuffer.length)
}
finally { _free(buffer) }
}

}
else if (ev.data instanceof ArrayBuffer) {

var dataBuffer = new Uint8Array(ev.data);

Expand Down