2024-10-27 00:54:10 +08:00
|
|
|
|
using Serein.Library.Utils;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Net.WebSockets;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading;
|
|
|
|
|
|
using System.Threading.Channels;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
2025-07-27 23:34:01 +08:00
|
|
|
|
namespace Serein.Proto.WebSocket
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
2024-10-28 15:21:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 消息处理工具
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class MsgHandleUtil
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly Channel<string> _msgChannel;
|
|
|
|
|
|
|
2024-10-28 15:21:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化优先容器
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="capacity"></param>
|
|
|
|
|
|
public MsgHandleUtil(int capacity = 100)
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
2024-10-28 15:21:08 +08:00
|
|
|
|
_msgChannel = Channel.CreateBounded<string>(new BoundedChannelOptions(capacity)
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
FullMode = BoundedChannelFullMode.Wait
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
2024-10-28 15:21:08 +08:00
|
|
|
|
|
2024-10-27 00:54:10 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 等待消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<string> WaitMsgAsync()
|
|
|
|
|
|
{
|
2024-10-28 15:21:08 +08:00
|
|
|
|
// 检查是否可以读取消息
|
|
|
|
|
|
if (await _msgChannel.Reader.WaitToReadAsync())
|
|
|
|
|
|
{
|
|
|
|
|
|
return await _msgChannel.Reader.ReadAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
return null; // 若通道关闭,则返回null
|
2024-10-27 00:54:10 +08:00
|
|
|
|
}
|
2024-10-28 15:21:08 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 写入消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="msg">消息内容</param>
|
|
|
|
|
|
/// <returns>是否写入成功</returns>
|
|
|
|
|
|
public async Task<bool> WriteMsgAsync(string msg)
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
2024-10-28 15:21:08 +08:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _msgChannel.Writer.WriteAsync(msg);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (ChannelClosedException)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Channel 已关闭
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2024-10-27 00:54:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-28 15:21:08 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 尝试关闭通道,停止写入消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public void CloseChannel()
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
2024-10-28 15:21:08 +08:00
|
|
|
|
_msgChannel.Writer.Complete();
|
2024-10-27 00:54:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-10-28 15:21:08 +08:00
|
|
|
|
|
2024-10-27 00:54:10 +08:00
|
|
|
|
public class SocketExtension
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="webSocket"></param>
|
|
|
|
|
|
/// <param name="message"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2025-07-27 23:34:01 +08:00
|
|
|
|
public static async Task SendAsync(System.Net.WebSockets.WebSocket webSocket, string message)
|
2024-10-27 00:54:10 +08:00
|
|
|
|
{
|
|
|
|
|
|
var buffer = Encoding.UTF8.GetBytes(message);
|
|
|
|
|
|
await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|