HTML5 Development Tour WebSocket Add Event Listener (6)

Source: Internet
Author: User

WebSocket programming follows the asynchronous programming model; After opening the socket, just wait for the event to occur, instead of actively polling the server, you need to add a callback function to the WebSocket object to listen for the event.

The WebSocket object has three events: Open,close and message. The Open event is triggered when the connection is established, and the message event is triggered when the messages are received, and the close event is triggered when the WebSocket connection is closed. With most JavaScript API, event handling invokes the corresponding (OnOpen, onmessage, and OnClose) callback functions.

1W.onopen =function () {2Log"Open");3W.send ("Thank you for accepting this websocket request! ");4 }5W.onmessage =function (e) {6 log (e.data);7 }8W.onclose =function (e) {9Log"closed");Ten}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.