【问题标题】:Typescript and WebRTC, 'createDataChannel' does not exist on type 'RTCPeerConnection'Typescript 和 WebRTC,“RTCPeerConnection”类型上不存在“createDataChannel”
【发布时间】:2019-02-21 18:15:45
【问题描述】:

我正在尝试使用 WebRTC 和 typescript,因为 typescript 很棒,而 webrtc 甚至更棒...我正在使用 VS Code 在 Angular 6 的上下文中执行此操作。

这似乎不是天作之合(可能是因为我不了解其中任何一个组成部分)。

我很难找到任何使用 typescript for webrtc 的示例,但是当我开始尝试翻译一个普通的 javascript 示例时,我注意到方法和属性似乎至少在 Visual Studio Code 中被识别。但我没有走多远。

您可以在下面看到我试图将一个简单的示例翻译成打字稿。

createConnection ()
{       
    // window.localConnection = localConnection = new RTCPeerConnection(servers);
    this.localConnection = new RTCPeerConnection();
    console.log(this.sendChannel);

    // sendChannel = localConnection.createDataChannel('sendDataChannel');
    var sendChannel = this.localConnection.createDataChannel('sendDataChannel');
    console.log(this.sendChannel);

    // localConnection.onicecandidate = e => {
    //   onIceCandidate(localConnection, e);
    // };
    // sendChannel.onopen = onSendChannelStateChange;
    // sendChannel.onclose = onSendChannelStateChange;

    // window.remoteConnection = remoteConnection = new RTCPeerConnection(servers);
    // console.log('Created remote peer connection object remoteConnection');

    // remoteConnection.onicecandidate = e => {
    //   onIceCandidate(remoteConnection, e);
    // };
    // remoteConnection.ondatachannel = receiveChannelCallback;

    // localConnection.createOffer().then(
    //   gotDescription1,
    //   onCreateSessionDescriptionError
    // );
    // startButton.disabled = true;
    // closeButton.disabled = false;
  }

关于 sendChannel 的定义我得到这个错误:

src/app/app.component.ts(48,44):错误 TS2339:“RTCPeerConnection”类型上不存在属性“createDataChannel”。

第一个日志的控制台日志是:

RTCPeerConnection { localDescription:null,currentLocalDescription:null,pendingLocalDescription:null,remoteDescription:null,currentRemoteDescription:null,pendingRemoteDescription:null,signalingState:“稳定”,canTrickleIceCandidates:null,iceGatheringState:“new”,iceConnectionState:“new” }

第二次记录为 null。

感谢任何提示或提示,了解我为什么会收到此错误以及如何修复它。

更新:添加了 git repositoryng se 来报告问题。

git clone https://github.com/wokawaka/typescriptwebrtctest
npm install
ng serve --open

【问题讨论】:

  • 什么浏览器?例如,Edge 不支持 RTCDataChannel。
  • 我在 VS Code 中遇到错误,但使用的是 Firefox(最新版本)。

标签: typescript webrtc


【解决方案1】:

看起来 TypeScript 3.0.0 和更新版本有 createDataChannel 的声明,但旧版本没有。尝试升级 TypeScript。还有 @types/webrtc 包,它在进入 TypeScript 标准库之前可能有更新的声明。

【讨论】:

  • 添加了 @types/webrtc 但这没有帮助。我会尝试升级。
  • 我使用的是 Typescript 3.0.3,一直都是。
  • 我也在页面中添加了adapter.js,但没有变化。
  • 那我也不知道怎么回事。如果你发布一个为你重现问题的存储库,我会搞砸的。
  • 添加了 git repo,将错误重现到原始问题。只需重做列出的这三个步骤,我就会得到“createDataChannel”不存在错误。谢谢你的时间:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-13
  • 2016-03-30
  • 1970-01-01
  • 2021-05-11
  • 2023-04-02
  • 2018-08-17
  • 1970-01-01
相关资源
最近更新 更多