PeerJS 对等点未接收数据

Nes*_*tor 1 javascript peerjs

我打算使用 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');
    });

}
Run Code Online (Sandbox Code Playgroud)

在浏览器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');
    });

}
Run Code Online (Sandbox Code Playgroud)

显示“连接已打开”消息,但从未显示“数据”“已连接”消息。

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

Nes*_*tor 5

看来我忘记添加另一个回调了。JS 编译没有问题真是太棒了,即使回调丢失了……一块 g*rbage……

所以代替这个:

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

我必须使用这个:

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