javascript - When is WebSocket.onmessage triggered? -
(1) open websocket connection.
var ws = new websocket(url);
(2) when connection established, send message trigger blob sent in response.
ws.onopen = function () { ws.send('1000000'); }
(3) onmessage
triggered when response begins or complete?
ws.onmessage = function () { // has entire blob arrived or has download begun? }
the w3c spec websocket api says message
event should dispatched "when websocket message has been received":
when websocket message has been received type type , data data, user agent must queue task follow these steps:
...
- let event event uses
messageevent
interface, event typemessage
, not bubble, not cancelable, , has no default action....
- dispatch event @
websocket
object.
to understand meant "when websocket message has been received," must consult rfc 6455, "the websocket protocol". in websockets, participants send messages contained in 1 or more frames:
after successful handshake, clients , servers transfer data , forth in conceptual units referred in specification "messages". on wire, message composed of 1 or more frames.
once understand that, can understand section 6 of rfc 6455, defines phrase "a websocket message has been received":
if frame comprises unfragmented message (section 5.4), said _a websocket message has been received_ type /type/ , data /data/. if frame part of fragmented message, "application data" of subsequent data frames concatenated form /data/. when last fragment received indicated fin bit (frame-fin), said _a websocket message has been received_ data /data/...
assuming sever-side library's send
method treats argument "message", specification requires client wait receipt of entire message before firing message
event.
a server-side api allowed streaming (e.g., kept message incomplete , withheld fin bit until kind of finish
method called) not cause client fire message
event until message concluded.
Comments
Post a Comment