跨域WebSocket与Golang

huangapple go评论91阅读模式
英文:

Cross origin websockets with Golang

问题

我正在开发一个相对简单的PhoneGap应用程序(iOS),需要向Go服务器发出跨域websocket请求。在进行任何网络活动之前,我已经在应用程序中设置了$.support.cors = true; $.mobile.allowCrossDomainPages = true;。服务器为测试/诊断目的提供了一个简单的HTML/JavaScript页面,一切都很顺利 - websocket连接正常工作,服务器传递了正确的数据,一切都很好。

但是在iOS模拟器上运行的PhoneGap应用程序却不是这样。在模拟器中,Safari实际上可以访问我的测试/诊断页面。然而,PhoneGap应用程序却超时了。

我正在进行更多的调查,但我怀疑这是一个简单的服务器配置问题,需要有更多关于Go和/或受保护的websockets包的知识的人,所以我想在这里询问一下。

英文:

I'm developing a relatively simple PhoneGap application (iOS) that needs to make cross origin websocket requests to a Go server. I have set $.support.cors = true; $.mobile.allowCrossDomainPages = true; in my application before any network activity. The server serves a simple HTML/JavaScript page for testing / diagnostic purposes and everything works great there - the websocket connections work, the server delivers the correct data, it's all dandy.

This is not the case with the PhoneGapplication running on the iOS Simulator. In the Simulator, Safari can in fact reach my testing/diagnostic page. However, the PhoneGapplication times out.

I'm doing more investigations but my suspicion is that this is a simple server configuration issue and someone with more knowledge of Go and/or the blessed websockets package and I thought I'd ask here concurrent with my investigation.

答案1

得分: 2

CORS不适用于WebSocket。使用WebSocket时,有一个“origin”头,浏览器必须使用包含打开WS连接的JS的HTML的来源来填充该头。非浏览器客户端可能会填充该头,也可能不填充(并且可以使用任何他们喜欢的内容填充)。然后,WS服务器能够决定是否接受连接。

此外:我不知道PhoneGap使用的WebView在HTML来自本地文件系统时是否设置了origin头。我会尝试窃听流量并查看一下。

英文:

CORS does not apply to WebSocket. With WebSocket, there is an "origin" header, which browser MUST fill with the origin of the HTML containing the JS that opens the WS connection. Non-browser clients may or may not fill that header (and can fill it with anything they like anyway). The WS server then is able to decide whether to accept the connection or not.

Further: I don't know if the WebView used by PhoneGap sets the origin header when the HTML comes from local filesystem. I would try wiretap the traffic and have a look.

答案2

得分: 0

在PhoneGAP应用程序中没有跨域问题(请记住,此策略在客户端执行,服务器的标头只是给出一个指示)。

问题可能在于握手过程,因为PhoneGAP不原生支持websocket握手。有一些开源项目将其引入到PhoneGAP/iOS中,比如这个(我没有测试过,因为我不是iOS开发者)。

英文:

There is no Cross Domain concern in PhoneGAP appliations (remember that this policy is enforced client side and that the server's header only give an indication).

The problem is probably in your handshake as PhoneGAP doesn't support natively the websocket handshake. There are open source projects introducing it in PhoneGAP/iOS as this one (not tested by me as I'm not an iOS developer).

huangapple
  • 本文由 发表于 2012年6月18日 11:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/11076658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定