diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Controllers/TranslateController.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Controllers/TranslateController.cs index 85b2c89b589f98018e34063a60df72f491010976..424fcfdea866bd6e1d3949f76fdfc7b8f46717a8 100644 --- a/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Controllers/TranslateController.cs +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Controllers/TranslateController.cs @@ -6,6 +6,7 @@ using Aurora.Core.IService; using Aurora.Core.IService.Dto; using Aurora.Infrastructure.Response; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; namespace Aurora.Core.Api.Controllers @@ -16,12 +17,15 @@ namespace Aurora.Core.Api.Controllers { private readonly ILogger _logger; private readonly IRocketService _rocketService; + private readonly IHubContext _hubContext; public TranslateController(ILogger logger, - IRocketService rocketService) + IRocketService rocketService, + IHubContext hubContext) { _logger = logger; _rocketService = rocketService; + _hubContext = hubContext; } [HttpGet] @@ -42,8 +46,21 @@ namespace Aurora.Core.Api.Controllers /// public async Task> Rocket(RocketDto model) { - var res = await _rocketService.Add(model); + var res = await _rocketService.Rocket(model); + + await _hubContext.Clients.All.SendAsync("rocketWord", model); return res; } + + [HttpGet] + /// + /// biu word + /// + /// + public async Task> Test() + { + await _hubContext.Clients.All.SendAsync("rocketWord", "model"); + return new ResponseModel(new RocketDto()); + } } } diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/ModuleCoreApi.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/ModuleCoreApi.cs index 261cb498830b10442911925071338ea8ad96b38d..e93baa50b7fe0d2beb7db110f433927a6a65a0cb 100644 --- a/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/ModuleCoreApi.cs +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/ModuleCoreApi.cs @@ -28,6 +28,7 @@ namespace Aurora.Core.Api option.UseMySQL(configuration["ReadonlyConnectionStrings:MySql"])); services.AddTransient(); + services.AddTransient(); return services; } diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Startup.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Startup.cs index 6cc495eef968183ab1096f0a1d78b522330e093c..06b19266d88661ac22e9cbd4714eac2b763477e0 100644 --- a/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Startup.cs +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/Startup.cs @@ -41,6 +41,7 @@ namespace Aurora.Core.Api services.AddCommonService(); services.AddModuleCoreApi(Configuration); + services.AddSignalR(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. @@ -65,15 +66,23 @@ namespace Aurora.Core.Api endpoints.MapControllers(); }); // register this service - ServiceEntity serviceEntity = new ServiceEntity + // ServiceEntity serviceEntity = new ServiceEntity + // { + // // IP = NetworkHelper.LocalIPAddress, + // Port = Convert.ToInt32(Configuration["Service:Port"]), + // ServiceName = Configuration["Service:Name"], + // ConsulIP = Configuration["Consul:IP"], + // ConsulPort = Convert.ToInt32(Configuration["Consul:Port"]) + // }; + + // app.RegisterConsul(lifetime, serviceEntity); + + app.UseEndpoints(endpoints => { - // IP = NetworkHelper.LocalIPAddress, - Port = Convert.ToInt32(Configuration["Service:Port"]), - ServiceName = Configuration["Service:Name"], - ConsulIP = Configuration["Consul:IP"], - ConsulPort = Convert.ToInt32(Configuration["Consul:Port"]) - }; - app.RegisterConsul(lifetime, serviceEntity); + endpoints.MapControllers(); + endpoints.MapHub("/hub/TranslateRocketHub"); + }); + } } } diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/TranslateRocketHub.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/TranslateRocketHub.cs new file mode 100644 index 0000000000000000000000000000000000000000..24c04b9b4961699bb9300f34a21f513c80b4b54f --- /dev/null +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.Api/TranslateRocketHub.cs @@ -0,0 +1,37 @@ +using System; +using System.Threading.Tasks; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Extensions.Logging; + +namespace Aurora.Core.Api +{ + public class TranslateRocketHub : Hub + { + private readonly ILogger _logger; + + public TranslateRocketHub(ILogger logger) + { + _logger = logger; + } + /// + /// 客户端连接 + /// + /// + public override async Task OnConnectedAsync() + { + await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users"); + await base.OnConnectedAsync(); + } + public override async Task OnDisconnectedAsync(Exception exception) + { + await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users"); + await base.OnDisconnectedAsync(exception); + } + + public async Task SendMessage(string word) + { + await Clients.All.SendAsync("rocketWord", word); + } + + } +} \ No newline at end of file diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.Domain/Entities/Rocket.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.Domain/Entities/Rocket.cs index c980bf761f405d70bd4e4acf202182923b18d846..422801ea27ac0aceb5768cacb9dc3b3f66f395fc 100644 --- a/Aurora.Microservices/Aurora.Core/Aurora.Core.Domain/Entities/Rocket.cs +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.Domain/Entities/Rocket.cs @@ -4,9 +4,9 @@ using System.Collections.Generic; namespace Aurora.Core.Domain.Entities { /// - /// 单词,我就用 Rocket 老表示单词 + /// 单词,我就用 Rocket 表示单词 /// - public class Rocket : TenantEntityBase + public class RocketWord : TenantEntityBase { public int CategoryId { get; set; } public string Name { get; set; } diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.EntityFramework/ApplicationReadonlyDbContext.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.EntityFramework/ApplicationReadonlyDbContext.cs index 5f6fa78eba8c77695d76856a44e11115ceef4642..2736575705ef42a5607e895c2420c27a31c77db6 100644 --- a/Aurora.Microservices/Aurora.Core/Aurora.Core.EntityFramework/ApplicationReadonlyDbContext.cs +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.EntityFramework/ApplicationReadonlyDbContext.cs @@ -34,6 +34,7 @@ namespace Aurora.Core.EntityFramework } public DbSet Categories { get; set; } + public DbSet RocketWords { get; set; } public DbSet Subjects { get; set; } public DbSet Exampapers { get; set; } public DbSet Question { get; set; } diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.IService/IRocketService.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.IService/IRocketService.cs index a0e72f2dec42a401ab4985fcbcd674db10a8477b..032a70c69c008c81082258cad8b8420282a210e0 100644 --- a/Aurora.Microservices/Aurora.Core/Aurora.Core.IService/IRocketService.cs +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.IService/IRocketService.cs @@ -9,6 +9,6 @@ namespace Aurora.Core.IService public interface IRocketService { Task>> GetList(); - Task> Rocket(RocketDto model); + Task> Rocket(RocketDto model); } } diff --git a/Aurora.Microservices/Aurora.Core/Aurora.Core.Service/RocketService.cs b/Aurora.Microservices/Aurora.Core/Aurora.Core.Service/RocketService.cs index fd632c105ccc82a7d6ced885cbb8cbf21542de92..e7a3eafdeba1da471b02952f824cb948d5fe6ee0 100644 --- a/Aurora.Microservices/Aurora.Core/Aurora.Core.Service/RocketService.cs +++ b/Aurora.Microservices/Aurora.Core/Aurora.Core.Service/RocketService.cs @@ -50,12 +50,23 @@ namespace Aurora.Core.Service public async Task> Rocket(RocketDto model) { + #region 保存到数据库 + var entity = _mapper.Map(model); - _dbContext.Categories.Add(entity); + _dbContext.RocketWords.Add(entity); var num = await _dbContext.SaveChangesAsync(); + #endregion + + #region 发送到移动端 + + // 按照用户发送 + // 广播发送 + + #endregion + var dto = _mapper.Map(entity); var res = new ResponseModel(dto); diff --git a/Samples/chrome-plugins/translation-rocket/js/background.js b/Samples/chrome-plugins/translation-rocket/js/background.js index f135ec44a7f15e787e60c81cb82d4e6c848358ab..2921fde82ab0c5c548a96c466aab35ec7fa8e471 100644 --- a/Samples/chrome-plugins/translation-rocket/js/background.js +++ b/Samples/chrome-plugins/translation-rocket/js/background.js @@ -26,15 +26,14 @@ function openUrlCurrentTab(url) { getCurrentTabId((tabId) => { chrome.tabs.update(tabId, { url: url }) }) -} +} function handleStateChange() {} chrome.contextMenus.create({ title: '发射选择的单词:%s', // %s表示选中的文字 contexts: ['selection'], // 只有当选中文字时才会出现此右键菜单 - onclick: function (params) { - + onclick: function (params) { chrome.browserAction.setBadgeText({ text: 'new' }) chrome.browserAction.setBadgeBackgroundColor({ color: [255, 0, 0, 255] }) @@ -43,10 +42,20 @@ chrome.contextMenus.create({ xhr.open( 'GET', // chrome.extension.getURL('/config_resources/config.json'), - 'http://www.luoboit.cn/api/blog/post/query?Page=1&Limit=10', + // 'http://www.luoboit.cn/api/blog/post/query?Page=1&Limit=10', + 'http://localhost:8000/Translate/Test', true, ) + // prepare form data + // let data = new FormData(form) + let data = new FormData() + // set headers + // xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') + // xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest') + + // send request + // xhr.send(data) //访问内部位于config_resources目录下的config.json文件 - xhr.send() + xhr.send() }, })