Compare commits
17 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
100e782d9c | ||
|
f2ff558d2c | ||
|
4a0603b724 | ||
|
9d1c8568ce | ||
|
88bbbc5e91 | ||
|
ba120bfa24 | ||
|
ef49715655 | ||
|
c6f3bee4de | ||
|
c94d912f8b | ||
|
fc5098c012 | ||
|
baba3bd6c2 | ||
|
e9441d510e | ||
|
056d810909 | ||
|
515c2b0dea | ||
d30d2128c9 | |||
30c228a78f | |||
8315645af7 |
@ -1,4 +1,5 @@
|
|||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -6,33 +7,58 @@ namespace BinaryDad.Coding.Hubs
|
|||||||
{
|
{
|
||||||
public class CodeHub : Hub
|
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)
|
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)
|
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
|
// only send to all if it's a teacher
|
||||||
if (isTeacher)
|
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)
|
public async Task SaveName(string name, string color)
|
||||||
{
|
{
|
||||||
Users.Sessions[Context.ConnectionId].Id = Context.ConnectionId;
|
await _cache.SetStringAsync($"name-{Context.ConnectionId}", name);
|
||||||
Users.Sessions[Context.ConnectionId].Name = name;
|
await _cache.SetStringAsync($"color-{Context.ConnectionId}", string.IsNullOrWhiteSpace(color) ? "white" : color);
|
||||||
Users.Sessions[Context.ConnectionId].Color = 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()
|
public override async Task OnConnectedAsync()
|
||||||
@ -42,16 +68,19 @@ namespace BinaryDad.Coding.Hubs
|
|||||||
Name = string.Empty
|
Name = string.Empty
|
||||||
};
|
};
|
||||||
|
|
||||||
await Clients.All.SendAsync("UsersList", Users.Sessions);
|
await Clients.Others.SendAsync("UsersList", Users.Sessions);
|
||||||
|
|
||||||
await base.OnConnectedAsync();
|
await base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task OnDisconnectedAsync(Exception exception)
|
public override async Task OnDisconnectedAsync(Exception exception)
|
||||||
|
{
|
||||||
|
if (Users.Sessions.ContainsKey(Context.ConnectionId))
|
||||||
{
|
{
|
||||||
Users.Sessions.Remove(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);
|
await base.OnDisconnectedAsync(exception);
|
||||||
}
|
}
|
||||||
|
@ -8,16 +8,10 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"profiles": {
|
"profiles": {
|
||||||
"IIS Express": {
|
|
||||||
"commandName": "IISExpress",
|
|
||||||
"launchBrowser": true,
|
|
||||||
"environmentVariables": {
|
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"BinaryDad.Coding": {
|
"BinaryDad.Coding": {
|
||||||
"commandName": "Project",
|
"commandName": "Project",
|
||||||
"launchBrowser": true,
|
"launchBrowser": true,
|
||||||
|
"hotReloadEnabled": false,
|
||||||
"environmentVariables": {
|
"environmentVariables": {
|
||||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
},
|
},
|
||||||
|
@ -5,6 +5,9 @@ using Microsoft.AspNetCore.Http;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using StackExchange.Redis;
|
||||||
|
using System;
|
||||||
|
using System.Net;
|
||||||
|
|
||||||
namespace BinaryDad.Coding
|
namespace BinaryDad.Coding
|
||||||
{
|
{
|
||||||
@ -21,7 +24,37 @@ namespace BinaryDad.Coding
|
|||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
services.AddSignalR().AddStackExchangeRedis("redis-service:6379");
|
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 =>
|
services.AddHttpsRedirection(options =>
|
||||||
{
|
{
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
using System;
|
using Microsoft.Extensions.Caching.Distributed;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace BinaryDad.Coding
|
namespace BinaryDad.Coding
|
||||||
{
|
{
|
||||||
@ -7,4 +9,26 @@ namespace BinaryDad.Coding
|
|||||||
{
|
{
|
||||||
public static readonly IDictionary<string, User> Sessions = new Dictionary<string, User>(StringComparer.OrdinalIgnoreCase);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,8 @@
|
|||||||
"Microsoft": "Warning",
|
"Microsoft": "Warning",
|
||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"Redis": "cluster.home.lan:6379"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,5 +6,8 @@
|
|||||||
"Microsoft.Hosting.Lifetime": "Information"
|
"Microsoft.Hosting.Lifetime": "Information"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"AllowedHosts": "*"
|
"AllowedHosts": "*",
|
||||||
|
"ConnectionStrings": {
|
||||||
|
"Redis": "redis:6379"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9';
|
let isTeacher = window.location.hash === '#4ELQcUq7UnFGghAZCVr4Chr9JmtncigfkGu5WSf9';
|
||||||
|
|
||||||
let isLive = false;
|
let isLive = false;
|
||||||
|
let editorLock = false;
|
||||||
|
|
||||||
let isLiveMode = function () {
|
let isLiveMode = function () {
|
||||||
|
|
||||||
@ -73,9 +74,13 @@
|
|||||||
|
|
||||||
if (!isTeacher && isLive) {
|
if (!isTeacher && isLive) {
|
||||||
|
|
||||||
|
editorLock = true;
|
||||||
|
|
||||||
let editor = ace.edit($editors[index]);
|
let editor = ace.edit($editors[index]);
|
||||||
|
|
||||||
editor.session.setValue(code);
|
editor.session.setValue(code);
|
||||||
|
|
||||||
|
editorLock = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -100,7 +105,10 @@
|
|||||||
let editor = ace.edit(e);
|
let editor = ace.edit(e);
|
||||||
|
|
||||||
editor.session.on('change', function () {
|
editor.session.on('change', function () {
|
||||||
|
|
||||||
|
if (!editorLock) {
|
||||||
updateCode(editor, i);
|
updateCode(editor, i);
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// send initial code
|
// send initial code
|
||||||
|
Loading…
x
Reference in New Issue
Block a user