【问题标题】:WebRTC , socket.io, node.js: Cannot read property 'emit' of undefined?WebRTC、socket.io、node.js:无法读取未定义的属性“发射”?
【发布时间】:2015-03-23 22:54:44
【问题描述】:

我正在尝试制作一个 webrtc 视频应用程序。 在客户端代码中,我有这个:

getUserMedia(constraints, handlemedia, errorhandle);
constraints = {video: true};
function handlemedia(stream){
    //other stuff I do here

    document.getElementById("connect").addEventListener("click", function(e){
        var socket = io.connect();
    }

    var servers = {"iceServers": [{ "url": "stun:localhost:2013"}]};
    pc = new RTCPeerConnection(servers);
    pc.onicecandidate = function (evt, socket) {
        console.log("THIS IS THE evt.candidate: ", evt.candidate);
        somecandidate = evt.candidate;
        socket.emit( "candidate", somecandidate );
    };
    pc.addStream(localStream);
    function gotDescription(desc, socket) {
        console.log("WE GOT THE DESC.");
        pc.setLocalDescription(desc);
        socket.emit( "sdp", desc);
    }

    document.getElementById("createoffer").addEventListener("click", function(e){
        pc.createOffer(gotDescription);
    });
}

然后在 server.js(在 node.js 中)代码中:

var io = require('socket.io').listen(app);
io.sockets.on('connection', function(socket){
    socket.on("candidate", function(somecandidate){
    console.log("GOT CANDIDATE.");
    });

    socket.on("sdp", function(desc){
    console.log("Got desc");
    });
});

我意识到我现在只是在使用按钮,但这只是现在,以便我可以简单地测试它。我只需要把这整个 webrtc 搞定。

因此,如果我运行此代码,它会在浏览器控制台中显示“无法读取未定义的属性 'emit'。很明显,它不喜欢 socket.emit 的东西。为什么不呢?它不喜欢 socket 的什么地方。发射?

【问题讨论】:

  • 您尝试在函数外部使用 var socket,以便您可以在该函数中分配它并稍后在 pc.onicecandidate 上使用它?在那个阶段它是未定义的。

标签: javascript node.js socket.io webrtc p2p


【解决方案1】:
document.getElementById("connect").addEventListener("click", function(e){
 var socket = io.connect();
}

var 将变量限定在定义它的函数内。这里的socket 只在这个函数内部可见。

pc.onicecandidate = function (evt, socket) {
    console.log("THIS IS THE evt.candidate: ", evt.candidate);
    somecandidate = evt.candidate;
    socket.emit( "candidate", somecandidate );
};

上面的var socket 在这里不可见,所以socket 将是undefined。

因此,在您的情况下,按钮实际上使其更难使用。你最好不要使用它们:

getUserMedia(constraints, handlemedia, errorhandle);
constraints = {video: true};
function handlemedia(stream){
    //other stuff I do here

    var socket = io.connect();

    // Start the RTCPeerConnection and candidate gathering
    // after we've done io.connect()
    var servers = {"iceServers": [{ "url": "stun:localhost:2013"}]};
    pc = new RTCPeerConnection(servers);

    pc.onicecandidate = function (evt, socket) {
        console.log("THIS IS THE evt.candidate: ", evt.candidate);
        somecandidate = evt.candidate;
        socket.emit( "candidate", somecandidate );
    };
    pc.addStream(localStream);
        function gotDescription(desc, socket) {
        console.log("WE GOT THE DESC.");
        pc.setLocalDescription(desc);
        socket.emit( "sdp", desc);
    }

    pc.createOffer(gotDescription);
}

如果您真的想要按钮,那么将上面的整个内容包装到一个函数中,您将用作点击处理程序,而不是仅仅将var socket = io.connect() 单独放入处理程序中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多