Compare commits

..

2 Commits

Author SHA1 Message Date
e582a9c0cd add docker.sh 2024-01-17 19:53:52 +00:00
Ryan Peters
5d76c86ff3 Revert "testing use of redis backend"
This reverts commit 03968bb11f.
2024-01-17 14:36:17 -05:00
9 changed files with 26 additions and 122 deletions

View File

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

View File

@ -8,10 +8,16 @@
}
},
"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,9 +5,6 @@ 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
{
@ -24,37 +21,7 @@ namespace BinaryDad.Coding
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
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.AddSignalR();
services.AddHttpsRedirection(options =>
{

View File

@ -1,7 +1,5 @@
using Microsoft.Extensions.Caching.Distributed;
using System;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace BinaryDad.Coding
{
@ -9,26 +7,4 @@ 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,8 +6,5 @@
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"Redis": "cluster.home.lan:6379"
}
}

View File

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

View File

@ -14,7 +14,6 @@
let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9';
let isLive = false;
let editorLock = false;
let isLiveMode = function () {
@ -74,13 +73,9 @@
if (!isTeacher && isLive) {
editorLock = true;
let editor = ace.edit($editors[index]);
editor.session.setValue(code);
editorLock = false;
}
});
@ -105,10 +100,7 @@
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:redis
sudo docker push binarydad/coding:redis
sudo docker build -f BinaryDad.Coding/Dockerfile . -t binarydad/coding:latest
sudo docker push binarydad/coding:latest