Home Web Front-end H5 Tutorial New protocol in HTML5: Example of WebSocket protocol

New protocol in HTML5: Example of WebSocket protocol

Aug 08, 2018 am 10:02 AM
websocket protocol

This article brings you an example of a new protocol in HTML5: WebSocket protocol. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

WebSocket is a new protocol in HTML 5. It performs full-duplex communication based on TCP connection.

Full-duplex communication: Indicates that data is allowed to be transmitted in both directions simultaneously. .

Determine whether the browser supports WebSocket

// 判断浏览器是否支持 WebSocket
if (window.WebSocket != undefined)
 {    
    var Socket = new WebSocket('ws://localhost: 8080')
  }
Copy after login

ws protocol is a new protocol for WebSocket. In addition, there is wss protocol, which represents the encrypted WebSocket protocol. The relationship between the two is like the HTTP protocol corresponding to the HTTPS protocol.

WebSocket Example

// 创建 WebSocket 实例
var Socket = new WebSocket('ws://localhost:8080')
// 连接打开
Socket.addEventListener('open', function(event) {
    socket.send('Hello Server!')
})
// 建通数据传送,有数据到达时触发
Socket.addEventListener('message', function(event) {
    console.log('Message from Server', event.data)
})
// 当错误发生时,用于监听 error 事件的事件监听器
Socker.addEventListener('error', function(event) {
    console.log(event.data)
})
// 连接关闭
Socket.addEventListener('close', function(event){
    console.log(event.data)
})
Copy after login

Recommended related articles:

HTML5 Application: Offline Application and Storage Application

HTML tag: summary of usage of img tag

The above is the detailed content of New protocol in HTML5: Example of WebSocket protocol. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Add Audio to My HTML5 Website? How to Add Audio to My HTML5 Website? Mar 10, 2025 pm 03:01 PM

How to Add Audio to My HTML5 Website?

How do I use the HTML5 Page Visibility API to detect when a page is visible? How do I use the HTML5 Page Visibility API to detect when a page is visible? Mar 13, 2025 pm 07:51 PM

How do I use the HTML5 Page Visibility API to detect when a page is visible?

How to Use HTML5 Forms for User Input? How to Use HTML5 Forms for User Input? Mar 10, 2025 pm 02:59 PM

How to Use HTML5 Forms for User Input?

How do I use viewport meta tags to control page scaling on mobile devices? How do I use viewport meta tags to control page scaling on mobile devices? Mar 13, 2025 pm 08:00 PM

How do I use viewport meta tags to control page scaling on mobile devices?

How do I use the HTML5 Notifications API to display desktop notifications? How do I use the HTML5 Notifications API to display desktop notifications? Mar 13, 2025 pm 07:57 PM

How do I use the HTML5 Notifications API to display desktop notifications?

How do I handle user location privacy and permissions with the Geolocation API? How do I handle user location privacy and permissions with the Geolocation API? Mar 18, 2025 pm 02:16 PM

How do I handle user location privacy and permissions with the Geolocation API?

How do I use the HTML5 Drag and Drop API for interactive user interfaces? How do I use the HTML5 Drag and Drop API for interactive user interfaces? Mar 18, 2025 pm 02:17 PM

How do I use the HTML5 Drag and Drop API for interactive user interfaces?

How to Create Interactive Games with HTML5 and JavaScript? How to Create Interactive Games with HTML5 and JavaScript? Mar 10, 2025 pm 06:34 PM

How to Create Interactive Games with HTML5 and JavaScript?

See all articles