You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PeerJS对等体连接异常:音频通话应用接收端问题求助

Hey there, let's break down the issues with your PeerJS audio call setup and fix them step by step:

1. Misplaced Connection Listener

Your initiator side's peer.on('connection') is wrapped inside callDialog.init(function(){...}) — if this callback runs after the receiver has already sent the connection request, you'll miss the connection event entirely. You should set up this listener as soon as you initialize your Peer instance, not wait for the call dialog to load.

2. Wrong Stream Assigned to Audio Element

In your receiver's call.on('stream') handler, you're using your local stream instead of the incoming remoteStream for the audio source. That's why you won't hear the caller's audio even if the call connects.

3. Undefined currentCall on Decline

When declining the call, you're trying to close window.currentCall, but this variable only gets set when you accept the call. You should directly close the incoming call object instead.

4. Unnecessary Separate Data Connection

PeerJS's Call object already supports sending data via call.send() — you don't need to create a separate peer.connect() connection just to send ACCEPT/DECLINED messages. This simplifies your code and avoids potential connection race conditions.


Fixed Receiver Code

peer.on('call', function(call) {
    var receiveCall = bootbox.dialog({
        className: "modal-danger nonumpad",
        closeButton: false,
        animate: true,
        title: 'Call Received',
        message: "Accept or Decline",
        onEscape: null,
        buttons: {
            pickup: {
                label: "<i class=\"fa fa-phone\"></i> Answer",
                className: "btn-warning btn-lg pull-left",
                callback: function(){
                    // Send accept directly via the call object
                    call.send('ACCEPT');
                    window.currentCall = call;
                    call.answer(stream); // Answer with local stream
                    call.on('stream', function(remoteStream) {
                        var audio = $('<audio autoplay />').appendTo('body');
                        // Use remoteStream here, not local stream!
                        audio[0].src = (URL || webkitURL || mozURL).createObjectURL(remoteStream);
                    });
                    return false;
                }
            },
            hangup: {
                label: "<i class=\"fa fa-phone\"></i> Decline",
                className: "btn-warning btn-lg pull-left",
                callback: function(){
                    // Send decline directly via the call object
                    call.send('DECLINED');
                    call.close(); // Close the incoming call directly
                    stream.getTracks().forEach(track => track.stop());
                    receiveCall.modal('hide'); // Close the dialog
                    return false;
                }
            }
        }
    });
});

Fixed Initiator Code

// When you initiate the call, listen for data directly on the call object
var call = peer.call(remotePeerId, localStream);
call.on('data', function(data) {
    console.log('Received call status:', data);
    if(data == "ACCEPT"){
        $("#test").text("This is dynamic msg");
        $("#hangup").text("Hangup");
    } else if (data == "DECLINED"){
        call.close();
        // Add your declined logic here (e.g. update UI to show call was rejected)
    }
});

// Keep your callDialog init as is — no need for separate connection listener now
callDialog.init(function(){
    // Your existing dialog initialization logic here
});

内容的提问来源于stack exchange,提问作者sourceplaze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:48:02