HTML and CSS Reference
In-Depth Information
// Find the text view and the button.
var textView =
var buttonSend =
// Handle the button click event.
buttonSend.onclick = function() {
// Send the data!!!
It's that simple!
But wait… The preceding code is not 100 percent correct. Remember that you
can send messages only if the connection is open. This means that we either
need to place the send() method inside the onopen event handler or check the
readyState property. This property returns the state of the WebSocket connection.
So, the previous snippet should be modified accordingly:
button.onclick = function() {
// Send the data if the connection is open.
if (socket.readyState === WebSocket.OPEN) {
After sending the desired data, you can wait for an interaction from the server or
close the connection. In our demo example, we leave the connection open, unless
the stop button is clicked on.
The close() method stands as a goodbye handshake. It terminates the connection
and no data can be exchanged unless the connection opens again.
Search WWH ::

Custom Search