与Golang交叉来源的websockets [英] Cross origin websockets with Golang

查看:106
本文介绍了与Golang交叉来源的websockets的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个相对简单的PhoneGap应用程序(iOS),它需要将跨站点websocket请求发送到Go服务器。我已经设置了 $。support.cors = true; $ .mobile.allowCrossDomainPages = true; 在我的应用程序之前的任何网络活动。服务器提供了一个简单的HTML / JavaScript页面用于测试/诊断目的,并且所有的工作都非常有效 - websocket连接工作,服务器提供正确的数据,这一切都很棒。



iOS模拟器上运行的PhoneGapplication并非如此。在模拟器中,Safari实际上可以访问我的测试/诊断页面。但是,PhoneGapplication超时。



我正在做更多的调查,但我怀疑这是一个简单的服务器配置问题,以及对Go和/或祝福websockets包有更多了解的人,我想我会在这里与我的调查同时询问。 解决方案

CORS不适用于WebSocket。使用WebSocket,有一个origin头文件,浏览器必须填写包含打开WS连接的JS的HTML的原点。非浏览器客户端可能会或可能不会填充该标头(并且可以用任何他们喜欢的东西填充它)。然后,WS服务器可以决定是否接受连接。



更进一步:我不知道PhoneGap使用的WebView是否在HTML来自本地文件系统。我会尝试窃听流量并看看。


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.

解决方案

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.

这篇关于与Golang交叉来源的websockets的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