HTML and CSS Reference
In-Depth Information
We'll learn more about the supported data types in Chapter 4 , Data Transfer - Send-
ing, Receiving, and Decoding
The onclose event marks the end of the conversation. Whenever this event is fired,
no messages can be transferred between the server and the client unless the con-
nection is reopened. A connection might be terminated due to a number of reasons.
It can be closed by the server, it may be closed by the client using the close()
method, or due to TCP errors.
You can easily detect the reason the connection was closed by checking the code ,
reason , and wasClean parameters of the event.
The code parameter provides you with a unique number indicating the origin of the
The reason parameter provides the description of the interruption in a string format.
Finally, the wasClean parameter indicates whether the connection was closed due
to a server decision or due to unexpected network behavior. The following code snip-
pet illustrates the proper usage of the parameters:
socket.onclose = function(event) {
console.log("Connection closed.");
var code = event.code;
var reason = event.reason;
var wasClean = event.wasClean;
var label =
if (wasClean) {
label.innerHTML = "Connection closed
else {
Search WWH ::

Custom Search