繁体   English   中英

AppFog node.js客户端无法加载socket.io

[英]AppFog node.js client cannot load socket.io

我刚刚将一个node.js站点部署到AppFog。 当我转到“ http:// localhost:8080”时,它在本地运行良好,但是当我在AppFog上找到它时: http ://bandwithfriends.aws.af.cm/我收到以下Chrome控制台错误:

XMLHttpRequest cannot load http://localhost:8080/socket.io/1/?t=1357769454152. Origin http://bandwithfriends.aws.af.cm is not allowed by Access-Control-Allow-Origin.

服务器代码:

var app = require('http').createServer(handler),
    io = require('socket.io').listen(app),
    static = require('node-static'); // for serving files

// This will make all the files in the current folder
// accessible from the web
var fileServer = new static.Server('./');


app.listen(process.env.VCAP_APP_PORT || 8080);

io.configure('development', function(){
  io.set('transports', ['xhr-polling']);
});

客户代码index.html:我这样包含socket.io:

<script src="/socket.io/socket.io.js"></script>

客户代码script.js:

var url = 'http://localhost:8080';
var socket = io.connect(url);

我该如何解决?

为了解决这个问题,我要做的就是删除客户端代码上的网址:

var socket = io.connect();

希望这可以帮助其他有相同问题的人!

您的客户端和服务器端都是错误的。

在客户端; 进行更改:

var url = io.connect('http://<your domain name>');
//For example: var url = io.connect('http://shash7.ap01.aws.af.cm');

在服务器端,您需要这样实现socket.io:

var app = require('express')()   , server =
require('http').createServer(app)   , io =
require('socket.io').listen(server);

server.listen(80);

上面的代码来自socket.io网站。 他们更改了0.9版本中socket.io的侦听方式,因此请使用上述版本。

除此之外,一切看起来还不错。 告诉我您是否仍然无法解决这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM