简体   繁体   English

从iframe连接时出现socket.io错误

[英]socket.io error when connecting from iframe

So I have a couple applications on different servers all from inside of our network and I am using node.js and socket.io js to handle real time communication between them which when each is run separately works fine, but when I put application 2 inside an iframe on application 1 I get the following error "Blocked a frame with origin "http : // 192.128.1.97" from accessing a frame with origin "http : // intranet". Protocols, domains, and ports must match. " *note I added spaces in the urls above because the page was telling me links weren't allowed. 所以我在网络内部的不同服务器上都有几个应用程序,并且我正在使用node.js和socket.io js来处理它们之间的实时通信,当分别运行它们时,它们可以正常工作,但是当我将应用程序2放入内部时应用程序1上的iframe时,出现以下错误“通过访问源为“ http:// intranet”的帧阻止了源为“ http:// 192.128.1.97”的帧。协议,域和端口必须匹配。“ *请注意,我在上面的网址中添加了空格,因为该页面告诉我不允许链接。

is there some way to allow the iframe to connect to socket.io? 有什么方法可以让iframe连接到socket.io? the code is pretty simple, but here is the server code... 代码很简单,但这是服务器代码...

/**
 * 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"))
});

application 1 code.... 应用程序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();
})

and application 2 code.... 和应用2代码。

// NODE JS INITIALIZATION
var socket = null;
var sessionId = '';
function initialize_node(){

var serverBaseUrl = 'http://192.168.1.76:8080';

socket = io.connect(serverBaseUrl);
sessionId = '';

socket.on('connect', function() {
    sessionId = socket.socket.sessionId;
    socket.emit('newFluxClient', {id: sessionId, badgeNumber: "PDX000022", name: "matthew.hicks"});
//        socket.emit('newUser', {id: sessionId, badgeNumber: user.badge, name: user.name});
})

socket.on('allowedPhrase', function(data) {
    if(sessionId === data.id) {
        alert("I'm a preddy little princess. Console logging data returned");
        console.log(data);
        /*
         functions to allow or disallow the phrase
         based on data.allowed
         it will be true if the phrase is in the shopping cart
         and false if it is not
         */
    }
});

//  $('#phrase').blur(function() {
//      checkPhrase();
//  })
};

function checkPhrase() {
//var phrase = $('#phrase').val();
var phrase = "Shindigs in Ptown";
socket.emit('phraseCheck', {id: sessionId, phrase: phrase});
}


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

Sorry for the mass amount of code but trying to give all the conte4xt necessary. 很抱歉,大量的代码,但尝试提供所有必需的conte4xt。 Essentially server is up and running, application 1 connects and gets a unique session id, then when application 2 tries to connect from the iframe I get the above mentioned error, when application 2 is not in an iframe it connects just fine and gets a session id. 本质上,服务器已启动并正在运行,应用程序1连接并获得了唯一的会话ID,然后当应用程序2尝试从iframe连接时,出现上述错误,当应用程序2不在iframe中时,它连接得很好,并获得了会话ID。 Please help if you can, I can't figure out why it is getting blocked and I really need this up and running. 如果可以的话,请帮忙,我不知道为什么它被阻止了,我真的需要启动它并运行它。 Thank you in advance for any help 预先感谢您的任何帮助

You have encountered Same Origin Policy. 您已经遇到了相同来源政策。

The simplest solution is to run the iframe from the same server. 最简单的解决方案是从同一服务器运行iframe。

Since you have access to IT time read up on CORS You will basically have to configure the server to allow XSS from your domain. 由于您可以访问IT时间,可以在CORS上进行阅读,因此,您基本上必须将服务器配置为允许来自您域的XSS

You can also try something like: 您也可以尝试以下方法:

document.domain = "intranet"

Read up on it here 这里阅读

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

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