Javascript 如何使用PostMessage将变量从一台计算机传输到另一台计算机?

Javascript 如何使用PostMessage将变量从一台计算机传输到另一台计算机?,javascript,html,Javascript,Html,使用Javascript的消息传递可以通过window.postMessage(“yourMessage”)完成http://example.com“但是根据我的测试,你不能通过这个发送变量。我是做错了,还是使用了错误的函数?这是我的发件人: <html> //This Page is http://example.com/2 <label> <textarea readonly></textarea> </label> &l

使用Javascript的消息传递可以通过
window.postMessage(“yourMessage”)完成http://example.com“
但是根据我的测试,你不能通过这个发送变量。我是做错了,还是使用了错误的函数?这是我的发件人:

<html>
//This Page is http://example.com/2

<label>
    <textarea readonly></textarea>
</label>
<button onclick="messageFunction(x)">SEND MESSAGE</button>
<script type="text/javascript">
    function messageFunction(xsend) {
        window.postMessage((xsend), "http://example.com")
    }

    //Just some basic variable
    let x = "Whatever you want it to be"

</script>
</html>

//这一页是http://example.com/2
发送消息
函数messageFunction(xsend){
window.postMessage((xsend),”http://example.com")
}
//只是一些基本变量
让x=“不管你想要什么”

这里是我遇到问题的地方(甚至可能在上面的代码中),如果我说添加一个
窗口。addEventListener('message',function)
我怎样才能取出变量?

你读了吗?“window.postMessage()方法可以安全地启用窗口对象之间的跨源通信;例如,页面和它生成的弹出窗口之间,或者页面和嵌入其中的iframe之间。”可以尝试使用WebRTC。或者在服务器上使用WebSocket