簡體   English   中英

WebRTC數據通道不會發送?

[英]WebRTC datachannel wont send?

最近,我一直在嘗試在Haxe中實現WebRTC數據通道,但是遇到了很多困難。 當我使用

dataChannel.send();

盡管已成功打開了數據通道,但似乎沒有任何效果。

我正在使用的(極其低效且凌亂的)代碼如下所示:

package arm;
import haxe.Json;
import js.html.rtc.*;
import js.html.Document;
import js.html.WebSocket;
import js.html.DataElement;
@:expose
class DataChannelManager extends iron.Trait {
    var user = "gobbledygook";
    var first = false;
    var initiator = true;
    public function new() {
        super();

        var document = new Document();

        var ws:js.html.WebSocket;

        var config = {"iceServers":[{"url":"stun:stun.l.google.com:19302"}]};//temporary arrangement

        //var optional:Array<Dynamic> = [{'DtlsSrtpKeyAgreement': true}, {'RtcDataChannels': true }];

        // var connection:Dynamic = {
        //  'optional'://try changing this to mandatory some time
        //      optional
        // };

        var peerConnection = new PeerConnection(config);

        var dataChannel:js.html.rtc.DataChannel;

        var ready = false;

        function sendNegotiation(type, sdp) {
            var json = {user:user/*, theloc:myloc*/, action: type, data: sdp};
            ws.send(Json.stringify(json));
            trace("Negotiation of type "+json.action);
        }

        var sdpConstraints = {
                offerToReceiveAudio: false,
                offerToReceiveVideo: false
        };
        var dcOpen=false;
        notifyOnInit(function() {
            var optionalStruct:Dynamic = {reliable: true}
            dataChannel = peerConnection.createDataChannel("datachannel", optionalStruct);
            dataChannel.onmessage = function(e){trace("DC message:" +e.data);};
            dataChannel.onopen = function(){trace("-DC OPENED");dcOpen=true;};
            dataChannel.onclose = function(){trace("-DC closed!");};
            dataChannel.onerror = function(){trace("DC ERROR");};
            trace("intialization!");
        });
        var firstfirst=true;
        notifyOnUpdate(function() {
            if (dcOpen) {
                dcOpen=false;
                trace("sending...");
                dataChannel.send("stuff!");
            }
            if (firstfirst&&object.properties['go']) {
                user=object.properties['string'];
                first=true;
                firstfirst=false;

                // if (initiator) {
                //  peerConnection.createOffer(sdpConstraints).then(function (sdp) {
                //      peerConnection.setLocalDescription(sdp);
                //      sendNegotiation("offer", sdp);
                //      trace("SEND OFFER");
                //  }, function (data) {
                //      trace("Offer creation failure,", data);
                //  });
                // } else {
                //  peerConnection.createAnswer(sdpConstraints).then(function (sdp) {
                //      trace("Answer made.");

                //      peerConnection.setLocalDescription(sdp);

                //      sendNegotiation("answer", sdp);
                //  });
                // }
            }
            if (first) {
                first=false;
                ws = new WebSocket("ws://----------/*yes, there's an ip here*/:8080");
                ws.onopen = function() {
                    trace("ws opened!");
                    peerConnection.onicecandidate = function(event) {
                        trace("ICE offer ready");
                        if (peerConnection==null || event ==null || event.candidate == null) return;
                        sendNegotiation("candidate", event.candidate);
                    }

                    if (initiator) {
                        trace("initiating");
                        // var optionalStruct:Dynamic = {reliable: true}
                        // dataChannel = peerConnection.createDataChannel("datachannel", optionalStruct);
                        // dataChannel.onmessage = function(e){trace("DC message:" +e.data);};
                        // dataChannel.onopen = function(){trace("-DC OPENED");dcOpen=true;};
                        // dataChannel.onclose = function(){trace("-DC closed!");};
                        // dataChannel.onerror = function(){trace("DC ERROR");};
                        peerConnection.createOffer(/*sdpConstraints*/).then(function (sdp) {
                            peerConnection.setLocalDescription(sdp);
                            sendNegotiation("offer", sdp);
                            trace("SEND OFFER");
                        }, function (data) {
                            trace("Offer creation failure,", data);
                        });
                    }
                    ws.onmessage = function (data) {
                        //var info=data.data.split()
                        if (data.data=="connected!") {return;}
                        var adata = Json.parse(data.data.substring(5));

                        if (adata.action=="offer") {
                            trace("Offer recieved.");
                            // var optionalStruct:Dynamic = {reliable: true}
                            // dataChannel = peerConnection.createDataChannel("datachannel", optionalStruct);
                            // dataChannel.onmessage = function(e){trace("DC message:" +e.data);};
                            // dataChannel.onopen = function(){trace("DC OPENED");dcOpen=true;};
                            // dataChannel.onclose = function(){trace("DC CLOSED");};
                            // dataChannel.onerror = function(){trace("DC ERROR");};
                            peerConnection.setRemoteDescription(/*try variations here*/ adata.data);


                            peerConnection.createAnswer(sdpConstraints).then(function (sdp) {
                                trace("Answer made.");

                                peerConnection.setLocalDescription(sdp);

                                sendNegotiation("answer", sdp);
                            });
                        }
                        if (adata.action=="answer") {
                            trace("Answer recieved.");

                            peerConnection.setRemoteDescription(/*try variations here*/ adata.data);
                        }
                        if (adata.action=="candidate") {
                            trace("ICE candidate recieved, looks like:",adata);
                            var soItDoesntComplain:Dynamic = adata.data;
                            peerConnection.addIceCandidate(soItDoesntComplain);
                        }
                    }
                }

            }
            if (ready) {
                trace("connected to net");
            }
        });

        // notifyOnRemove(function() {
        // });
    }
}

您會發現有很多代碼被注釋掉了–我在移動dataChannel的創建即將到期。

為了更好地了解問題所在,這里分別是接收和發起客戶端的控制台輸出:

在此處輸入圖片說明

在此處輸入圖片說明

如果您想知道,notifyOnInit會獲取一個函數,該函數在開始時執行一次,而notifyOnUpdate會獲取定期調用的函數。 給定用戶名時,object.properties ['go']由另一個類設置。

JS api基本上是相同的(據我所知,我過去從未使用過WebRTC),我還沒有發現任何差異,我非常確定我的問題是我的錯,而不是哈克的。

謝謝那些回答。

這不是答案。 着急點。

peerCoonection.createOffer()
peerCoonection.createAnswer()
peerCoonection.setLocalDescription()
peerCoonection.setRemoteDescription()
peerCoonection.addIceCandidate()

需要等待。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM