123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811 |
- //------------------------------------------------------------------------------
- // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
- // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
- // CSDN博客:https://blog.csdn.net/qq_40374647
- // 哔哩哔哩视频:https://space.bilibili.com/94253567
- // Gitee源代码仓库:https://gitee.com/RRQM_Home
- // Github源代码仓库:https://github.com/RRQM
- // API首页:https://www.yuque.com/rrqm/touchsocket/index
- // 交流QQ群:234762506
- // 感谢您的下载和使用
- //------------------------------------------------------------------------------
- //------------------------------------------------------------------------------
- using System;
- using TouchSocket.Core;
- namespace TouchSocket.Http.WebSockets
- {
- /// <summary>
- /// IWSClientBase辅助扩展
- /// </summary>
- public static class WSClientExtensions
- {
- /// <summary>
- /// 是否已经完成握手
- /// </summary>
- /// <param name="client"></param>
- /// <returns></returns>
- public static bool GetHandshaked(this IHttpClientBase client)
- {
- return client.GetValue<bool>(WebSocketServerPlugin.HandshakedProperty);
- }
- /// <summary>
- /// 获取WebSocket版本号。
- /// </summary>
- /// <param name="client"></param>
- /// <returns></returns>
- public static string GetWebSocketVersion(this IHttpClientBase client)
- {
- return client.GetValue<string>(WebSocketServerPlugin.WebSocketVersionProperty);
- }
- /// <summary>
- /// 设置WebSocket版本号。
- /// </summary>
- public static void SetWebSocketVersion(this IHttpClientBase client, string value)
- {
- client.SetValue(WebSocketServerPlugin.WebSocketVersionProperty, value);
- }
- #region 客户端
- /// <summary>
- /// 发送Ping报文。
- /// </summary>
- /// <param name="client"></param>
- /// <returns></returns>
- public static bool PingWS(this HttpClientBase client)
- {
- try
- {
- SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Ping });
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 发送Pong报文。
- /// </summary>
- /// <param name="client"></param>
- /// <returns></returns>
- public static bool PongWS(this HttpClientBase client)
- {
- try
- {
- SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Pong });
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 发送Close报文。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="msg"></param>
- /// <returns></returns>
- public static bool CloseWithWS(this HttpClientBase client, string msg)
- {
- try
- {
- SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Close }.AppendText(msg));
- return true;
- }
- catch
- {
- return false;
- }
- }
- #region 同步发送
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- public static void SendWithWS(this HttpClientBase client, byte[] buffer, int offset, int length)
- {
- using (ByteBlock byteBlock = new ByteBlock(length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendBinary(buffer, offset, length).BuildRequest(byteBlock);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- public static void SendWithWS(this HttpClientBase client, ByteBlock byteBlock)
- {
- SendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len);
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- public static void SendWithWS(this HttpClientBase client, byte[] buffer)
- {
- SendWithWS(client, buffer, 0, buffer.Length);
- }
- /// <summary>
- /// 采用WebSocket协议,发送文本数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="text"></param>
- public static void SendWithWS(this HttpClientBase client, string text)
- {
- using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendText(text).BuildRequest(byteBlock);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送WS数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="dataFrame"></param>
- public static void SendWithWS(this HttpClientBase client, WSDataFrame dataFrame)
- {
- using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
- {
- dataFrame.BuildRequest(byteBlock);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- #endregion 同步发送
- #region 异步发送
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- public static void SendWithWSAsync(this HttpClientBase client, byte[] buffer, int offset, int length)
- {
- using (ByteBlock byteBlock = new ByteBlock(length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendBinary(buffer, offset, length).BuildRequest(byteBlock);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- public static void SendWithWSAsync(this HttpClientBase client, ByteBlock byteBlock)
- {
- SendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len);
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- public static void SendWithWSAsync(this HttpClientBase client, byte[] buffer)
- {
- SendWithWSAsync(client, buffer, 0, buffer.Length);
- }
- /// <summary>
- /// 采用WebSocket协议,发送文本数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="text"></param>
- public static void SendWithWSAsync(this HttpClientBase client, string text)
- {
- using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendText(text).BuildRequest(byteBlock);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送WS数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="dataFrame"></param>
- public static void SendWithWSAsync(this HttpClientBase client, WSDataFrame dataFrame)
- {
- using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
- {
- dataFrame.BuildRequest(byteBlock);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- #endregion 异步发送
- #region 同步分包发送
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWS(this HttpClientBase client, byte[] buffer, int offset, int length, int packageSize)
- {
- lock (client)
- {
- if (packageSize >= length)
- {
- SendWithWS(client, buffer, offset, length);
- return;
- }
- int sentLen = 0;
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.SetMaskString("RRQM");
- dataFrame.Mask = true;
- dataFrame.FIN = false;
- dataFrame.Opcode = WSDataType.Binary;
- int count;
- if (length % packageSize == 0)
- {
- count = length / packageSize;
- }
- else
- {
- count = length / packageSize + 1;
- }
- for (int i = 0; i < count; i++)
- {
- if (i > 0)
- {
- dataFrame.Opcode = WSDataType.Cont;
- }
- if (i == count - 1)//最后
- {
- dataFrame.FIN = true;
- }
- using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
- {
- int thisLen = Math.Min(packageSize, length - sentLen);
- WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- sentLen += thisLen;
- offset += thisLen;
- }
- }
- }
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWS(this HttpClientBase client, byte[] buffer, int packageSize)
- {
- SubSendWithWS(client, buffer, 0, buffer.Length, packageSize);
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWS(this HttpClientBase client, ByteBlock byteBlock, int packageSize)
- {
- SubSendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
- }
- #endregion 同步分包发送
- #region 异步分包发送
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWSAsync(this HttpClientBase client, byte[] buffer, int offset, int length, int packageSize)
- {
- lock (client)
- {
- if (packageSize >= length)
- {
- SendWithWSAsync(client, buffer, offset, length);
- return;
- }
- int sentLen = 0;
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.SetMaskString("RRQM");
- dataFrame.Mask = true;
- dataFrame.FIN = false;
- dataFrame.Opcode = WSDataType.Binary;
- int count;
- if (length % packageSize == 0)
- {
- count = length / packageSize;
- }
- else
- {
- count = length / packageSize + 1;
- }
- for (int i = 0; i < count; i++)
- {
- if (i > 0)
- {
- dataFrame.Opcode = WSDataType.Cont;
- }
- if (i == count - 1)//最后
- {
- dataFrame.FIN = true;
- }
- using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
- {
- int thisLen = Math.Min(packageSize, length - sentLen);
- WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- sentLen += thisLen;
- offset += thisLen;
- }
- }
- }
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWSAsync(this HttpClientBase client, byte[] buffer, int packageSize)
- {
- SubSendWithWSAsync(client, buffer, 0, buffer.Length, packageSize);
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWSAsync(this HttpClientBase client, ByteBlock byteBlock, int packageSize)
- {
- SubSendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
- }
- #endregion 异步分包发送
- #endregion 客户端
- #region 服务器
- /// <summary>
- /// 发送WebSocket协议的Ping报文。
- /// </summary>
- /// <param name="client"></param>
- /// <returns></returns>
- public static bool PingWS(this HttpSocketClient client)
- {
- try
- {
- SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Ping });
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 发送WebSocket协议的Pong报文。
- /// </summary>
- /// <param name="client"></param>
- /// <returns></returns>
- public static bool PongWS(this HttpSocketClient client)
- {
- try
- {
- SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Pong });
- return true;
- }
- catch
- {
- return false;
- }
- }
- /// <summary>
- /// 发送WebSocket协议的Close报文。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="msg"></param>
- /// <returns></returns>
- public static bool CloseWithWS(this HttpSocketClient client, string msg)
- {
- try
- {
- SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Close }.AppendText(msg));
- return true;
- }
- catch
- {
- return false;
- }
- }
- #region 同步发送
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- public static void SendWithWS(this HttpSocketClient client, byte[] buffer, int offset, int length)
- {
- using (ByteBlock byteBlock = new ByteBlock(length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendBinary(buffer, offset, length).BuildResponse(byteBlock);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- public static void SendWithWS(this HttpSocketClient client, ByteBlock byteBlock)
- {
- SendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len);
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- public static void SendWithWS(this HttpSocketClient client, byte[] buffer)
- {
- SendWithWS(client, buffer, 0, buffer.Length);
- }
- /// <summary>
- /// 采用WebSocket协议,发送文本数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="text"></param>
- public static void SendWithWS(this HttpSocketClient client, string text)
- {
- using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendText(text).BuildResponse(byteBlock);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送WS数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="dataFrame"></param>
- public static void SendWithWS(this HttpSocketClient client, WSDataFrame dataFrame)
- {
- using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
- {
- dataFrame.BuildResponse(byteBlock);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- #endregion 同步发送
- #region 异步发送
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- public static void SendWithWSAsync(this HttpSocketClient client, byte[] buffer, int offset, int length)
- {
- using (ByteBlock byteBlock = new ByteBlock(length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendBinary(buffer, offset, length).BuildResponse(byteBlock);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- public static void SendWithWSAsync(this HttpSocketClient client, ByteBlock byteBlock)
- {
- SendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len);
- }
- /// <summary>
- /// 采用WebSocket协议,发送二进制流数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- public static void SendWithWSAsync(this HttpSocketClient client, byte[] buffer)
- {
- SendWithWSAsync(client, buffer, 0, buffer.Length);
- }
- /// <summary>
- /// 采用WebSocket协议,发送文本数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="text"></param>
- public static void SendWithWSAsync(this HttpSocketClient client, string text)
- {
- using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
- {
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.AppendText(text).BuildResponse(byteBlock);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- /// <summary>
- /// 采用WebSocket协议,发送WS数据。
- /// </summary>
- /// <param name="client"></param>
- /// <param name="dataFrame"></param>
- public static void SendWithWSAsync(this HttpSocketClient client, WSDataFrame dataFrame)
- {
- using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
- {
- dataFrame.BuildResponse(byteBlock);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- }
- }
- #endregion 异步发送
- #region 同步分包发送
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWS(this HttpSocketClient client, byte[] buffer, int offset, int length, int packageSize)
- {
- lock (client)
- {
- if (packageSize >= length)
- {
- SendWithWS(client, buffer, offset, length);
- return;
- }
- int sentLen = 0;
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.FIN = false;
- dataFrame.Opcode = WSDataType.Binary;
- int count;
- if (length % packageSize == 0)
- {
- count = length / packageSize;
- }
- else
- {
- count = length / packageSize + 1;
- }
- for (int i = 0; i < count; i++)
- {
- if (i > 0)
- {
- dataFrame.Opcode = WSDataType.Cont;
- }
- if (i == count - 1)//最后
- {
- dataFrame.FIN = true;
- }
- using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
- {
- int thisLen = Math.Min(packageSize, length - sentLen);
- WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
- client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
- sentLen += thisLen;
- offset += thisLen;
- }
- }
- }
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWS(this HttpSocketClient client, byte[] buffer, int packageSize)
- {
- SubSendWithWS(client, buffer, 0, buffer.Length, packageSize);
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWS(this HttpSocketClient client, ByteBlock byteBlock, int packageSize)
- {
- SubSendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
- }
- #endregion 同步分包发送
- #region 异步分包发送
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="offset"></param>
- /// <param name="length"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWSAsync(this HttpSocketClient client, byte[] buffer, int offset, int length, int packageSize)
- {
- lock (client)
- {
- if (packageSize >= length)
- {
- SendWithWSAsync(client, buffer, offset, length);
- return;
- }
- int sentLen = 0;
- WSDataFrame dataFrame = new WSDataFrame();
- dataFrame.FIN = false;
- dataFrame.Opcode = WSDataType.Binary;
- int count;
- if (length % packageSize == 0)
- {
- count = length / packageSize;
- }
- else
- {
- count = length / packageSize + 1;
- }
- for (int i = 0; i < count; i++)
- {
- if (i > 0)
- {
- dataFrame.Opcode = WSDataType.Cont;
- }
- if (i == count - 1)//最后
- {
- dataFrame.FIN = true;
- }
- using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
- {
- int thisLen = Math.Min(packageSize, length - sentLen);
- WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
- client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
- sentLen += thisLen;
- offset += thisLen;
- }
- }
- }
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="buffer"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWSAsync(this HttpSocketClient client, byte[] buffer, int packageSize)
- {
- SubSendWithWSAsync(client, buffer, 0, buffer.Length, packageSize);
- }
- /// <summary>
- /// 分包发送。
- /// <para>
- /// 按packageSize的值,每次发送数据包。
- /// </para>
- /// </summary>
- /// <param name="client"></param>
- /// <param name="byteBlock"></param>
- /// <param name="packageSize"></param>
- public static void SubSendWithWSAsync(this HttpSocketClient client, ByteBlock byteBlock, int packageSize)
- {
- SubSendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
- }
- #endregion 异步分包发送
- #endregion 服务器
- }
- }
|