WebSocket Protocol is a new protocol for HTML5. It implements full duplex communication between browser and server, and allows cross-domain communication. It is a good implementation of server push technology. We use socket. IO, which nicely encapsulates the webSocket interface, provides a simpler, flexible interface, and provides backward compatibility for browsers that do not support webSocket.

The parent page and child page need to communicate, and the parent page cross domain. What should I do?

In order to ensure that parent-child page communication is point-to-point, it is necessary to establish the corresponding relationship between the parent-child page WebSocket on the server side, that is, the message sent by the parent page is only received by the quilt page, and the message of the child page is also received by the parent page. We did the following work to strictly ensure that the WebSocket communication is point-to-point:

One is to establish the WebSocket link URL plus the timestamp to ensure that the communication session is unique; The second is to ensure the one-to-one correspondence between parent and child websockets on the server. The WebSocket of the parent and child pages will send messages to the server for registration when it is Open to establish the corresponding relationship between Senssion. The parent and child pages can then communicate via a mutually constrained communication protocol. Here we write a demo:

Var p = document. GetElementsByTagName (” p “) [0];

Var IO = IO. Connect (” http://127.0.0.1:3001 “);

IO. On (‘ data ‘, function (data) {

Alert (‘ change data after 2S ‘);

p.innerHTML = data

});

The server side

Var IO = the require (” socket. IO ‘) (server);

IO. On (‘ connection ‘, function (the client) {

Client. Emit (” data “, “hello WebSocket from 3001. ‘);

});

That’s all for today, I hope it’s helpful, and if you don’t want to spend too much time on WebSocket, you can try using three-party WebSocket, like GoEasy Aurora and so on. GoEasy is recommended here. It’s easy to use and www.goeasy.io is free. You can try it.