This repository was archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/muaz-khan/WebRTC-ASPNET-MVC
- Loading branch information
0 parents
commit d922a09
Showing
45 changed files
with
3,654 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
/* Muaz Khan : Nov 14, 2012!! | ||
* @muazk: http://twitter.com/muazkh | ||
* Github: github.com/muaz-khan | ||
******************************/ | ||
using System; | ||
using System.Linq; | ||
using System.Web.Mvc; | ||
using WebRTCExperiment.Models; | ||
using System.Web.WebPages; | ||
|
||
namespace WebRTCExperiment.Controllers | ||
{ | ||
public class WebRTCController : Controller | ||
{ | ||
public ActionResult Index() | ||
{ | ||
//return RedirectPermanent("https://www.webrtc-experiment.com/"); | ||
return View(); | ||
} | ||
|
||
readonly WebRTCDataContext _db = new WebRTCDataContext(); | ||
|
||
#region Create / Join room | ||
|
||
[HttpPost] | ||
public JsonResult CreateRoom(string ownerName, string roomName, string partnerEmail = null) | ||
{ | ||
if (ownerName.IsEmpty() || roomName.IsEmpty()) return Json(false); | ||
|
||
back: | ||
string token = RandomNumbers.GetRandomNumbers(); | ||
if (_db.Rooms.Any(r => r.Token == token)) goto back; | ||
|
||
back2: | ||
string ownerToken = RandomNumbers.GetRandomNumbers(); | ||
if (_db.Rooms.Any(r => r.OwnerToken == ownerToken)) goto back2; | ||
|
||
var room = new Room | ||
{ | ||
Token = token, | ||
Name = roomName.GetValidatedString(), | ||
OwnerName = ownerName.GetValidatedString(), | ||
OwnerToken = ownerToken, | ||
LastUpdated = DateTime.Now, | ||
SharedWith = partnerEmail.IsEmpty() ? "Public" : partnerEmail, | ||
Status = Status.Available | ||
}; | ||
|
||
_db.Rooms.InsertOnSubmit(room); | ||
_db.SubmitChanges(); | ||
|
||
return Json(new | ||
{ | ||
roomToken = room.Token, | ||
ownerToken = room.OwnerToken | ||
}); | ||
} | ||
|
||
[HttpPost] | ||
public JsonResult JoinRoom(string participant, string roomToken, string partnerEmail = null) | ||
{ | ||
if (participant.IsEmpty() || roomToken.IsEmpty()) return Json(false); | ||
|
||
var room = _db.Rooms.FirstOrDefault(r => r.Token == roomToken); | ||
if (room == null) return Json(false); | ||
|
||
if (room.SharedWith != "Public") | ||
{ | ||
if (partnerEmail.IsEmpty()) return Json(false); | ||
if (room.SharedWith != partnerEmail) return Json(false); | ||
} | ||
|
||
back: | ||
string participantToken = RandomNumbers.GetRandomNumbers(); | ||
if (_db.Rooms.Any(r => r.OwnerToken == participantToken)) goto back; | ||
|
||
room.ParticipantName = participant.GetValidatedString(); | ||
room.ParticipantToken = participantToken; | ||
room.LastUpdated = DateTime.Now; | ||
room.Status = Status.Active; | ||
|
||
_db.SubmitChanges(); | ||
|
||
return Json(new | ||
{ | ||
participantToken, | ||
friend = room.OwnerName | ||
}); | ||
} | ||
|
||
#endregion | ||
|
||
#region Search rooms | ||
|
||
[HttpPost] | ||
public JsonResult SearchPublicRooms(string partnerEmail) | ||
{ | ||
if (!partnerEmail.IsEmpty()) return SearchPrivateRooms(partnerEmail); | ||
|
||
var rooms = _db.Rooms.Where(r => r.SharedWith == "Public" && r.Status == Status.Available && r.LastUpdated.AddMinutes(1) > DateTime.Now).OrderByDescending(o => o.ID); | ||
return Json( | ||
new | ||
{ | ||
rooms = rooms.Select(r => new | ||
{ | ||
roomName = r.Name, | ||
ownerName = r.OwnerName, | ||
roomToken = r.Token | ||
}), | ||
availableRooms = rooms.Count(), | ||
publicActiveRooms= _db.Rooms.Count(r => r.Status == Status.Active && r.LastUpdated.AddMinutes(1) > DateTime.Now && r.SharedWith == "Public"), | ||
privateAvailableRooms = _db.Rooms.Count(r => r.Status == Status.Available && r.LastUpdated.AddMinutes(1) > DateTime.Now && r.SharedWith != "Public") | ||
} | ||
); | ||
} | ||
|
||
[HttpPost] | ||
public JsonResult SearchPrivateRooms(string partnerEmail) | ||
{ | ||
if (partnerEmail.IsEmpty()) return Json(false); | ||
|
||
var rooms = _db.Rooms.Where(r => r.SharedWith == partnerEmail && r.Status == Status.Available && r.LastUpdated.AddMinutes(1) > DateTime.Now).OrderByDescending(o => o.ID); | ||
return Json(new | ||
{ | ||
rooms = rooms.Select(r => new | ||
{ | ||
roomName = r.Name, | ||
ownerName = r.OwnerName, | ||
roomToken = r.Token | ||
}) | ||
}); | ||
} | ||
|
||
#endregion | ||
|
||
#region SDP Messages | ||
|
||
[HttpPost] | ||
public JsonResult PostSDP(string sdp, string roomToken, string userToken) | ||
{ | ||
if (sdp.IsEmpty() || roomToken.IsEmpty() || userToken.IsEmpty()) return Json(false); | ||
|
||
var sdpMessage = new SDPMessage | ||
{ | ||
SDP = sdp, | ||
IsProcessed = false, | ||
RoomToken = roomToken, | ||
Sender = userToken | ||
}; | ||
|
||
_db.SDPMessages.InsertOnSubmit(sdpMessage); | ||
_db.SubmitChanges(); | ||
|
||
return Json(true); | ||
} | ||
|
||
[HttpPost] | ||
public JsonResult GetSDP(string roomToken, string userToken) | ||
{ | ||
if (roomToken.IsEmpty() || userToken.IsEmpty()) return Json(false); | ||
|
||
var sdp = _db.SDPMessages.FirstOrDefault(s => s.RoomToken == roomToken && s.Sender != userToken && !s.IsProcessed); | ||
|
||
if(sdp == null) return Json(false); | ||
|
||
sdp.IsProcessed = true; | ||
_db.SubmitChanges(); | ||
|
||
return Json(new | ||
{ | ||
sdp = sdp.SDP | ||
}); | ||
} | ||
|
||
#endregion | ||
|
||
#region ICE Candidates | ||
|
||
[HttpPost] | ||
public JsonResult PostICE(string candidate, string label, string roomToken, string userToken) | ||
{ | ||
if (candidate.IsEmpty() || label.IsEmpty() || roomToken.IsEmpty() || userToken.IsEmpty()) return Json(false); | ||
|
||
var candidateTable = new CandidatesTable | ||
{ | ||
Candidate = candidate, | ||
Label = label, | ||
IsProcessed = false, | ||
RoomToken = roomToken, | ||
Sender = userToken | ||
}; | ||
|
||
_db.CandidatesTables.InsertOnSubmit(candidateTable); | ||
_db.SubmitChanges(); | ||
|
||
return Json(true); | ||
} | ||
|
||
[HttpPost] | ||
public JsonResult GetICE(string roomToken, string userToken) | ||
{ | ||
if (roomToken.IsEmpty() || userToken.IsEmpty()) return Json(false); | ||
|
||
var candidate = _db.CandidatesTables.FirstOrDefault(c => c.RoomToken == roomToken && c.Sender != userToken && !c.IsProcessed); | ||
if (candidate == null) return Json(false); | ||
|
||
candidate.IsProcessed = true; | ||
_db.SubmitChanges(); | ||
|
||
return Json(new | ||
{ | ||
candidate = candidate.Candidate, | ||
label = candidate.Label | ||
}); | ||
} | ||
|
||
#endregion | ||
|
||
#region Extras | ||
|
||
[HttpPost] | ||
public JsonResult GetParticipant(string roomToken, string ownerToken) | ||
{ | ||
if (roomToken.IsEmpty() || ownerToken.IsEmpty()) return Json(false); | ||
|
||
var room = _db.Rooms.FirstOrDefault(r => r.Token == roomToken && r.OwnerToken == ownerToken); | ||
if (room == null) return Json(false); | ||
|
||
room.LastUpdated = DateTime.Now; | ||
_db.SubmitChanges(); | ||
|
||
if (room.ParticipantName.IsEmpty()) return Json(false); | ||
return Json(new { participant = room.ParticipantName }); | ||
} | ||
|
||
[HttpPost] | ||
public JsonResult Stats() | ||
{ | ||
var numberOfRooms = _db.Rooms.Count(); | ||
var numberOfPublicRooms = _db.Rooms.Count(r => r.SharedWith == "Public"); | ||
var numberOfPrivateRooms = _db.Rooms.Count(r => r.SharedWith != "Public"); | ||
var numberOfEmptyRooms = _db.Rooms.Count(r => r.ParticipantName == null); | ||
var numberOfFullRooms = _db.Rooms.Count(r => r.ParticipantName != null); | ||
return Json(new { numberOfRooms, numberOfPublicRooms, numberOfPrivateRooms, numberOfEmptyRooms, numberOfFullRooms }); | ||
} | ||
|
||
#endregion | ||
} | ||
struct Status | ||
{ | ||
public const string Available = "Available"; | ||
public const string Active = "Active"; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* Muaz Khan – http://twitter.com/muazkh */ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace WebRTCExperiment | ||
{ | ||
public class RandomNumbers | ||
{ | ||
internal static string GetRandomNumbers(int length = 6) | ||
{ | ||
var values = new byte[length]; | ||
var rnd = new Random(); | ||
rnd.NextBytes(values); | ||
return values.Aggregate(string.Empty, (current, v) => current + v.ToString()); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%@ Application Codebehind="Global.asax.cs" Inherits="WebRTCExperiment.MvcApplication" Language="C#" %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Web; | ||
using System.Web.Mvc; | ||
using System.Web.Routing; | ||
|
||
namespace WebRTCExperiment | ||
{ | ||
// Note: For instructions on enabling IIS6 or IIS7 classic mode, | ||
// visit http://go.microsoft.com/?LinkId=9394801 | ||
|
||
public class MvcApplication : System.Web.HttpApplication | ||
{ | ||
public static void RegisterGlobalFilters(GlobalFilterCollection filters) | ||
{ | ||
filters.Add(new HandleErrorAttribute()); | ||
} | ||
|
||
public static void RegisterRoutes(RouteCollection routes) | ||
{ | ||
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | ||
|
||
routes.MapRoute("message", "message", new { controller = "Email", action = "SendEmail" }); | ||
|
||
routes.MapRoute( | ||
"Default", // Route name | ||
"{controller}/{action}/{id}", // URL with parameters | ||
new { controller = "WebRTC", action = "Index", id = UrlParameter.Optional } // Parameter defaults | ||
); | ||
|
||
} | ||
|
||
protected void Application_Start() | ||
{ | ||
AreaRegistration.RegisterAllAreas(); | ||
|
||
RegisterGlobalFilters(GlobalFilters.Filters); | ||
RegisterRoutes(RouteTable.Routes); | ||
} | ||
|
||
/* To allow Cross-Origin Requests from: https://webrtc-experiment.appspot.com/ */ | ||
protected void Application_BeginRequest(object sender, EventArgs e) | ||
{ | ||
Response.AddHeader("Access-Control-Allow-Origin", "*"); | ||
} | ||
} | ||
} |
Oops, something went wrong.