Files
serein-flow/Serein.Proto.WebSocket/TestExtension.cs

94 lines
2.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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;
namespace Serein.Proto.WebSocket
{
/// <summary>
/// 消息处理工具
/// </summary>
public class MsgHandleUtil
{
private readonly Channel<string> _msgChannel;
/// <summary>
/// 初始化优先容器
/// </summary>
/// <param name="capacity"></param>
public MsgHandleUtil(int capacity = 100)
{
_msgChannel = Channel.CreateBounded<string>(new BoundedChannelOptions(capacity)
{
FullMode = BoundedChannelFullMode.Wait
});
}
/// <summary>
/// 等待消息
/// </summary>
/// <returns></returns>
public async Task<string> WaitMsgAsync()
{
// 检查是否可以读取消息
if (await _msgChannel.Reader.WaitToReadAsync())
{
return await _msgChannel.Reader.ReadAsync();
}
return null; // 若通道关闭则返回null
}
/// <summary>
/// 写入消息
/// </summary>
/// <param name="msg">消息内容</param>
/// <returns>是否写入成功</returns>
public async Task<bool> WriteMsgAsync(string msg)
{
try
{
await _msgChannel.Writer.WriteAsync(msg);
return true;
}
catch (ChannelClosedException)
{
// Channel 已关闭
return false;
}
}
/// <summary>
/// 尝试关闭通道,停止写入消息
/// </summary>
public void CloseChannel()
{
_msgChannel.Writer.Complete();
}
}
public class SocketExtension
{
/// <summary>
/// 发送消息
/// </summary>
/// <param name="webSocket"></param>
/// <param name="message"></param>
/// <returns></returns>
public static async Task SendAsync(System.Net.WebSockets.WebSocket webSocket, string message)
{
var buffer = Encoding.UTF8.GetBytes(message);
await webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
}
}
}