介绍
在 ASP.NET Core 中使用 WebSocket 协议创建实时通信的完整示例涉及几个步骤。在此示例中,我们将创建一个简单的聊天应用程序,用户可以在其中实时发送和接收消息。此示例假设您对 ASP.NET Core 和 C# 有基本了解。
步骤1.创建一个新的ASP.NET Core项目
首先,使用 Visual Studio 或命令行创建一个新的 ASP.NET Core 项目。您可以选择 ASP.NET Core Web 应用程序模板并选择空模板。
步骤 2. 安装 WebSocket NuGet 包
在您的项目中,您需要安装 Microsoft.AspNetCore.WebSockets NuGet 包,它提供 WebSocket 支持。
dotnet add package Microsoft.AspNetCore.WebSockets
步骤 3. 创建 WebSocket 中间件
在 Startup.cs 文件中,在 Configure 方法中配置 WebSocket 中间件。
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WebSocketChatApp.Middleware;
Author: Sardar Mudassar Ali Khan
namespace WebSocketChatApp
{
public class Startup
{
// ... other configurations ...
public void ConfigureServices(IServiceCollection services)
{
// ... other services ...
services.AddWebSocketManager();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ... other middleware ...
app.UseWebSockets();
app.MapWebSocketManager("/chat", app.ApplicationServices.GetService<ChatWebSocketHandler>());
// ... other configurations ...
}
}
}
步骤 4. 创建 WebSocket 处理程序
创建 WebSocket 处理程序类来管理 WebSocket 连接和消息。您可以将其命名为 ChatWebSocketHandler.cs。
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
Author: Sardar Mudassar Ali Khan
namespace WebSocketChatApp.Middleware
{
public class ChatWebSocketHandler
{
private readonly WebSocketConnectionManager _connectionManager;
private readonly ILogger<ChatWebSocketHandler> _logger;
public ChatWebSocketHandler(WebSocketConnectionManager connectionManager, ILogger<ChatWebSocketHandler> logger)
{
_connectionManager = connectionManager;
_logger = logger;
}
public async Task HandleWebSocket(HttpContext context, WebSocket webSocket)
{
var socketId = _connectionManager.AddSocket(webSocket);
_logger.LogInformation($"WebSocket connection established with ID {socketId}");
while (webSocket.State == WebSocketState.Open)
{
var message = await ReceiveMessageAsync(webSocket);
if (message != null)
{
_logger.LogInformation($"Received message from ID {socketId}: {message}");
await BroadcastMessageAsync(message);
}
}
_connectionManager.RemoveSocket(socketId);
_logger.LogInformation($"WebSocket connection closed with ID {socketId}");
}
private async Task<string?> ReceiveMessageAsync(WebSocket webSocket)
{
var buffer = new byte[1024];
var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None);
if (result.CloseStatus.HasValue)
{
return null;
}
return System.Text.Encoding.UTF8.GetString(buffer, 0, result.Count);
}
private async Task BroadcastMessageAsync(string message)
{
foreach (var socket in _connectionManager.GetAllSockets())
{
if (socket.Value.State == WebSocketState.Open)
{
await socket.Value.SendAsync(System.Text.Encoding.UTF8.GetBytes(message), WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}
}
}
步骤 5. 创建 WebSocket 连接管理器
创建 WebSocket 连接管理器来跟踪连接的 WebSocket 客户端。您可以将其命名为 WebSocketConnectionManager.cs。
using System;
using System.Collections.Concurrent;
using System.Net.WebSockets;
Author: Sardar Mudassar Ali Khan
namespace WebSocketChatApp.Middleware
{
public class WebSocketConnectionManager
{
private readonly ConcurrentDictionary<Guid, WebSocket> _sockets = new ConcurrentDictionary<Guid, WebSocket>();
public WebSocket AddSocket(WebSocket socket)
{
var socketId = Guid.NewGuid();
_sockets.TryAdd(socketId, socket);
return socketId;
}
public WebSocket? GetSocket(Guid socketId)
{
_sockets.TryGetValue(socketId, out var socket);
return socket;
}
public ConcurrentDictionary<Guid, WebSocket>.ValueCollection GetAllSockets()
{
return _sockets.Values;
}
public Guid? GetSocketId(WebSocket socket)
{
foreach (var (key, value) in _sockets)
{
if (value == socket)
{
return key;
}
}
return null;
}
public void RemoveSocket(Guid socketId)
{
_sockets.TryRemove(socketId, out _);
}
}
}
步骤 6. 创建 WebSocket 端点
在控制器中创建一个 WebSocket 端点来处理 WebSocket 连接。
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net.WebSockets;
using System.Threading.Tasks;
using WebSocketChatApp.Middleware;
Author: Sardar Mudassar Ali Khan
namespace WebSocketChatApp.Controllers
{
[Route("api/[controller]")]
public class ChatController : ControllerBase
{
private readonly ChatWebSocketHandler _webSocketHandler;
public ChatController(ChatWebSocketHandler webSocketHandler)
{
_webSocketHandler = webSocketHandler;
}
[HttpGet]
public async Task<IActionResult> Get()
{
if (HttpContext.WebSockets.IsWebSocketRequest)
{
var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
await _webSocketHandler.HandleWebSocket(HttpContext, webSocket);
}
else
{
return BadRequest("WebSocket is not supported.");
}
return Ok();
}
}
}
步骤 7. 创建客户端应用程序
使用 JavaScript 创建一个简单的 HTML 页面来连接到 WebSocket 服务器并发送/接收消息。这是一个基本示例:
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Chat By Sardar Mudassar Ali Khan</title>
</head>
<body>
<input type="text" id="messageInput" placeholder="Enter your message" />
<button οnclick="sendMessage()">Send</button>
<div id="chat"></div>
<script>
const socket = new WebSocket("ws://localhost:5000/api/chat");
socket.onopen = (event) => {
console.log("WebSocket connection established.");
};
socket.onmessage = (event) => {
const chatDiv = document.getElementById("chat");
chatDiv.innerHTML += `<p>${event.data}</p>`;
};
socket.onclose = (event) => {
if (event.wasClean) {
console.log(`WebSocket connection closed cleanly, code=${event.code}, reason=${event.reason}`);
} else {
console.error(`WebSocket connection died`);
}
};
function sendMessage() {
const messageInput = document.getElementById("messageInput");
const message = messageInput.value;
socket.send(message);
messageInput.value = "";
}
</script>
</body>
</html>
步骤 8. 运行应用程序
构建并运行您的 ASP.NET Core 应用程序。在浏览器中访问 WebSocket 聊天页面。您应该能够实时发送和接收消息。
此示例提供了 ASP.NET Core 中 WebSocket 聊天应用程序的基本实现。您可以根据需要扩展和自定义它。
结论
此示例提供了使用 WebSocket 在 ASP.NET Core 中进行实时通信的基础结构。您可以在此基础上进行扩展,通过添加身份验证、用户管理和消息持久性等功能来创建更复杂的实时应用程序,例如聊天室、通知或协作编辑工具。