Compare commits

..

17 Commits

Author SHA1 Message Date
Ryan Peters
100e782d9c use connection strings for redis 2024-01-23 11:23:05 -05:00
Ryan Peters
f2ff558d2c attempts to share user info 2024-01-17 21:49:23 -05:00
Ryan Peters
4a0603b724 more lock fixes 2024-01-17 20:43:59 -05:00
Ryan Peters
9d1c8568ce fine tuning 2024-01-17 20:40:27 -05:00
Ryan Peters
88bbbc5e91 logging 2024-01-17 20:21:35 -05:00
Ryan Peters
ba120bfa24 dfgdf 2024-01-17 20:16:31 -05:00
Ryan Peters
ef49715655 logging 2024-01-17 20:07:30 -05:00
Ryan Peters
c6f3bee4de more.... 2024-01-17 20:02:43 -05:00
Ryan Peters
c94d912f8b tweaks!! 2024-01-17 19:49:18 -05:00
Ryan Peters
fc5098c012 more tweaks... 2024-01-17 19:47:11 -05:00
Ryan Peters
baba3bd6c2 additional redis tweaks 2024-01-17 19:41:19 -05:00
Ryan Peters
e9441d510e null checks on code hub 2024-01-17 19:27:07 -05:00
Ryan Peters
056d810909 Merge branch 'dev/redis' of https://git.binarydad.com/ryan/Coding into dev/redis 2024-01-17 19:05:58 -05:00
Ryan Peters
515c2b0dea testing changes 2024-01-17 19:05:55 -05:00
d30d2128c9 fix docker push tag 2024-01-17 20:04:36 +00:00
30c228a78f update docker tag for redis 2024-01-17 20:00:09 +00:00
8315645af7 add docker.sh 2024-01-17 14:55:03 -05:00
9 changed files with 122 additions and 26 deletions

View File

@ -6,7 +6,9 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="8.0.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
<PackageReference Include="StackExchange.Redis" Version="2.7.17" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Threading.Tasks;
@ -6,33 +7,58 @@ namespace BinaryDad.Coding.Hubs
{
public class CodeHub : Hub
{
public Task UpdateCode(string user, int index, string code, bool isTeacher)
private readonly IDistributedCache _cache;
public CodeHub(IDistributedCache cache)
{
_cache = cache;
}
public async Task UpdateCode(string user, int index, string code, bool isTeacher)
{
Console.WriteLine($"Received code from {user} with index {index}");
if (index == 0)
{
Users.Sessions[Context.ConnectionId].Html = code;
await _cache.SetStringAsync($"html-{Context.ConnectionId}", code);
//if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value))
//{
// value.Html = code;
//}
}
else if (index == 1)
{
Users.Sessions[Context.ConnectionId].Css = code;
await _cache.SetStringAsync($"css-{Context.ConnectionId}", code);
//if (Users.Sessions.TryGetValue(Context.ConnectionId, out User value))
//{
// value.Css = code;
//}
}
// only send to all if it's a teacher
if (isTeacher)
{
return Clients.All.SendAsync("ReceiveCode", user, index, code);
}
Console.WriteLine($"Sending code from {user}");
return null;
await Clients.Others.SendAsync("ReceiveCode", user, index, code);
}
}
public async Task SaveName(string name, string color)
{
Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId;
Users.Sessions[Context.ConnectionId].Name = name;
Users.Sessions[Context.ConnectionId].Color = string.IsNullOrWhiteSpace(color) ? "white" : color;
await _cache.SetStringAsync($"name-{Context.ConnectionId}", name);
await _cache.SetStringAsync($"color-{Context.ConnectionId}", string.IsNullOrWhiteSpace(color) ? "white" : color);
await Clients.All.SendAsync("UsersList", Users.Sessions);
//if (!Users.Sessions.ContainsKey(Context.ConnectionId))
//{
// Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId;
// Users.Sessions[Context.ConnectionId].Name = name;
// Users.Sessions[Context.ConnectionId].Color = string.IsNullOrWhiteSpace(color) ? "white" : color;
//}
await Clients.Others.SendAsync("UsersList", Users.Sessions);
}
public override async Task OnConnectedAsync()
@ -42,16 +68,19 @@ namespace BinaryDad.Coding.Hubs
Name = string.Empty
};
await Clients.All.SendAsync("UsersList", Users.Sessions);
await Clients.Others.SendAsync("UsersList", Users.Sessions);
await base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
if (Users.Sessions.ContainsKey(Context.ConnectionId))
{
Users.Sessions.Remove(Context.ConnectionId);
}
await Clients.All.SendAsync("UsersList", Users.Sessions);
await Clients.Others.SendAsync("UsersList", Users.Sessions);
await base.OnDisconnectedAsync(exception);
}

View File

@ -8,16 +8,10 @@
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"BinaryDad.Coding": {
"commandName": "Project",
"launchBrowser": true,
"hotReloadEnabled": false,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},

View File

@ -5,6 +5,9 @@ using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StackExchange.Redis;
using System;
using System.Net;
namespace BinaryDad.Coding
{
@ -21,7 +24,37 @@ namespace BinaryDad.Coding
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSignalR();
services.AddSignalR().AddStackExchangeRedis(options =>
{
options.ConnectionFactory = async writer =>
{
var config = new ConfigurationOptions
{
AbortOnConnectFail = false
};
config.EndPoints.Add(Configuration.GetConnectionString("Redis"));
config.SetDefaultPorts();
var connection = await ConnectionMultiplexer.ConnectAsync(config, writer);
connection.ConnectionFailed += (_, e) =>
{
Console.WriteLine("Connection to Redis failed.");
Console.WriteLine(e.ToString());
};
if (!connection.IsConnected)
{
Console.WriteLine("Did not connect to Redis.");
}
else
{
Console.WriteLine("Connected to Redis.");
}
return connection;
};
});
services.AddHttpsRedirection(options =>
{

View File

@ -1,5 +1,7 @@
using System;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BinaryDad.Coding
{
@ -7,4 +9,26 @@ namespace BinaryDad.Coding
{
public static readonly IDictionary<string, User> Sessions = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase);
}
public class UserCache
{
private readonly IDistributedCache _cache;
public UserCache(IDistributedCache cache)
{
_cache = cache;
}
public async Task<User> GetUser(string connectionId)
{
var user = new User();
user.Id = connectionId;
user.Name = await _cache.GetStringAsync($"name-{connectionId}");
user.Color = await _cache.GetStringAsync($"color-{connectionId}");
return user;
}
}
}

View File

@ -6,5 +6,8 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"Redis": "cluster.home.lan:6379"
}
}

View File

@ -6,5 +6,8 @@
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ConnectionStrings": {
"Redis": "redis:6379"
}
}

View File

@ -14,6 +14,7 @@
let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9';
let isLive = false;
let editorLock = false;
let isLiveMode = function () {
@ -73,9 +74,13 @@
if (!isTeacher && isLive) {
editorLock = true;
let editor = ace.edit($editors[index]);
editor.session.setValue(code);
editorLock = false;
}
});
@ -100,7 +105,10 @@
let editor = ace.edit(e);
editor.session.on('change', function () {
if (!editorLock) {
updateCode(editor, i);
};
});
// send initial code

View File

@ -1,2 +1,2 @@
sudo docker build -f BinaryDad.Coding/Dockerfile . -t binarydad/coding:latest
sudo docker push binarydad/coding:latest
sudo docker build -f BinaryDad.Coding/Dockerfile . -t binarydad/coding:redis
sudo docker push binarydad/coding:redis