CORS with socket.io [英] CORS with socket.io
问题描述
我在使用express.io的node.js应用程序中遇到了CORS问题。我远程托管socket.io客户端js,因为这需要作为一个远程应用程序。
I'm having trouble with CORS on a node.js app using express.io. I'm hosting the socket.io client js remotely since this needs to works as a remote app.
<script src="resources/js/socket.io.min.js"></script>
它托管在OpenShift
It's hosted on OpenShift
js:
var ipaddr = process.env.OPENSHIFT_NODEJS_IP || "localhost";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var express = require('express.io');
// magical express.io
var app = express();
// Enables CORS
var enableCORS = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, *');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
};
};
app.configure(function() {
// enable CORS!
app.use(enableCORS);
});
app.http().io();
app.io.set('origins', '*:*');
//.... other stuff
app.listen(port, ipaddr);
然后在客户端:
var socket = io.connect(window.chat_url);
当我从localhost:8888和服务器localhost:8080 socket.io运行客户端时工作正常。
When I run the client from localhost:8888 with the server localhost:8080 socket.io works fine.
当我从localhost:8888和odechat-latestsightings.rhcloud.com上的服务器运行客户端时,socket.io会超时:
When I run the client from localhost:8888 and the server on odechat-latestsightings.rhcloud.com then socket.io times out:
Firebug:
GET http://nodechat-latestsightings.rhcloud.com:8888/socket.io/1/?t=1391542144169 1分16秒
Firebug: GET http://nodechat-latestsightings.rhcloud.com:8888/socket.io/1/?t=1391542144169 1m 16s
其他路线工作正常:
GET http://nodechat-latestsightings.rhcloud.com/rooms 200 OK 664ms
The other routes work fine: GET http://nodechat-latestsightings.rhcloud.com/rooms 200 OK 664ms
我根本无法理解这一点。
I just can't figure this out
推荐答案
,如果你的服务器是在openshift然后你应该绑定到端口:8000使用websockets,因为这是端口openshift的nginx反向代理有公共访问。
Also, if your server is on openshift then you should bind to port: 8000 to use websockets since that is the port openshift's nginx reverse proxy has public access.
您可以阅读更多从这里开始: https://developers.openshift.com/en/managing- port-binding-routing.html
You can read more from here: https://developers.openshift.com/en/managing-port-binding-routing.html
这篇关于CORS with socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!