Created
October 31, 2012 22:48
-
-
Save anantn/3990456 to your computer and use it in GitHub Desktop.
Data Channel Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Assume we've connected a PeerConnection with a friend - usually with audio | |
* and/or video. For the time being, always at least include a 'fake' audio | |
* stream - this will be fixed soon. | |
* | |
* connectDataConnection is a temporary function that will soon disappear. | |
* The two sides need to use inverted copies of the two numbers (eg. 5000, 5001 | |
* on one side, 5001, 5000 on the other) | |
*/ | |
pc.connectDataConnection(5001, 5000); | |
function handle_new(channel) { | |
channel.binaryType = "blob"; | |
channel.onmessage = function(evt) { | |
if (evt.data instanceof Blob) { | |
console.log("I received a blob"); | |
// assign data to an image, save in a file, etc | |
} else { | |
console.log("I got a message: " + evt.data); | |
} | |
}; | |
channel.onopen = function() { | |
// We can now send, like WebSockets | |
channel.send("The channel is open!"); | |
}; | |
channel.onclose = function() { | |
console.log("pc1 onclose fired"); | |
}; | |
}; | |
/* For when the other side creates a channel */ | |
pc.onDataChannel = handle_new; | |
channel = pc.createDataChannel("My Datastream",{}); | |
if (channel) { | |
handle_new(channel); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment