0.7.2 • Published 12 years ago

peer-connection-shim v0.7.2

Weekly downloads
2
License
-
Repository
github
Last release
12 years ago

peer-connection-shim

Emulating peer connection in node & browser

Example

var PeerConnection = require("peer-connection-shim")
    , signalingChannel = createSignalingChannel()
    , pc
    , channel

// call start(true) to initiate
function start(isInitiator) {
    // uri is used to relay connections through
    pc = PeerConnection({
        uri: "http://discoverynetwork.co/relay"
    })

    if (isInitiator) {
        pc.createOffer(localDescriptionCreated)
    } else {
        pc.createAnswer(localDescriptionCreated)
    }

    if (isInitiator) {
        // create data channel and setup chat
        channel = pc.createDataChannel("chat")
        setupChat()
    } else {
        // setup chat on incoming data channel
        pc.ondatachannel = function (evt) {
            channel = evt.channel
            setupChat()
        }
    }
}

function localDescriptionCreated(desc) {
    pc.setLocalDescription(desc, function () {
        signalingChannel.emit("sdp", pc.localDescription)
    })
}

signalingChannel.on("sdp", function (sdp) {
    if (!pc) {
        start(false)
    }

    pc.setRemoteDescription(sdp)
}

function setupChat() {
    channel.onopen = function () {
        // e.g. enable send button
        enableChat(channel)
    }

    channel.onmessage = function (evt) {
        showChatMessage(evt.data)
    }
}

function sendChatMessage(msg) {
    channel.send(msg)
}

Installation

npm install peer-connection-shim

Contributors

  • Raynos

MIT Licenced

0.7.2

12 years ago

0.7.1

12 years ago

0.7.0

12 years ago

0.6.0

12 years ago

0.5.0

12 years ago

0.4.1

12 years ago

0.4.0

12 years ago

0.3.1

12 years ago

0.3.0

12 years ago

0.2.0

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago