PeerJS 对等方未接收数据

PeerJS peer not receiving data

本文关键字:数据 方未接 对等 PeerJS      更新时间:2023-09-26

我打算使用PeerJs来创建P2P连接(数据和后来的视频)。我检查了教程并使用以下代码。

在浏览器 A (Chrome 38) 中:

var local;
var remote;
function peerJsInit() {
    //listening host
    local = new Peer(
    {
        key: 'myPeerJSKey'
    });
    local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
    });
}

function peerSend() {
    remote = new Peer(
    {
        key: 'myPeerJSKey',
        debug: true
    });
    var c = remote.connect('remoteId');
    c.on('open', function() {
        alert('connected');
        c.send(' peer');
    });
}

在浏览器 B (Chrome 38) 中:

var local;
var remote;
function peerJsInit() {
    //listening host
    local = new Peer({
        key: 'myPeerJSKey'
    });
    local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
    });
}

function peerSend() {
    remote = new Peer({
        key: 'myPeerJSKey',
        debug: true
    });
    var c = remote.connect('remoteId');
    c.on('open', function() {
        alert('connected');
        c.send(' peer');
    });
}
显示"连接

已打开"消息,但"数据和连接"消息从不显示。

你能告诉我我应该改变什么吗?

看来我忘了添加另一个回调。它是如此之好,即使缺少回调,JS也可以毫无问题地编译......一块G*RBAGE...

所以取而代之的是:

 local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
    });

我必须使用这个:

 local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('open',function(){
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
       });
    });