最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

Asp.net Core中如何使用中間件來(lái)管理websocket

來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:34:54
文檔

Asp.net Core中如何使用中間件來(lái)管理websocket

Asp.net Core中如何使用中間件來(lái)管理websocket:介紹 我喜歡.NET CORE 這個(gè)東西,其實(shí)不僅僅源于它性能很高,可以跨平臺(tái),還因?yàn)樗脑O(shè)計(jì)模式確實(shí)令人著迷。以前沒(méi).NET CORE 的時(shí)候,.NET用websocket必須跑在windows server 2012上,但我一般不會(huì)這么干,都把websocket架在nodejs的服務(wù)器上
推薦度:
導(dǎo)讀Asp.net Core中如何使用中間件來(lái)管理websocket:介紹 我喜歡.NET CORE 這個(gè)東西,其實(shí)不僅僅源于它性能很高,可以跨平臺(tái),還因?yàn)樗脑O(shè)計(jì)模式確實(shí)令人著迷。以前沒(méi).NET CORE 的時(shí)候,.NET用websocket必須跑在windows server 2012上,但我一般不會(huì)這么干,都把websocket架在nodejs的服務(wù)器上

介紹

我喜歡.NET CORE 這個(gè)東西,其實(shí)不僅僅源于它性能很高,可以跨平臺(tái),還因?yàn)樗脑O(shè)計(jì)模式確實(shí)令人著迷。以前沒(méi).NET CORE 的時(shí)候,.NET用websocket必須跑在windows server 2012上,但我一般不會(huì)這么干,都把websocket架在nodejs的服務(wù)器上。這么分出來(lái),麻煩肯定是麻煩的,而且js這東西,寫(xiě)復(fù)雜和幾年后再看都是頭疼的問(wèn)題。那么,如果.NET CORE是以kestrel運(yùn)行的,那么就不再需要考慮服務(wù)器的版本運(yùn)行,任何一個(gè)地方都可以用websocket

ASP.NET Core SignalR是一個(gè)有用的庫(kù),可以簡(jiǎn)化Web應(yīng)用程序中實(shí)時(shí)通信的管理。但是,我寧愿使用WebSockets,因?yàn)槲蚁胍`活,并且與任何WebSocket客戶端兼容。

在Microsoft的文檔中,我找到了一個(gè)很好的WebSockets工作示例。它仍然是管理連接,以便能夠從一個(gè)連接向其他連接廣播消息,這是SignalR開(kāi)箱即用的功能。期望這個(gè)邏輯非常復(fù)雜,我想從Startup類(lèi)中刪除它。

背景

要閱讀ASP.NET Core中的WebSockets支持,可以在此處查看。如果您想了解中間件以及如何在ASP.NET Core中編寫(xiě)它,請(qǐng)閱讀此鏈接。

代碼使用

首先,你必須添加 Microsoft.AspNetCore.WebSockets 包到你的項(xiàng)目。

現(xiàn)在,您可以創(chuàng)建一個(gè)擴(kuò)展方法和類(lèi)來(lái)管理WebSockets:

public static class WebSocketExtensions
{
 public static IApplicationBuilder UseCustomWebSocketManager(this IApplicationBuilder app)
 {
 return app.UseMiddleware<CustomWebSocketManager>();
 }
}

public class CustomWebSocketManager
{
 private readonly RequestDelegate _next;

 public CustomWebSocketManager(RequestDelegate next)
 {
 _next = next;
 }

 public async Task Invoke(HttpContext context, ICustomWebSocketFactory wsFactory, ICustomWebSocketMessageHandler wsmHandler)
 {
 if (context.Request.Path == "/ws")
 {
 if (context.WebSockets.IsWebSocketRequest)
 {
 string username = context.Request.Query["u"];
 if (!string.IsNullOrEmpty(username))
 {
 WebSocket webSocket = await context.WebSockets.AcceptWebSocketAsync();
 CustomWebSocket userWebSocket = new CustomWebSocket()
 {
 WebSocket = webSocket,
 Username = username
 };
 wsFactory.Add(userWebSocket);
 await wsmHandler.SendInitialMessages(userWebSocket);
 await Listen(context, userWebSocket, wsFactory, wsmHandler);
 }
 }
 else
 {
 context.Response.StatusCode = 400;
 }
 }
 await _next(context);
 }

 private async Task Listen(HttpContext context, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory, ICustomWebSocketMessageHandler wsmHandler)
 {
 WebSocket webSocket = userWebSocket.WebSocket;
 var buffer = new byte[1024 * 4];
 WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
 while (!result.CloseStatus.HasValue)
 {
 await wsmHandler.HandleMessage(result, buffer, userWebSocket, wsFactory);
 buffer = new byte[1024 * 4];
 result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
 } 
 wsFactory.Remove(userWebSocket.Username);
 await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
 }
}

在這種情況下,WebSockets請(qǐng)求在URL中始終包含“/ ws”。查詢字符串包含用于將WebSocket與登錄用戶相關(guān)聯(lián)的用戶名的參數(shù)u。

CustomWebSocket是一個(gè)包含WebSocket和用戶名的類(lèi):

public class CustomWebSocket
{
 public WebSocket WebSocket { get; set; }
 public string Username { get; set; }
}

我也創(chuàng)建了自定義WebSocket消息:

class CustomWebSocketMessage
{
 public string Text { get; set; }
 public DateTime MessagDateTime { get; set; }
 public string Username { get; set; }
 public WSMessageType Type { get; set; }
}

其中Type是您可能擁有的不同類(lèi)型消息的枚舉。

在Startup類(lèi)中,您必須注冊(cè)以下服務(wù):

services.AddSingleton<ICustomWebSocketFactory, CustomWebSocketFactory>();
services.AddSingleton<ICustomWebSocketMessageHandler, CustomWebSocketMessageHandler>();

CustomWebSocketFactory負(fù)責(zé)收集連接的WebSockets列表:

public interface ICustomWebSocketFactory
{
 void Add(CustomWebSocket uws);
 void Remove(string username);
 List<CustomWebSocket> All();
 List<CustomWebSocket> Others(CustomWebSocket client);
 CustomWebSocket Client(string username);
}

public class CustomWebSocketFactory : ICustomWebSocketFactory
{
 List<CustomWebSocket> List;

 public CustomWebSocketFactory()
 {
 List = new List<CustomWebSocket>();
 }

 public void Add(CustomWebSocket uws)
 {
 List.Add(uws);
 }

 //when disconnect
 public void Remove(string username) 
 {
 List.Remove(Client(username));
 }

 public List<CustomWebSocket> All()
 {
 return List;
 }
 
 public List<CustomWebSocket> Others(CustomWebSocket client)
 {
 return List.Where(c => c.Username != client.Username).ToList();
 }
 
 public CustomWebSocket Client(string username)
 {
 return List.First(c=>c.Username == username);
 }
}

CustomWebSocketMessageHandler包含有關(guān)消息的邏輯(即在連接時(shí)需要發(fā)送任何消息以及如何對(duì)傳入消息作出反應(yīng))

public interface ICustomWebSocketMessageHandler
{
 Task SendInitialMessages(CustomWebSocket userWebSocket);
 Task HandleMessage(WebSocketReceiveResult result, byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory);
 Task BroadcastOthers(byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory);
 Task BroadcastAll(byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory);
}

public class CustomWebSocketMessageHandler : ICustomWebSocketMessageHandler
{
 public async Task SendInitialMessages(CustomWebSocket userWebSocket)
 {
 WebSocket webSocket = userWebSocket.WebSocket;
 var msg = new CustomWebSocketMessage
 {
 MessagDateTime = DateTime.Now,
 Type = WSMessageType.anyType,
 Text = anyText,
 Username = "system"
 };

 string serialisedMessage = JsonConvert.SerializeObject(msg);
 byte[] bytes = Encoding.ASCII.GetBytes(serialisedMessage);
 await webSocket.SendAsync(new ArraySegment<byte>(bytes, 0, bytes.Length), WebSocketMessageType.Text, true, CancellationToken.None);
 }

 public async Task HandleMessage(WebSocketReceiveResult result, byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
 {
 string msg = Encoding.ASCII.GetString(buffer);
 try
 {
 var message = JsonConvert.DeserializeObject<CustomWebSocketMessage>(msg);
 if (message.Type == WSMessageType.anyType)
 {
 await BroadcastOthers(buffer, userWebSocket, wsFactory);
 }
 }
 catch (Exception e)
 {
 await userWebSocket.WebSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);
 }
 }

 public async Task BroadcastOthers(byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
 {
 var others = wsFactory.Others(userWebSocket);
 foreach (var uws in others)
 {
 await uws.WebSocket.SendAsync(new ArraySegment<byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, CancellationToken.None);
 }
 }

 public async Task BroadcastAll(byte[] buffer, CustomWebSocket userWebSocket, ICustomWebSocketFactory wsFactory)
 {
 var all = wsFactory.All();
 foreach (var uws in all)
 {
 await uws.WebSocket.SendAsync(new ArraySegment<byte>(buffer, 0, buffer.Length), WebSocketMessageType.Text, true, CancellationToken.None);
 }
 }
}

最后,在Configure方法的Startup類(lèi)中添加以下內(nèi)容:

var webSocketOptions = new WebSocketOptions()
{
 KeepAliveInterval = TimeSpan.FromSeconds(120),
 ReceiveBufferSize = 4 * 1024
};

app.UseWebSockets(webSocketOptions);
app.UseCustomWebSocketManager();

通過(guò)這種方式,Starup類(lèi)保持干凈,管理WebSockets的邏輯可以擴(kuò)展,使您可以根據(jù)自己的喜好靈活地組織它。

總結(jié)

聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Asp.net Core中如何使用中間件來(lái)管理websocket

Asp.net Core中如何使用中間件來(lái)管理websocket:介紹 我喜歡.NET CORE 這個(gè)東西,其實(shí)不僅僅源于它性能很高,可以跨平臺(tái),還因?yàn)樗脑O(shè)計(jì)模式確實(shí)令人著迷。以前沒(méi).NET CORE 的時(shí)候,.NET用websocket必須跑在windows server 2012上,但我一般不會(huì)這么干,都把websocket架在nodejs的服務(wù)器上
推薦度:
標(biāo)簽: web core 中間件
  • 熱門(mén)焦點(diǎn)

最新推薦

猜你喜歡

熱門(mén)推薦

專(zhuān)題
Top