Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the Cross-domain problem of WebSocket in html

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to solve the cross-domain problem of WebSocket in html". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to solve the cross-domain problem of WebSocket in html" can help you solve your doubts.

WebSocket protocol is a new protocol of HTML5. It realizes full-duplex communication between browser and server, and allows cross-domain communication at the same time. It is a good implementation of server push technology. We use Socket.io, which encapsulates the webSocket interface well, provides a simpler and flexible interface, and provides backward compatibility for browsers that do not support webSocket.

Javascript cross-domain problem encountered in the project, the parent page and child page to communicate, and the parent-child page cross-domain, how to do?

To ensure that the parent-child page communication is peer-to-peer in the project, we need to establish a corresponding relationship on the server side to the parent-child page WebSocket, that is, the message sent by the parent page is only received by the child page, and the message from the child page is only received by the parent page. We have done the following work, which is strictly guaranteed.

WebSocket communication is point to point:

One is to establish the URL of the WebSocket link with a timestamp to ensure that the communication session is unique.

The second is to ensure the one-to-one correspondence of the parent-child page WebSocket on the server. During the Open, the WebSocket of the parent and child page will send a message to the server to register and establish the corresponding relationship between the Senssion. The parent and son pages can then communicate through a communication protocol constrained by both parties.

Here we write a demo:

Var p = document.getElementsByTagName ('p') [0]; var io = io.connect ('change data after http://127.0.0.1:3001');io.on('data',function(data){alert('2S'); p [XSS _ clean] = data})

Server side

Var io = require ('socket.io') (server); io.on (' connection',function (client) {client.emit ('data','hello WebSocket from 3001.');}) After reading this, the article "how to solve the cross-domain problem of WebSocket in html" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report