Javascript 从iframe连接时发生socket.io错误

Javascript 从iframe连接时发生socket.io错误,javascript,node.js,socket.io,Javascript,Node.js,Socket.io,因此,我在网络内部的不同服务器上有两个应用程序,我使用node.js和socket.io js来处理它们之间的实时通信,这两个应用程序单独运行时效果良好,但当我将应用程序2放在应用程序1的iframe中时,我得到以下错误 阻止源代码为“http://192.128.1.97”的帧访问源代码为“http://intranet”的帧。协议、域和端口必须匹配 *注意,我在上面的URL中添加了空格,因为页面告诉我链接是不允许的 是否有某种方法允许iframe连接到socket.io?代码非常简单,但这里

因此,我在网络内部的不同服务器上有两个应用程序,我使用node.js和socket.io js来处理它们之间的实时通信,这两个应用程序单独运行时效果良好,但当我将应用程序2放在应用程序1的iframe中时,我得到以下错误 阻止源代码为“http://192.128.1.97”的帧访问源代码为“http://intranet”的帧。协议、域和端口必须匹配 *注意,我在上面的URL中添加了空格,因为页面告诉我链接是不允许的

是否有某种方法允许iframe连接到socket.io?代码非常简单,但这里是服务器代码

/**
 * Server js file for node
 * this will handle all of the incoming requests from all the apps
 * and push them to the clients
 */

var express = require("express"),
    app = express(),
    http = require("http").createServer(app),
    io = require("socket.io").listen(http);
    _ = require("underscore");

var participants = [];

// setup the environment and tell node and express what it needs
app.set("ipaddr", "192.168.1.76");
app.set("port", 8080);
app.set("views", __dirname + "/views");
app.set("view engine", "jade");

//further environment setup telling node and express what to use to handle requests
app.use(express.static("public", __dirname));
app.use(express.bodyParser());

//setup the default page
app.get("/", function(request, response) {
    //render the view page
    //response.render("node_home");
    //just post a message to the screen
    response.send("Server is up and running");
    //respond with a json object
//    reponse.json(200, {message: "Server is up and running"});
});

//setup a handler for requests to /message
app.post("/message", function(request, response) {
    var message = request.body.message;
    if(_.isUndefined(message) || _.isEmpty(message.trin())) {
        return response.json(400, {error: "Message is invalid"});
    }

    var name = request.body.name;
    io.sockets.emit("incomingMessage", {message: message, name: name});
    response.json(200, {message: "Message received"});
})

io.on("connection", function(socket) {
    socket.on("newUser", function(data) {
        participants.push({id: data.id, name: data.name});
        io.sockets.emit("newConnection", {participants: participants, badgeNumber: data.badgeNumber, id: data.id})
    });
    socket.on("nameChange", function(data) {
        _findWhere(paticipants, {id: socket.id}).name = data.name;
        io.sockets.emit("nameChanged", {id: data.id, name: data.name})
    });
    socket.on("disconnect", function() {
        participants = _.without(participants, _.findWhere(participants, {id: socket.id}));
        io.sockets.emit("userDisconnected", {id: socket.id, sender: "system"})
    });
    socket.on("phraseCheck", function(data) {
        io.sockets.emit("checkPhrase", {id: data.id, phrase: data.phrase});
    });
    socket.on('newFluxClient', function(data) {
    console.log(data);
        io.sockets.emit('fluxConnection', {badgeNumber: data.badgeNumber, id: data.id});
    });
    socket.on('phraseAllowed', function(data) {
        io.sockets.emit('allowedPhrase', {id: data.id, allowed: data.allowed});
    });
    socket.on('customFunction', function(data) {
        console.log(data);
    io.sockets.emit('customFunction', data);
    });
});


//start the app and have it listen for incoming requests
http.listen(app.get("port"), app.get("ipaddr"), function() {
    console.log("Server up and running. Go to http://" + app.get("ipaddr") + ":" + app.get("port"))
});
应用程序1代码

/**
 * client js file
 * this will handle connecting to node and handle the incoming messages
 * as well as sending responses and messages to the server
 */
var childSessionId = '',
sessionId = '',
socket = '',
serverBaseUrl = '',
participants = [];

function init() {
serverBaseUrl = 'http://192.168.1.76:8080';

socket = io.connect(serverBaseUrl);

sessionId = '';
function updateParticipants(part) {
    participants = part;
    $("#participants").html('');
    for(var i=0; i<participants.length;i++) {
        $("#participants").append('<span id="' + participants[i].id + '">' + participants[i].name + ' ' + (participants[i].id === sessionId ? '(You)' : '') + '<br /></span>');
    }
}
socket.on('connect', function() {
   sessionId = socket.socket.sessionid;
    console.log('Connected ' + sessionId);
    socket.emit("newUser", {id: sessionId, name: page.user});
});
socket.on('userDisconnect', function(data) {
    $('#' + data.id).remove();
});
socket.on('nameChanged', function(data) {
    $('#' + data.id).html(data.name + ' ' + (data.id === sessionId ? '(You)' : '') + '<br />');
});
socket.on('newConnection', function(data) {
    if(data.badgeNumber === page.userBadgeNumber) {
        childSessionId = data.id;
    }
    updateParticipants(data.participants);
});
socket.on('fluxConnection', function(data) {
    console.log('flux connection data:');
    console.log(data);
    if(data.badgeNumber === "**********") {
        childSessionId = data.id;
    }
});
socket.on('incomingMessage', function(data) {
    $("#messages").prepend('<b>' + data.name + '</b><br />' + data.message + '<hr />');
});
socket.on('error', function(reason) {
    console.log('Unable to connect to server', reason);
});
socket.on('customFunction', function(data) {
    console.log(data);

        data.data();

});
socket.on('checkPhrase', function(data) {
    if(data.id === childSessionId) {
        var phrases = shoppingcart.getPhrasesInCart();
        var allowed = ($.inArray(data.phrase, phrases) >= 0);
        socket.emit('phraseAllowed', {id: data.id, allowed: allowed});
    }
});

}
$(document).ready(function() {
    init();
})

很抱歉,代码太多,但正在尝试提供所有必需的内容。基本上,服务器已启动并正在运行,应用程序1连接并获取唯一的会话id,然后当应用程序2尝试从iframe连接时,我会收到上述错误,当应用程序2不在iframe中时,它连接正常并获取会话id。如果可以,请提供帮助,我不明白为什么它会被阻塞,我真的需要启动并运行它。提前感谢您提供的任何帮助

您遇到了同一原产地政策

最简单的解决方案是从同一台服务器运行iframe

既然你有权访问I.T时间,请阅读 基本上,您必须将服务器配置为允许来自您的域的XSS

您也可以尝试以下方法:

document.domain = "intranet"

仔细阅读

不行,应用程序2必须位于特定的服务器上,I.T.部门不希望从该服务器运行节点,他们希望它位于单独的服务器上以防万一。因此,基本上所有3个应用程序都必须位于不同的服务器上。我的假设是,使用node和socket.io,您不会遇到相同策略来源的问题,因为它们意味着允许在所有应用程序上使用基于推送的通知,而不管服务器是什么。我编辑了我的答案。您可能会遇到这种情况,因为您将socket.io包装在iframe中
document.domain = "intranet"