WebRTC канал передачи данных не отправит? - PullRequest
0 голосов
/ 09 декабря 2018

В последнее время я пытался внедрить каналы данных WebRTC в Haxe, но столкнулся с большими трудностями.Когда я использую

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.

Чтобы лучше понять, в чем проблема, вотконсольный вывод для принимающих и инициирующих клиентов соответственно:

enter image description here

enter image description here

В случаевам интересно, notifyOnInit получает функцию, которая выполняется один раз в начале, а notifyOnUpdate получает функцию, вызываемую через регулярный интервал.object.properties ['go'] устанавливается другим классом, когда дается имя пользователя.

API JS в основном одинаков (насколько я могу судить, я вообще не использовал WebRTC вПрошлое), я еще не заметил никаких различий, и я очень уверен, что моя проблема - моя вина, а не Хэйкс.

Спасибо тем, кто отвечает.

1 Ответ

0 голосов
/ 09 декабря 2018

это не ответ.Тревожная точка.

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

ждут, требуется.

...