Java Websocket 和跨域
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24031357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Websocket and cross-domain
提问by
Currently, I am developing two webapps :
目前,我正在开发两个 webapps :
- the core, developed in Java with Vert.x, receives data from a lot of other apps and sends it to client)
- the client, developed in PHP/JS, display data from the core.
- 核心,使用 Vert.x 用 Java 开发,从许多其他应用程序接收数据并将其发送到客户端)
- 用 PHP/JS 开发的客户端显示核心数据。
The client isn't on the same domain as my core for the production phase.
在生产阶段,客户端与我的核心不在同一个域中。
Since my development period, the two webapps are hosted in the same computer. I used "ws:\\localhost:9090" to connect my client to my core and I had no problem to transfer data.
在我的开发期间,这两个 webapp 托管在同一台计算机上。我使用“ws:\\localhost:9090”将我的客户端连接到我的核心,并且传输数据没有问题。
But, today, I try to replace localhost with my IP Address and, it didn't work :s
但是,今天,我尝试用我的 IP 地址替换 localhost,但它不起作用:s
I think that it's because the client isn't in the same domain as the core. But I don't know how I can correct this problem ?
我认为这是因为客户端与核心不在同一个域中。但我不知道如何解决这个问题?
Do you have any idea ?
你有什么主意吗 ?
Thanks !
谢谢 !
Code example (based on my code but not my code ;) ):
代码示例(基于我的代码但不是我的代码;)):
- For the core : http://pastebin.com/h2ZnBvQJ
- For the client : http://pastebin.com/DR5BeABf
- 对于核心:http: //pastebin.com/h2ZnBvQJ
- 对于客户端:http: //pastebin.com/DR5BeABf
回答by Daniel
Yes, Websocket can do cross-domain, but you will need to have a handshake in order to get this going. You might want to take a look at: http://en.wikipedia.org/wiki/WebSocketsfor an example of a handshake.
是的,Websocket 可以跨域,但你需要握手才能实现这一点。您可能需要查看:http: //en.wikipedia.org/wiki/WebSockets以获取握手示例。
Also related and maybe duplicate: Web sockets make ajax/CORS obsolete?
同样相关并且可能重复:Web sockets make ajax/CORS obsolete?