From 0320b68d5d410cc1f1b3ca722f4b83f13b2fa8dd Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Mon, 11 Jun 2018 12:48:11 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0WebSocket=E6=94=AF=E6=8C=81=EF=BC=8C=E6=9C=AA?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Controllers/HomeController.cs | 10 ++++ Bootstrap.Admin/Startup.cs | 1 + Bootstrap.Admin/Views/Home/Error.cshtml | 17 ++++++ Bootstrap.Admin/WebSocketHandlerMiddleware.cs | 54 +++++++++++++++++++ Bootstrap.Admin/wwwroot/html/Test.html | 48 +++++++++++++++++ Bootstrap.DataAccess/NotificationHelper.cs | 3 +- Bootstrap.DataAccess/Notifications.cs | 31 ----------- Bootstrap.DataAccess/NotificationsHelper.cs | 23 -------- 8 files changed, 131 insertions(+), 56 deletions(-) create mode 100644 Bootstrap.Admin/Views/Home/Error.cshtml create mode 100644 Bootstrap.Admin/WebSocketHandlerMiddleware.cs create mode 100644 Bootstrap.Admin/wwwroot/html/Test.html delete mode 100644 Bootstrap.DataAccess/Notifications.cs delete mode 100644 Bootstrap.DataAccess/NotificationsHelper.cs diff --git a/Bootstrap.Admin/Controllers/HomeController.cs b/Bootstrap.Admin/Controllers/HomeController.cs index 4e9ef534..e1981c1a 100644 --- a/Bootstrap.Admin/Controllers/HomeController.cs +++ b/Bootstrap.Admin/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using Bootstrap.Admin.Models; using Bootstrap.DataAccess; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace Bootstrap.Admin.Controllers @@ -18,5 +19,14 @@ namespace Bootstrap.Admin.Controllers var v = new HeaderBarModel(User.Identity) { HomeUrl = DictHelper.RetrieveHomeUrl() }; return v.HomeUrl.StartsWith("~/") ? (ActionResult)View(v) : Redirect(v.HomeUrl); } + /// + /// + /// + /// + [AllowAnonymous] + public ActionResult Error() + { + return View(); + } } } \ No newline at end of file diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index 45f2ce96..a5576524 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -66,6 +66,7 @@ namespace Bootstrap.Admin app.UseStaticFiles(); app.UseAuthentication(); app.UseBootstrapRoleAuthorization(); + app.UseWebSocketHandler(); app.UseCacheManagerCorsHandler(); app.UseMvc(routes => { diff --git a/Bootstrap.Admin/Views/Home/Error.cshtml b/Bootstrap.Admin/Views/Home/Error.cshtml new file mode 100644 index 00000000..550f13af --- /dev/null +++ b/Bootstrap.Admin/Views/Home/Error.cshtml @@ -0,0 +1,17 @@ +@{ + ViewBag.Title = "服务器内部错误"; + Layout = "~/Views/Shared/_Root.cshtml"; +} +@section css { + +} +@section Javascript { + +} +
+ +

服务器内部错误

+

相关错误信息已经记录到日志中,请登录服务器查看

+
+ 返回首页 +
diff --git a/Bootstrap.Admin/WebSocketHandlerMiddleware.cs b/Bootstrap.Admin/WebSocketHandlerMiddleware.cs new file mode 100644 index 00000000..22f749c7 --- /dev/null +++ b/Bootstrap.Admin/WebSocketHandlerMiddleware.cs @@ -0,0 +1,54 @@ +using Longbow.Configuration.Middleware; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using System; +using System.Net.WebSockets; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace Bootstrap.Admin +{ + public class WebSocketHandlerMiddleware : LgbMiddleware + { + /// + /// + /// + /// + public WebSocketHandlerMiddleware(RequestDelegate next) : base(next) + { + + } + /// + /// + /// + /// + /// + public override async Task Invoke(HttpContext context) + { + if (!context.WebSockets.IsWebSocketRequest || !context.User.Identity.IsAuthenticated) return; + using (var socket = await context.WebSockets.AcceptWebSocketAsync()) + { + while (socket.State == WebSocketState.Open) + { + await Task.Delay(60000); + var data = new ArraySegment(Encoding.UTF8.GetBytes(DateTimeOffset.Now.ToString())); + await socket.SendAsync(data, WebSocketMessageType.Text, true, CancellationToken.None); + } + } + } + } + + public static class WebSocketExtensions + { + /// + /// + /// + /// + public static void UseWebSocketHandler(this IApplicationBuilder app) + { + app.UseWebSockets(); + app.Map("/Foo", builder => builder.UseMiddleware()); + } + } +} \ No newline at end of file diff --git a/Bootstrap.Admin/wwwroot/html/Test.html b/Bootstrap.Admin/wwwroot/html/Test.html new file mode 100644 index 00000000..f4464450 --- /dev/null +++ b/Bootstrap.Admin/wwwroot/html/Test.html @@ -0,0 +1,48 @@ + + + + + 测试WebSocket + + + + +
+ +
+ + \ No newline at end of file diff --git a/Bootstrap.DataAccess/NotificationHelper.cs b/Bootstrap.DataAccess/NotificationHelper.cs index 90a4ef60..2996f339 100644 --- a/Bootstrap.DataAccess/NotificationHelper.cs +++ b/Bootstrap.DataAccess/NotificationHelper.cs @@ -7,7 +7,6 @@ using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; -using System.Net.WebSockets; using System.Threading; namespace Bootstrap.DataAccess @@ -154,7 +153,7 @@ namespace Bootstrap.DataAccess /// public override string ToString() { - return string.Format("{0};{1}-{2}", Category, Message); + return string.Format("{0}-{1}", Category, Message); } private void Dispose(bool disposing) { diff --git a/Bootstrap.DataAccess/Notifications.cs b/Bootstrap.DataAccess/Notifications.cs deleted file mode 100644 index c1667989..00000000 --- a/Bootstrap.DataAccess/Notifications.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; - -namespace Bootstrap.DataAccess -{ - /// - /// - /// - public class Notifications - { - /// - /// - /// - public string Category { get; set; } - /// - /// - /// - public string Title { get; set; } - /// - /// - /// - public string Content { get; set; } - /// - /// - /// - public DateTime RegisterTime { get; set; } - /// - /// - /// - public string Status { get; set; } - } -} diff --git a/Bootstrap.DataAccess/NotificationsHelper.cs b/Bootstrap.DataAccess/NotificationsHelper.cs deleted file mode 100644 index 2ea768e0..00000000 --- a/Bootstrap.DataAccess/NotificationsHelper.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Bootstrap.DataAccess -{ - /// - /// - /// - public static class NotificationsHelper - { - /// - /// - /// - /// - public static IEnumerable RetrieveNotifications() - { - var ret = new List(); - ret.Add(new Notifications() { Title = "测试消息1", Content = "用户注册", RegisterTime = DateTime.Now }); - ret.Add(new Notifications() { Title = "测试消息2", Content = "用户注册", RegisterTime = DateTime.Now }); - return ret; - } - } -}