Skip to content

Commit

Permalink
Initial Version
Browse files Browse the repository at this point in the history
  • Loading branch information
vavjeeva committed Aug 14, 2019
1 parent 3e1307b commit 52314bf
Show file tree
Hide file tree
Showing 36 changed files with 7,630 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ScreenCastR.App/App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Startup).Assembly">
<NotFoundContent>
<p>Sorry, there's nothing at this address.</p>
</NotFoundContent>
</Router>
</CascadingAuthenticationState>
55 changes: 55 additions & 0 deletions ScreenCastR.App/Hub/ScreenCastHub.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ScreenCastApp
{
public class ScreenCastHub : Hub
{
private readonly ScreenCastManager screenCastManager;
private const string AGENT_GROUP_PREFIX = "AGENT_";

public ScreenCastHub(ScreenCastManager screenCastManager)
{
this.screenCastManager = screenCastManager;
}

public async Task AddScreenCastAgent(string agentName)
{
await Clients.Others.SendAsync("NewScreenCastAgent", agentName);
await Groups.AddToGroupAsync(Context.ConnectionId, AGENT_GROUP_PREFIX + agentName);
}

public async Task RemoveScreenCastAgent(string agentName)
{
await Clients.Others.SendAsync("RemoveScreenCastAgent", agentName);
await Groups.RemoveFromGroupAsync(Context.ConnectionId, AGENT_GROUP_PREFIX + agentName);
screenCastManager.RemoveViewerByAgent(agentName);
}

public async Task AddScreenCastViewer(string agentName)
{
await Groups.AddToGroupAsync(Context.ConnectionId, agentName);
screenCastManager.AddViewer(Context.ConnectionId, agentName);
await Clients.Groups(AGENT_GROUP_PREFIX + agentName).SendAsync("NewViewer");
}

public async Task RemoveScreenCastViewer(string agentName)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, agentName);
screenCastManager.RemoveViewer(Context.ConnectionId);
if(!screenCastManager.IsViewerExists(agentName))
await Clients.Groups(AGENT_GROUP_PREFIX + agentName).SendAsync("NoViewer");
}

public async Task StreamCastData(IAsyncEnumerable<string> stream, string agentName)
{
await foreach (var item in stream)
{
await Clients.Group(agentName).SendAsync("OnStreamCastDataReceived", item);
}
}
}
}
5 changes: 5 additions & 0 deletions ScreenCastR.App/Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@page "/"

<ScreenCastView></ScreenCastView>


96 changes: 96 additions & 0 deletions ScreenCastR.App/Pages/ScreenCastView.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
@using Microsoft.AspNetCore.SignalR.Client

<div class="card border-primary mb-3" style="max-width: 20rem;">
@if (agents.Count > 0)
{
@foreach (var agent in agents)
{
<div class="card-body">
<div>
<h3 class="badge-primary">
@agent
</h3>
<div style="padding-top:10px">
<button id="ViewCast" disabled="@(IsViewingCastOf(agent))" class="btn btn-success btn-sm" @onclick="@(() => OnViewCastClicked(agent))">
View cast
</button>

<button id="StopViewCast" disabled="@(!IsViewingCastOf(agent))" class="btn btn-warning btn-sm" @onclick="@(() => OnStopViewCastClicked(agent))">
Stop cast
</button>
</div>
</div>
</div>
}
}
else
{
<div class="card-body">
<h3 class="card-header badge-warning">No Screencast Agents casting the screen now!</h3>
</div>
}
</div>
<div class="border">
<img id='screenImage' src="@imageSource" />
</div>
@code{

private List<string> agents = new List<string>();

HubConnection connection;
string imageSource = null;
string CurrentViewCastAgent = null;

protected async override Task OnInitializedAsync()
{
connection = new HubConnectionBuilder()
.WithUrl("https://localhost:5001/ScreenCastHub")
.Build();

connection.On<string>("NewScreenCastAgent", NewScreenCastAgent);
connection.On<string>("RemoveScreenCastAgent", RemoveScreenCastAgent);
connection.On<string>("OnStreamCastDataReceived", OnStreamCastDataReceived);

await connection.StartAsync();
}

bool IsViewingCastOf(string agentName)
{
return agentName == CurrentViewCastAgent;
}

void NewScreenCastAgent(string agentName)
{
agents.Add(agentName);
StateHasChanged();
}

void RemoveScreenCastAgent(string agentName)
{
agents.Remove(agentName);
imageSource = null;
CurrentViewCastAgent = null;
StateHasChanged();
}

void OnStreamCastDataReceived(string streamData)
{
imageSource = streamData;
StateHasChanged();
}

private async Task OnViewCastClicked(string agentName)
{
CurrentViewCastAgent = agentName;
await connection.InvokeAsync("AddScreenCastViewer", agentName);
}

private async Task OnStopViewCastClicked(string agentName)
{
CurrentViewCastAgent = null;
await connection.InvokeAsync("RemoveScreenCastViewer", agentName);
imageSource = null;
StateHasChanged();
}

}
20 changes: 20 additions & 0 deletions ScreenCastR.App/Pages/_Host.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@page "/"
@namespace ScreenCastApp.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>ScreenCastApp</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
</head>
<body>
<app>@(await Html.RenderComponentAsync<App>())</app>

