我有这个 Node.JS服务器:
var app = require('express')();
var server = app.listen(80);
var io = require('socket.io').listen(server);
var posx = 10;
var posy = 10;
app.get('/', function (req, res) {
res.sendfile( __dirname + '/index.html' );
});
io.sockets.on('connection', function (socket) {
socket.emit('start', {
x: posx,
y: posy
});
socket.on('newpos', function (data) {
posx = data["x"];
posy = data["y"];
socket.broadcast.emit('move', { x: posx, y: posy });
});
});
客户端代码:
var socket = io.connect('http://localhost');
socket.on('start', function (data) {
$("#pointer").animate({
'top': data["y"],
'left': data["x"]
}, 0);
});
socket.on('move', function (data) {
$("#pointer").animate({
'top': data["y"],
'left': data["x"]
}, "slow");
});
$("#pointer").draggable({
stop: function(event, ui) {
var pos = $("#pointer").position();
socket.emit('newpos', {
'x': pos.left,
'y': pos.top
});
}
});
问题是它似乎只在本地工作。在 ubuntu chrome 上我得到:
XMLHttpRequest cannot load http://localhost/socket.io/1/?t=1344711676473. Origin http://192.168.1.130 is not allowed by Access-Control-Allow-Origin.
在 Mac 上,我对同一个文件有 GET 错误...
知道问题可能是什么吗?