HTML and CSS Reference
In-Depth Information
WebSocket API
3
6
10
11
5
The
onmessage
event is used in every other messaging API in HTML5, so it should
come as no surprise to you that it gets used in WebSockets, too. For WebSockets you need
to create a
WebSocket
object and attach a function to the
message
event listener.
The code you write will dump information to the console as it receives it; sample console
output is shown in the following listing.
Listing 4.10. Server output for a simple WebSocket test
Figure 4.8
shows the corresponding output in the browser developer console. The browser
requests the page; then it upgrades the connection to a WebSocket. It sends the message
“My Message” before receiving the response from the server; in this case the same “My
Message” string is sent back as a message.