增加功能:增加WebSocket中间件,准备替换后台管理系统对客户端消息推送机制

This commit is contained in:
Argo-MacBookPro 2018-06-11 18:37:21 +08:00
parent 4f9894d5bb
commit 8e89d67e0c
7 changed files with 11 additions and 83 deletions

View File

@ -9,7 +9,6 @@
<PackageReference Include="Bootstrap.Security.Mvc" Version="1.0.0" />
<PackageReference Include="Longbow.Web" Version="1.0.1" />
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.0.3" />
</ItemGroup>
<ItemGroup>

View File

@ -1,25 +0,0 @@
using Bootstrap.DataAccess;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// </summary>
[Route("api/[controller]")]
public class WSController : Controller
{
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public List<MessageBody> Get()
{
return NotificationHelper.MessagePool.ToList();
}
}
}

View File

@ -5,12 +5,14 @@ using Longbow.Cache.Middleware;
using Longbow.Configuration;
using Longbow.Data;
using Longbow.Logging;
using Longbow.Web.WebSockets;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.IO;
@ -46,6 +48,7 @@ namespace Bootstrap.Admin
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
JsonConvert.DefaultSettings = () => options.SerializerSettings;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
}

View File

@ -1,11 +1,16 @@
@{
ViewBag.Title = "注册新用户";
Layout = "~/Views/Shared/_Layout.cshtml";
Layout = "~/Views/Shared/_Root.cshtml";
}
@section css {
<link href="~/css/bootstrap.css" rel="stylesheet">
<link href="~/css/bootstrap-theme.css" rel="stylesheet">
<link href="~/css/font-awesome.css" rel="stylesheet" />
<link href="~/css/register.css" rel="stylesheet" />
}
@section javascript {
<script src="~/js/bootstrap.js"></script>
<script src="~/js/Longbow.Common.js"></script>
<script src="~/js/jquery.validate.js"></script>
<script src="~/js/messages_zh.js"></script>
<script src="~/js/sweetalert.js"></script>

View File

@ -1,54 +0,0 @@
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
{
/// <summary>
///
/// </summary>
/// <param name="next"></param>
public WebSocketHandlerMiddleware(RequestDelegate next) : base(next)
{
}
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
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<byte>(Encoding.UTF8.GetBytes(DateTimeOffset.Now.ToString()));
await socket.SendAsync(data, WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}
}
public static class WebSocketExtensions
{
/// <summary>
///
/// </summary>
/// <param name="app"></param>
public static void UseWebSocketHandler(this IApplicationBuilder app)
{
app.UseWebSockets();
app.Map("/Foo", builder => builder.UseMiddleware<WebSocketHandlerMiddleware>());
}
}
}

View File

@ -6,7 +6,7 @@
<script src="../js/jquery-3.1.1.js"></script>
<script language="javascript" type="text/javascript">
var socket;
var uri = "ws://" + window.location.host + "/Foo";
var uri = "ws://" + window.location.host + "/WS";
var output;
var text = "test echo";

View File

@ -23,7 +23,7 @@ namespace Bootstrap.DataAccess
/// <summary>
///
/// </summary>
public static readonly ConcurrentBag<MessageBody> MessagePool = new ConcurrentBag<MessageBody>();
public static ConcurrentBag<MessageBody> MessagePool { get; } = new ConcurrentBag<MessageBody>();
/// <summary>
/// 新用户注册的通知的面板显示
/// </summary>