Skip to content

Instantly share code, notes, and snippets.

@peterhpchen
Created April 4, 2014 02:22
Show Gist options
  • Save peterhpchen/9966907 to your computer and use it in GitHub Desktop.
Save peterhpchen/9966907 to your computer and use it in GitHub Desktop.
RTCPeerConnection
var webRTCHub = $.connection.rtcHub;
webRTCHub.client.sendMessage = sendMessage;
$.connection.hub.start(startHub);
<video id="videoLocal" autoplay></video>
<video id="videoRemote" autoplay></video>
<button id="startBtn" disabled="disabled">Start Call</button>
<script src="Scripts/adapter.js"></script>
<script src="Scripts/jquery-1.6.4.js"></script>
<script src="Scripts/jquery.signalR-2.0.3.js"></script>
<script src="signalr/hubs"></script>
function sendMessage(data) {
var message = JSON.parse(data);
var connection = localPeerConnection || initialRTCPeerConnectionAPI();
// An SDP message contains connection and media information, and is either an 'offer' or an 'answer'
if (message.sdp) {
connection.setRemoteDescription(new RTCSessionDescription(message.sdp), function () {
if (connection.remoteDescription.type == 'offer') {
trace('received offer, sending answer...');
// Add our stream to the connection to be shared
connection.addStream(localMediaStream);
// Create an SDP response
connection.createAnswer(function (desc) {
// Which becomes our local session description
connection.setLocalDescription(desc, function () {
// And send it to the originator, where it will become their RemoteDescription
webRTCHub.server.send(JSON.stringify({ 'sdp': connection.localDescription }));
});
}, function (error) { trace('Error creating session description: ' + error); });
} else if (connection.remoteDescription.type == 'answer') {
trace('got an answer');
}
});
} else if (message.candidate) {
trace('adding ice candidate...');
connection.addIceCandidate(new RTCIceCandidate(message.candidate));
}
localPeerConnection = connection;
}
using System;
using System.Web;
using Microsoft.AspNet.SignalR;
namespace WebRTCDemoWithSignalR
{
public class rtcHub : Hub
{
public void Send(string message)
{
// Call the sendMessage method to update clients.
Clients.Others.sendMessage(message);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment