重构代码:增加WebSocket支持,未完成
This commit is contained in:
parent
7deaf9a46f
commit
0320b68d5d
|
@ -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);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[AllowAnonymous]
|
||||
public ActionResult Error()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -66,6 +66,7 @@ namespace Bootstrap.Admin
|
|||
app.UseStaticFiles();
|
||||
app.UseAuthentication();
|
||||
app.UseBootstrapRoleAuthorization();
|
||||
app.UseWebSocketHandler();
|
||||
app.UseCacheManagerCorsHandler();
|
||||
app.UseMvc(routes =>
|
||||
{
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
@{
|
||||
ViewBag.Title = "服务器内部错误";
|
||||
Layout = "~/Views/Shared/_Root.cshtml";
|
||||
}
|
||||
@section css {
|
||||
<link href="~/css/error.css" rel="stylesheet" />
|
||||
}
|
||||
@section Javascript {
|
||||
<script src="~/js/error.js"></script>
|
||||
}
|
||||
<section class="error-wrapper">
|
||||
<img src="~/images/error_icon.png" />
|
||||
<h1>服务器内部错误</h1>
|
||||
<h3>相关错误信息已经记录到日志中,请登录服务器查看</h3>
|
||||
<br />
|
||||
<a href="~/Home/Index" target="_top">返回首页</a>
|
||||
</section>
|
|
@ -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
|
||||
{
|
||||
/// <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>());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>测试WebSocket</title>
|
||||
<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 output;
|
||||
var text = "test echo";
|
||||
|
||||
function write(s) {
|
||||
var p = document.createElement("p");
|
||||
p.innerHTML = s;
|
||||
output.appendChild(p);
|
||||
}
|
||||
|
||||
function doConnect() {
|
||||
socket = new WebSocket(uri);
|
||||
socket.onopen = function (e) { write("opened " + uri); doSend(); };
|
||||
socket.onclose = function (e) { write("closed"); };
|
||||
socket.onmessage = function (e) {
|
||||
write("Received: " + e.data); //socket.close();
|
||||
};
|
||||
socket.onerror = function (e) { write("Error: " + e.data); };
|
||||
}
|
||||
|
||||
function doSend() {
|
||||
write("Sending: " + text);
|
||||
//socket.send(text);
|
||||
}
|
||||
|
||||
function onInit() {
|
||||
output = document.getElementById("output");
|
||||
doConnect();
|
||||
}
|
||||
|
||||
window.onload = onInit;
|
||||
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="output">
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -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
|
|||
/// <returns></returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format("{0};{1}-{2}", Category, Message);
|
||||
return string.Format("{0}-{1}", Category, Message);
|
||||
}
|
||||
private void Dispose(bool disposing)
|
||||
{
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace Bootstrap.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public class Notifications
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Category { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Title { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Content { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public DateTime RegisterTime { get; set; }
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public string Status { get; set; }
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bootstrap.DataAccess
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static class NotificationsHelper
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Notifications> RetrieveNotifications()
|
||||
{
|
||||
var ret = new List<Notifications>();
|
||||
ret.Add(new Notifications() { Title = "测试消息1", Content = "用户注册", RegisterTime = DateTime.Now });
|
||||
ret.Add(new Notifications() { Title = "测试消息2", Content = "用户注册", RegisterTime = DateTime.Now });
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue