0

我尝试使用生成的私人消息弹出窗口进行 Socket.IO 聊天。我在弹出窗口代码中使用 windows.opener var 来访问主页中的变量和函数。在 Firefox 和 Chrome 中,window.opener.socket.on(...) 函数从弹出窗口代码成功启动,但 IE9 不会。Node.js 服务器在后台发送和接收事件。我使用以下代码:

     //in index.php
    var socket = io.connect('http://localhost:8080');
     //other code
    $("#users .user").live('click',function(){
    //other code
    popUpWin[client_id]=window.open('private.php', client_id, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width=300,height=400,left=' + left + ', top=' + top + ',screenX=' + left + ',screenY=' + top + '');         
    //other code        
    });


    //in private.php
    //other code
    window.opener.socket.emit('popup',window.opener.client_id);//This work!         
    window.opener.socket.on('private_message', function (data) {This not work, private message event is send!
        $("#private_data_recieved").append('<div><b>'+data.nick+':</b> '+parseString(data.message)+'</div>');
        playSound();                        
    }); 
        //other code
4

1 回答 1

0

IE9 不支持原生 websocket。所以 socket.io 库在 xhr-polling 系统上做了一个后备。这会导致 IE 出现一些问题(老实说,我不知道)。

但是,如果您像这样改变解决问题的方法:

在 index.php 中

var EVENT = module.exports.EVENT;
var socket = io.connect('http://78.46.135.87:8060');
var win;
socket.on('private_message',function(data) {
    win.receivedPrivate(data);
});

function openPrivate() {
    win =window.open('private.php', 1, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width=300,height=400');         
}
function popupReady() {
     socket.emit('popup',"1");
}
// and html
<a href="#" onclick="openPrivate()">Open private</a>

在 private.php 中

window.receivedPrivate = function(data) {
    $('#private_data_recieved').append(data);
}
window.opener.popupReady();     

它应该有效。index.php我将套接字的逻辑移到了逻辑private.php视图中。我已经对其进行了测试,并且可以正常工作。

于 2012-10-19T20:36:25.503 回答