Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read BLOB data from a WebSocket which is not an image

I created a WebSocket connection to my webserver to receive some data. However, when I log the event that I receive in the onmessage function, then I cannot see the real content of the data.

When I copy the network connection that my Chrome browser v32 opens as a curl command and run it on my OS console, then everything works fine. So I think that somehow my WebSocket setup must be wrong. The event.data object is an instance of Blob.

Here is my code (actually CoffeeScript, but easy to understand):

socket = new WebSocket "wss://myserverurl/some-endpoint"

socket.onopen = (event) ->
    console.log 'Connection opened (WebSocket)'

socket.onclose = (event) ->
    console.log 'Connection closed (WebSocket)'
    code = event.code
    reason = event.reason
    wasClean = event.wasClean

socket.onmessage = (event) ->
    console.log JSON.stringify event

The event that I get:

{
    "ports": [],
    "data": {
        "type": "",
        "size": 594
    },
    ...
    "cancelBubble": false,
    "returnValue": true,
    "srcElement": {
        "binaryType": "blob",
        "extensions": "",
        "protocol": "",
        "onerror": null,
        "bufferedAmount": 0,
        "readyState": 1
    },
    "defaultPrevented": false,
    "timeStamp": 1390578698613,
    "cancelable": false,
    "bubbles": false,
    "eventPhase": 2,
    "currentTarget": {
        "binaryType": "blob",
        "extensions": "",
        "protocol": "",
        "onerror": null,
        "bufferedAmount": 0,
        "readyState": 1
    },
    "target": {
        "binaryType": "blob",
        "extensions": "",
        "protocol": "",
        "onerror": null,
        "bufferedAmount": 0,
        "readyState": 1
    },
    "type": "message"
}
like image 734
Benny Neugebauer Avatar asked Jan 24 '14 17:01

Benny Neugebauer


People also ask

How do I read a blob value from a string?

CreateImage (string id) is the method that reads the BLOB. The string id parameter gets the image ID that the user selects from the drop down list. To read the BLOB value, use the ExecuteScalar () method of the SqlCommand class. ExecuteScalar () returns an object, so we should cast it and store in a byte array like this.

What is WebSocket protocol and how does it work?

Websocket protocol is used to provide persistent real-time connection. It means that most of the websites use WebSocket to send real-time data from its server to the web so that you are able to view the ever-changing live data. You might ask, what kind of websites normally uses WebSocket?

How to only send string messages in a WebSocket?

you can also use Blob.text (). Basically the best answer I can tell that it's preferred thing to only send string messages in websocket. So the solution is when sending messages I will highly recommend you to use: So, what it does is converting the arrays or objects or any type of data into string

How do I find the URL of a WebSocket socket?

First, you will need to find the WebSocket URI. Here I am using Chrome developer tools to inspect. After open Chrome DevTools, click the WS (Web Socket) tab, then you will be able to find the request URL which is bounded by a purple box above.


1 Answers

You can set tye binaryType of the WebSocket in JavaScript like:

var socket = new WebSocket(url);
socket.binaryType = "arraybuffer";

Or respectively in CoffeeScript

socket = new WebSocket url
socket.binaryType = "arraybuffer"

And you will get ArrayBuffers instead of Blobs. Convert them to Uint8Array by new Uint8Array(event.data);

like image 74
Tamas Hegedus Avatar answered Sep 20 '22 09:09

Tamas Hegedus