Does WebSocket.send() copy the passed array buffer so that it is safe to modify it afterward? Say:
var a = new Uint8Array([1, 2, 3]);
websocket.send(a);
a[2] = 4;
Is this safe even if the socket is full and the buffer isn't sent immediately?
Does WebSocket.send() copy the passed array buffer so that it is safe to modify it afterward? Say:
var a = new Uint8Array([1, 2, 3]);
websocket.send(a);
a[2] = 4;
Is this safe even if the socket is full and the buffer isn't sent immediately?
Yes, it is safe to modify your buffer after
WebSocket.send()
.The working draft of the WebSocket specification refers to a separate buffer for the socket so the data is either immediately sent or copied into a separate buffer.
Note, that if the socket buffer is full, then that triggers an error condition and the socket is closed.
In addition, this makes sense from an implementation point of view. Exactly when the bytes are actually sent over the network is a detail of the socket implementation and thus, they have to copy the bytes into their own buffer in order to maintain control over the data before it is sent (a safe programming practice for a design like this). Otherwise, there could be horrible timing issues with resue of the buffer by the calling code.