<script src="_framework/blazor.server.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions ScreenCastR.App/Pages/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@layout MainLayout
28 changes: 28 additions & 0 deletions ScreenCastR.App/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ScreenCastApp
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
11 changes: 11 additions & 0 deletions ScreenCastR.App/ScreenCastApp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>preview</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="3.0.0-preview8.19405.7" />
</ItemGroup>
</Project>
25 changes: 25 additions & 0 deletions ScreenCastR.App/ScreenCastApp.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29123.89
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScreenCastApp", "ScreenCastApp.csproj", "{EA66AEBB-72BB-4A5D-993C-54D730BD1685}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EA66AEBB-72BB-4A5D-993C-54D730BD1685}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EA66AEBB-72BB-4A5D-993C-54D730BD1685}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EA66AEBB-72BB-4A5D-993C-54D730BD1685}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EA66AEBB-72BB-4A5D-993C-54D730BD1685}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FB84BC4E-D93B-4E22-AC9D-8EC420AFDAF3}
EndGlobalSection
EndGlobal
45 changes: 45 additions & 0 deletions ScreenCastR.App/ScreenCastManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ScreenCastApp
{
public class ScreenCastManager
{
private List<Viewer> viewers = new List<Viewer>();

public void AddViewer(string connectionId, string agentName)
{
viewers.Add(new Viewer(connectionId, agentName));
}

public void RemoveViewer(string connectionId)
{
viewers.Remove(viewers.First(i => i.ConnectionId == connectionId));
}

public void RemoveViewerByAgent(string agentName)
{
viewers.RemoveAll(i => i.AgentName == agentName);
}

public bool IsViewerExists(string agentName)
{
return viewers.Any(i => i.AgentName == agentName);
}

}

internal class Viewer
{
public string ConnectionId { get; set; }
public string AgentName { get; set; }

public Viewer(string connectionId, string agentName)
{
ConnectionId = connectionId;
AgentName = agentName;
}
}
}
11 changes: 11 additions & 0 deletions ScreenCastR.App/Shared/MainLayout.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@inherits LayoutComponentBase

<div class="sidebar">
<NavMenu />
</div>

<div class="main">
<div class="content px-4">
@Body
</div>
</div>
17 changes: 17 additions & 0 deletions ScreenCastR.App/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="top-row pl-4 navbar navbar-dark">
<a class="navbar-brand" href="">ScreenCastR </a>
<button class="navbar-toggler" @onclick="ToggleNavMenu">
<span class="navbar-toggler-icon"></span>
</button>
</div>

@code {
bool collapseNavMenu = true;

string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

void ToggleNavMenu()
{
collapseNavMenu = !collapseNavMenu;
}
}
55 changes: 55 additions & 0 deletions ScreenCastR.App/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace ScreenCastApp
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSignalR().AddHubOptions<ScreenCastHub>(options => { options.MaximumReceiveMessageSize = 102400000; });
services.AddSingleton<ScreenCastManager>();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
endpoints.MapHub<ScreenCastHub>("/ScreenCastHub");
});
}
}
}
7 changes: 7 additions & 0 deletions ScreenCastR.App/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.JSInterop
@using ScreenCastApp
@using ScreenCastApp.Shared
Loading

0 comments on commit 52314bf

Please sign in to comment.