How to use wrtc - 10 common examples

To help you get started, we’ve selected a few wrtc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github UWNetworksLab / uProxy-p2p / src / lib / socks / bin / webrtc.ts View on Github external
/// 

import * as node_server from '../node/server';
import * as node_socket from '../node/socket';
import * as session from '../session';

import * as wrtc from 'wrtc';

const SERVER_ADDRESS = '0.0.0.0';
const SERVER_PORT = 9999;

const getter = new wrtc.RTCPeerConnection();
const giver = new wrtc.RTCPeerConnection();

getter.onicecandidate = (event: any) => {
  if (event.candidate) {
    console.info('getter candidate', event.candidate.candidate);
    giver.addIceCandidate(event.candidate);
  }
}
giver.onicecandidate = (event: any) => {
  if (event.candidate) {
    console.info('giver candidate', event.candidate.candidate);
    getter.addIceCandidate(event.candidate);
  }
}

// Create a SocksSession in response to each new datachannel.
// NOTE: because onopen datachannel events don't fire for the
github ShareIt-project / WebP2P.io / dist / WebP2P_require.js View on Github external
function createPeerConnection(sessionID, callbackType, callback)
  {
    var pc = new RTCPeerConnection
    (
      {iceServers: [{url: 'stun:'+stun_server}]},
      {optional: [{DtlsSrtpKeyAgreement: true}]}
    );

    pc.addEventListener('icecandidate', function(event)
    {
      // There's a candidate, ignore it
      if(event.candidate)
        return;

      // There's no candidate, send the full SDP
      var type = this.localDescription.type;
      var sdp  = this.localDescription.sdp;

      if(type == callbackType)
github ShareIt-project / WebP2P.io / dist / WebP2P.js View on Github external
function createPeerConnection(sessionID, callbackType, callback)
  {
    var pc = new RTCPeerConnection
    (
      {iceServers: [{url: 'stun:'+stun_server}]},
      {optional: [{DtlsSrtpKeyAgreement: true}]}
    );

    pc.addEventListener('icecandidate', function(event)
    {
      // There's a candidate, ignore it
      if(event.candidate)
        return;

      // There's no candidate, send the full SDP
      var type = this.localDescription.type;
      var sdp  = this.localDescription.sdp;

      if(type == callbackType)
github local-group-games / web-udp / packages / client / src / provider / web-rtc / rtc-connection-provider.ts View on Github external
break
      }
      // Route session descriptions to peers.
      case "OFFER":
      case "ANSWER": {
        const {
          src,
          payload: { sdp },
        } = message
        const peer = this.peers[src] || this.addPeer(src)

        let description: RTCSessionDescription

        try {
          description = new RTCSessionDescription(sdp)
        } catch (e) {
          throw new Error("Invalid SDP")
        }

        peer.setRemoteDescription(description)

        if (message.type === "OFFER") {
          const sdp = await peer.answer()

          this.onPeerSDP(sdp, src)
        }

        break
      }
      case "KEEP_ALIVE": {
        this.transport.send({
github local-group-games / web-udp / lib / server.js View on Github external
peer.addIceCandidate(ice);

          break;
        }
        // Route session descriptions to peers.
        case "OFFER":
        case "ANSWER": {
          var _src = message.src,
            sdp = message.payload.sdp;

          var _peer = _this._peers[_src] || _this._addPeer(_src);

          var description = void 0;

          try {
            description = new wrtc.RTCSessionDescription(sdp);
          } catch (e) {
            throw new Error("Invalid SDP");
          }

          _peer.setRemoteDescription(description);

          if (message.type === "OFFER") {
            _peer.answer();
          }

          break;
        }
        default:
          break;
      }
    };
github boplish / core / js / adapter.js View on Github external
if (that.iceGatheringState === 'complete') {
                    var nullCandidate = new RTCIceCandidate();
                    nullCandidate.candidate = null;
                    that.onicecandidate(nullCandidate);
                } else {
                    checkIceState();
                }
            }, 1000).unref();
        })(); // miserable hack, waiting for https://github.com/js-platform/node-webrtc/issues/44
    };
    util.inherits(RTCPeerConnection, webrtc.RTCPeerConnection);

    GLOBAL.RTCPeerConnection = RTCPeerConnection;
    GLOBAL.RTCIceCandidate = webrtc.RTCIceCandidate;
    GLOBAL.RTCSessionDescription = webrtc.RTCSessionDescription;
    GLOBAL.DataChannel = webrtc.DataChannel;

    var CustomWebSocket = function(url) {
        if (!(this instanceof WebSocket)) {
            return new CustomWebSocket(url);
        }
        var websocketClient = new WebSocketClient();
        var that = this;
        websocketClient.on('connect', function(connection) {
            connection.on('message', function(msg) {
                msg.data = msg.utf8Data;
                that.onmessage(msg);
            });
            that.send = function(msg) {
                connection.send(msg);
            };
        });
github udevbe / westfield / server / node / example.livestream / compositor-server.js View on Github external
function clientStreamDescription(streamSource, description) {

    const signal = JSON.parse(description);

    if (signal.sdp) {

        streamSource.implementation.peerConnection.setRemoteDescription(new webrtc.RTCSessionDescription(signal.sdp)).then(() => {
            return streamSource.implementation.peerConnection.createAnswer();
        }).then((desc) => {
            return streamSource.implementation.peerConnection.setLocalDescription(desc);
        }).then(() => {
            streamSource.server_stream_description(JSON.stringify({"sdp": streamSource.implementation.peerConnection.localDescription}));
        }).catch((error) => {
            console.error(error);
            streamSource.client.close();
        });

    } else if (signal.candidate) {
        streamSource.implementation.peerConnection.addIceCandidate(new webrtc.RTCIceCandidate(signal.candidate)).catch((error) => {
            console.error("Error: Failure during addIceCandidate()", error);
            streamSource.client.close();
        });
    }
github udevbe / westfield / server / node / example.livestream / compositor-server.js View on Github external
if (signal.sdp) {

        streamSource.implementation.peerConnection.setRemoteDescription(new webrtc.RTCSessionDescription(signal.sdp)).then(() => {
            return streamSource.implementation.peerConnection.createAnswer();
        }).then((desc) => {
            return streamSource.implementation.peerConnection.setLocalDescription(desc);
        }).then(() => {
            streamSource.server_stream_description(JSON.stringify({"sdp": streamSource.implementation.peerConnection.localDescription}));
        }).catch((error) => {
            console.error(error);
            streamSource.client.close();
        });

    } else if (signal.candidate) {
        streamSource.implementation.peerConnection.addIceCandidate(new webrtc.RTCIceCandidate(signal.candidate)).catch((error) => {
            console.error("Error: Failure during addIceCandidate()", error);
            streamSource.client.close();
        });
    }
}
github local-group-games / web-udp / lib / server.js View on Github external
value: function _addPeer(id) {
        var _this2 = this;

        if (this._peers[id]) {
          throw new Error(
            "RTCPeer with id " + id + " already exists."
          );
        }

        var peer = new RTCPeer({
          pc: new wrtc.RTCPeerConnection(RTC_PEER_CONNECTION_OPTIONS),
          onChannel: this._onPeerChannel,
          onClose: function onClose() {
            return _this2._onPeerClose(id);
          },
          onSDP: function onSDP(sdp) {
            return _this2._onPeerSDP(sdp, id);
          },
          onICE: function onICE(ice) {
            return _this2._onPeerICE(ice, id);
          }
        });

        this._peers[id] = peer;

        return peer;
      }

wrtc

Standards-compliant WebRTC implementation for Node

BSD-2-Clause
Latest version published 3 years ago

Package Health Score

56 / 100
Full package analysis