NETCORE - WebSocket通信
webscoket 通信 一般为 客户端与服务端通信,如需两个服务端通信,可把其中一个服务端模拟成客户端。
项目框架 vue + .net6
创建 NET6 的 webapi 项目:NETCORE.WebSocketServer
创建类:MsgTemplate.cs
namespace NETCORE.WebSocketServer { public class MsgTemplate { public string SenderID { get; set; } public string ReceiverID { get; set; } public string MessageType { get; set; } public string Content { get; set; } } }
创建中间件 ChatWebSocketMiddleware.cs
using Newtonsoft.Json; using System.Collections.Concurrent; using System.Net.WebSockets; using System.Text; namespace NETCORE.WebSocketServer { public class ChatWebSocketMiddleware { private static ConcurrentDictionary<string, System.Net.WebSockets.WebSocket> _sockets = new ConcurrentDictionary<string, System.Net.WebSockets.WebSocket>(); private readonly RequestDelegate _next; public ChatWebSocketMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { if (!context.WebSockets.IsWebSocketRequest) { await _next.Invoke(context); return; } System.Net.WebSockets.WebSocket dummy; CancellationToken ct = context.RequestAborted; var currentSocket = await context.WebSockets.AcceptWebSocketAsync(); //string socketId = Guid.NewGuid().ToString(); string socketId = context.Request.Query["sid"].ToString(); if (!_sockets.ContainsKey(socketId)) { _sockets.TryAdd(socketId, currentSocket); } //_sockets.TryRemove(socketId, out dummy); //_sockets.TryAdd(socketId, currentSocket); while (true) { if (ct.IsCancellationRequested) { break; } string response = await ReceiveStringAsync(currentSocket, ct); Console.WriteLine("接收到数据:" + response); MsgTemplate msg = JsonConvert.DeserializeObject<MsgTemplate>(response); if (string.IsNullOrEmpty(response)) { if (currentSocket.State != WebSocketState.Open) { break; } continue; } foreach (var socket in _sockets) { if (socket.Value.State != WebSocketState.Open) { continue; } //控制只有接收者才能收到消息 if (socket.Key == msg.ReceiverID || socket.Key == socketId) { //await SendStringAsync(socket.Value, JsonConvert.SerializeObject(msg), ct); await SendStringAsync(socket.Value, $"收到,状态:正常,操作完成。", ct); } //if (socket.Key == "A0001") //{ // for (int i = 0; i < 10000; i++) // { // Thread.Sleep(1000); // await SendStringAsync(socket.Value, $"状态:正常,操作数:{i}", ct); // } //} } } //_sockets.TryRemove(socketId, out dummy); await currentSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", ct); currentSocket.Dispose(); } private static Task SendStringAsync(System.Net.WebSockets.WebSocket socket, string data, CancellationToken ct = default(CancellationToken)) { //var jsonData = JsonConvert.DeserializeObject<MsgTemplate>(data); //var sendText = "已收到信息,现在回复。" + jsonData.Content; var sendText = data; var buffer = Encoding.UTF8.GetBytes(sendText); var segment = new ArraySegment<byte>(buffer); return socket.SendAsync(segment, WebSocketMessageType.Text, true, ct); } private static async Task<string> ReceiveStringAsync(System.Net.WebSockets.WebSocket socket, CancellationToken ct = default(CancellationToken)) { var buffer = new ArraySegment<byte>(new byte[8192]); using (var ms = new MemoryStream()) { WebSocketReceiveResult result; do { ct.ThrowIfCancellationRequested(); result = await socket.ReceiveAsync(buffer, ct); ms.Write(buffer.Array, buffer.Offset, result.Count); } while (!result.EndOfMessage); ms.Seek(0, SeekOrigin.Begin); if (result.MessageType != WebSocketMessageType.Text) { return null; } using (var reader = new StreamReader(ms, Encoding.UTF8)) { return await reader.ReadToEndAsync(); } } } } }
在Program.cs 中使用中间件
app.UseWebSockets(); app.UseMiddleware<ChatWebSocketMiddleware>();
启动后 websocket 地址:
wss://localhost:7187
创建 VUE 项目:
<template> <div> websockettest </div> </template> <script> export default { mounted() { this.connectWebsocket(); }, methods: { connectWebsocket() { let websocket; if (typeof WebSocket === "undefined") { console.log("您的浏览器不支持WebSocket"); return; } else { let url = `wss://localhost:7187?sid=A0001`; // let url = `ws://127.0.0.1:18200?sid=A0001`; // 打开一个websocket websocket = new WebSocket(url); // 建立连接 websocket.onopen = () => { // sendData(0); // function sendData(iv) { // setTimeout(() => { // 发送数据 let obj = { SenderID: 'client', ReceiverID: 'A0001', MessageType: 'text', // Content: `客户端请求数据。![${iv}]` Content: `客户端请求数据。!` } websocket.send(JSON.stringify(obj)); console.log("websocket发送数据中"); // if (iv < 10000) sendData(++iv); // }, 1000); // } }; // 客户端接收服务端返回的数据 websocket.onmessage = evt => { console.log("websocket返回的数据:", evt); }; // 发生错误时 websocket.onerror = evt => { console.log("websocket错误:", evt); }; // 关闭连接 websocket.onclose = evt => { console.log("websocket关闭:", evt); }; } } } }; </script>
启动服务后:
注意要先启动 NETCORE 的后端服务。
参考:http://www.zzvips.com/article/64044.html
标签:websocket,socket,NETCORE,通信,var,WebSocket,string,ct From: https://www.cnblogs.com/1285026182YUAN/p/16819295.html