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"
}
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.
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?
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
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.
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 ArrayBuffer
s instead of Blob
s. Convert them to Uint8Array
by new Uint8Array(event.data);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With