Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell the type of websocket onmessage's parameter?

Here https://developer.mozilla.org/en/WebSockets/WebSockets_reference/MessageEvent it states attribute data is of type DOMString| Blob | ArrayBuffer. How do I tell it which type I want? Or how do I know which type I get?

like image 899
marc40000 Avatar asked Feb 27 '12 09:02

marc40000


People also ask

What is Socket OnMessage?

The WebSocket. onmessage property is an event handler that is called when a message is received from the server. It is called with a MessageEvent .

When using WebSocket send () How do you know?

The only way to know the client received the webSocket message for sure is to have the client send your own custom message back to the server to indicate you received it and for you to wait for that message on the server. That's the ONLY end-to-end test that is guaranteed.

How do I read WebSocket messages?

First, you need to copy your web browser's header to here and use json. dumps to convert it into the string format. After that, create the connection to the server by using create_connection . Then, perform the handshake by sending the message, and you will be able to see the data on your side.

How would you handle the reception of data from a WebSocket?

The best method is to parse using third party library like jQuery. In both XML and JSON, the server responds as a string, which is being parsed at the client end.


1 Answers

The appropriate two types of frames that a server can send are text frames and binary frames (5.2). The ws.binaryType allows you to define in which format you'd like to obtain the binary data.

  • Binary data: depending on binaryType being set to either arraybuffer or blob
  • Text data: string

To determine the type, you can use:

  • e.data instanceof ArrayBuffer
  • e.data instanceof Blob
  • typeof e.data === "string"

Reference:

4. If type indicates that the data is Text, then initialize event's data attribute to data.

If type indicates that the data is Binary, and binaryType is set to "blob", then initialize event's data attribute to a new Blob object that represents data as its raw data.

If type indicates that the data is Binary, and binaryType is set to "arraybuffer", then initialize event's data attribute to a new read-only ArrayBuffer object whose contents are data.

like image 110
pimvdb Avatar answered Sep 21 '22 19:09

pimvdb