WebRTC数据通道不会发送?

时间:2018-12-09 00:03:26

标签: javascript webrtc haxe

最近我一直在尝试在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创建时即将到期。

要更好地了解问题所在,以下分别是接收和发起客户端的控制台输出:

enter image description here

enter image description here

如果您想知道,notifyOnInit会获得一个在开始时执行一次的函数,而notifyOnUpdate会定期获取一个调用的函数。指定用户名时,object.properties ['go']由其他类设置。

JS api基本上是相同的(据我所知,我过去从未使用过WebRTC),我还没有发现任何差异,我非常确定我的问题是错误而不是Haxe的。

谢谢回答的人。

1 个答案:

答案 0 :(得分:0)

这不是答案。 焦虑点。

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

需要等待。