Skip to content

Instantly share code, notes, and snippets.

@anantn
Created October 31, 2012 22:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save anantn/3990456 to your computer and use it in GitHub Desktop.
Save anantn/3990456 to your computer and use it in GitHub Desktop.
Data Channel Example
/**
* 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