ACCES控制允许原产地与咕噜Server和瓶SocketIO应用 [英] Acces-Control-Allow-Origin with Grunt Server and Flask SocketIO-app
问题描述
我尝试过连接我的棱角分明的应用程序,与繁重的服务器上运行时收到以下错误信息,用socketIO瓶的应用程序:
I'm getting the following error message when trying too connect my angular app, run with the Grunt server, with a socketIO Flask app:
XMLHttpRequest cannot load http://localhost:8080/socket.io/1/?t=1381872821951.
Origin http://localhost:9000 is not allowed by Access-Control-Allow-Origin.
我一直在做一些挖掘这个问题已经和我知道它来自一个事实,客户端(angularjs)正在做从发出响应不同的服务器请求。
I have been doing some digging about this problem already and I know it comes from the fact the client (angularjs) is doing a request to a different server from which the response came.
在哪两个服务器的是什么问题?
Which of the both servers is the problem?
1)繁重的服务器?我已经尝试过这样的: http://stackoverflow.com/a/17256255/1819058
这应该解决的问题,如果它从咕噜服务器附带
1) The Grunt Server? I already tried this: http://stackoverflow.com/a/17256255/1819058 Which should solve the problem if it came from the Grunt server
2)烧瓶SocketIO服务器:
2) The Flask SocketIO server:
app = Flask(__name__)
@app.route("/socket.io/<path:path>")
def run_socketio(path):
socketio_manage(request.environ, {'': ChatNamespace})
if __name__ == '__main__':
print 'Listening on http://localhost:8080'
app.debug = True
import os
from werkzeug.wsgi import SharedDataMiddleware
app = SharedDataMiddleware(app, {
'/': os.path.join(os.path.dirname(__file__), 'static')
})
from socketio.server import SocketIOServer
SocketIOServer(('', 8080), app,
namespace="socket.io", policy_server=False).serve_forever()
这是我如何连接到服务器:
This is how I connect to the server:
var socket = ioSocket || io.connect('http://localhost:8080');
有人可以帮助我?是不是请求,如果您使用的WebSockets到另一台服务器逻辑是什么?
Can somebody help me with this? Isn't it logic that requests go to another server if you are using websockets?
另一个奇怪注:整个事情的工作,但停在重新启动后的工作...
Another strange note: The whole thing worked but stopped working after a reboot...
推荐答案
您需要指定自己的SocketIOHandler。首先,你需要修复的的init 在SocketIOServer。
You need assign your own SocketIOHandler. First of all you need fix init in SocketIOServer.
class CorsServer(SocketIOServer):
def __init__(self, *args, **kwargs):
self.sockets = {}
if 'resource' in kwargs:
print "DEPRECATION WARNING: use `namespace` instead of `resource`"
self.namespace = kwargs.pop('resource', kwargs.pop('namespace',
'socket.io'))
self.transports = kwargs.pop('transports', None)
if kwargs.pop('policy_server', True):
self.policy_server = FlashPolicyServer()
else:
self.policy_server = None
#fix
if 'handler_class' not in kwargs:
kwargs['handler_class'] = SocketIOHandler
super(SocketIOServer, self).__init__(*args, **kwargs)
然后让你自己SocketIOHandler
Then make your own SocketIOHandler
class CorsHandler(SocketIOHandler):
def write_plain_result(self, data):
self.start_response("200 OK", [
("Access-Control-Allow-Origin", self.environ.get('HTTP_ORIGIN', '*')),
("Access-Control-Allow-Credentials", "true"),
("Access-Control-Allow-Methods", "POST, GET, OPTIONS"),
("Access-Control-Max-Age", 3600),
("Content-Type", "text/plain"),
])
self.result = [data]
初始化瓶中,加航线
Init Flask, and add route
app = Flask(__name__)
@app.route("/socket.io/<path:path>")
def run_socket(param):
socketio_manage(request.environ, {'': YourNamespace})
初始化您CorsServer与CorsHandler
Init your CorsServer with your CorsHandler
if __name__ == "__main__":
server = CorsServer(('0.0.0.0', 8080), app,
namespace="socket.io",handler_class=CorsHandler).serve_forever()
这篇关于ACCES控制允许原产地与咕噜Server和瓶SocketIO应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!