Can we use SignalR with WebRTC video calling in WebForms

12,448

The logic is very similar to the signalR tutorial. Except your messages are the messages that WebRTC needs to communicate to connect.

Here is an example I wrote up. It does a broadcast to all clients that are connected via the signalR hub. However, it is very simple to set it up to where only certain users communicate with others. Here is a more flushed out example but it uses MVC.

Basic signalling logic done client side:

<script type="text/javascript">
        var signal = $.connection.webRTCHub;
        var ready = false;
        //set our client handler
        signal.client.broadcastMessage = function (from, message) {
            //handle your message that you received
        }

       //start the hub for long polling so it does not close    
       $.connection.hub.start({ transport: ['longPolling'] }).done(function () {
            ready = true;
        });
        //only send a message when we are ready
        var sendMessage = function (message) {
            if (!ready)
                setTimeout(sendMessage, 100, message);
            else
                signal.server.send(name, message);
        }

    </script>

Basic Hub Class to forward the messages

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;

namespace SignalRWebRTCExample
{
    public class WebRTCHub : Hub
    {
        //executed from javascript side via signal.server.send(name, message);
        public void Send(string from, string message)
        {
            //Code executed client side, aka, makes message available to client
            Clients.All.broadcastMessage(from, message);
        }
    }
}

Basic start up class to start signalr

using System;
using System.Threading.Tasks;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(SignalRWebRTCExample.Startup))]

namespace SignalRWebRTCExample
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

DISCLAIMER: This is very rough, but the example "works"(streams send between clients). This code is not optimized and not ideal. There are many awesome features in SignalR not utilized that probably could make it better and more efficient.

Share:
12,448
Admin
Author by

Admin

Updated on June 16, 2022

Comments

  • Admin
    Admin almost 2 years

    It might be witless since I am newbie. I want to include WebRTC video calling feature using SignalR in my ASP.NET WebForms project for registered and online users. I tried to search more than a week for walk-through/examples for using SignalR with WebRTC in Webforms but I always found examples in MVC. Can't we use SignalR with WebRTC in WebForms? If we can use, then can anyone provide/help me with a very simple and basic walk-through/example of it.