HTML and CSS Reference
In-Depth Information
label.innerHTML = "Connection closed with
message " + reason + "(Code: " + code + ")";
}
}
You can find a detailed list of the code values in the appendix of this topic.
onerror
The onerror event is fired when something wrong (usually unexpected behavior or
failure) occurs. Note that onerror is always followed by a connection termination,
which is a close event.
A good practice when something bad happens is to inform the user about the unex-
pected error and probably try to reconnect:
socket.onclose = function(event) {
console.log("Error occurred.");
// Inform the user about the error.
var label =
document.getElementById("status-label");
label.innerHTML = "Error: " + event;
}
Actions
Events are raised when something happens. We make explicit calls to actions (or
methods) when we want something to happen! The WebSocket protocol supports
two main actions: send() and close() .
send()
While a connection is open, you can exchange messages with the server. The
send() method allows you to transfer a variety of data to the web server. Here is
how we can send a chat message (actually, the contents of the HTML text field) to
everyone in the chat room:
Search WWH ::




Custom Search