WSClientExtensions.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. //------------------------------------------------------------------------------
  2. // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有
  3. // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权
  4. // CSDN博客:https://blog.csdn.net/qq_40374647
  5. // 哔哩哔哩视频:https://space.bilibili.com/94253567
  6. // Gitee源代码仓库:https://gitee.com/RRQM_Home
  7. // Github源代码仓库:https://github.com/RRQM
  8. // API首页:https://www.yuque.com/rrqm/touchsocket/index
  9. // 交流QQ群:234762506
  10. // 感谢您的下载和使用
  11. //------------------------------------------------------------------------------
  12. //------------------------------------------------------------------------------
  13. using System;
  14. using TouchSocket.Core;
  15. namespace TouchSocket.Http.WebSockets
  16. {
  17. /// <summary>
  18. /// IWSClientBase辅助扩展
  19. /// </summary>
  20. public static class WSClientExtensions
  21. {
  22. /// <summary>
  23. /// 是否已经完成握手
  24. /// </summary>
  25. /// <param name="client"></param>
  26. /// <returns></returns>
  27. public static bool GetHandshaked(this IHttpClientBase client)
  28. {
  29. return client.GetValue<bool>(WebSocketServerPlugin.HandshakedProperty);
  30. }
  31. /// <summary>
  32. /// 获取WebSocket版本号。
  33. /// </summary>
  34. /// <param name="client"></param>
  35. /// <returns></returns>
  36. public static string GetWebSocketVersion(this IHttpClientBase client)
  37. {
  38. return client.GetValue<string>(WebSocketServerPlugin.WebSocketVersionProperty);
  39. }
  40. /// <summary>
  41. /// 设置WebSocket版本号。
  42. /// </summary>
  43. public static void SetWebSocketVersion(this IHttpClientBase client, string value)
  44. {
  45. client.SetValue(WebSocketServerPlugin.WebSocketVersionProperty, value);
  46. }
  47. #region 客户端
  48. /// <summary>
  49. /// 发送Ping报文。
  50. /// </summary>
  51. /// <param name="client"></param>
  52. /// <returns></returns>
  53. public static bool PingWS(this HttpClientBase client)
  54. {
  55. try
  56. {
  57. SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Ping });
  58. return true;
  59. }
  60. catch
  61. {
  62. return false;
  63. }
  64. }
  65. /// <summary>
  66. /// 发送Pong报文。
  67. /// </summary>
  68. /// <param name="client"></param>
  69. /// <returns></returns>
  70. public static bool PongWS(this HttpClientBase client)
  71. {
  72. try
  73. {
  74. SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Pong });
  75. return true;
  76. }
  77. catch
  78. {
  79. return false;
  80. }
  81. }
  82. /// <summary>
  83. /// 发送Close报文。
  84. /// </summary>
  85. /// <param name="client"></param>
  86. /// <param name="msg"></param>
  87. /// <returns></returns>
  88. public static bool CloseWithWS(this HttpClientBase client, string msg)
  89. {
  90. try
  91. {
  92. SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Close }.AppendText(msg));
  93. return true;
  94. }
  95. catch
  96. {
  97. return false;
  98. }
  99. }
  100. #region 同步发送
  101. /// <summary>
  102. /// 采用WebSocket协议,发送二进制流数据。
  103. /// </summary>
  104. /// <param name="client"></param>
  105. /// <param name="buffer"></param>
  106. /// <param name="offset"></param>
  107. /// <param name="length"></param>
  108. public static void SendWithWS(this HttpClientBase client, byte[] buffer, int offset, int length)
  109. {
  110. using (ByteBlock byteBlock = new ByteBlock(length + 1024))
  111. {
  112. WSDataFrame dataFrame = new WSDataFrame();
  113. dataFrame.AppendBinary(buffer, offset, length).BuildRequest(byteBlock);
  114. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  115. }
  116. }
  117. /// <summary>
  118. /// 采用WebSocket协议,发送二进制流数据。
  119. /// </summary>
  120. /// <param name="client"></param>
  121. /// <param name="byteBlock"></param>
  122. public static void SendWithWS(this HttpClientBase client, ByteBlock byteBlock)
  123. {
  124. SendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len);
  125. }
  126. /// <summary>
  127. /// 采用WebSocket协议,发送二进制流数据。
  128. /// </summary>
  129. /// <param name="client"></param>
  130. /// <param name="buffer"></param>
  131. public static void SendWithWS(this HttpClientBase client, byte[] buffer)
  132. {
  133. SendWithWS(client, buffer, 0, buffer.Length);
  134. }
  135. /// <summary>
  136. /// 采用WebSocket协议,发送文本数据。
  137. /// </summary>
  138. /// <param name="client"></param>
  139. /// <param name="text"></param>
  140. public static void SendWithWS(this HttpClientBase client, string text)
  141. {
  142. using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
  143. {
  144. WSDataFrame dataFrame = new WSDataFrame();
  145. dataFrame.AppendText(text).BuildRequest(byteBlock);
  146. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  147. }
  148. }
  149. /// <summary>
  150. /// 采用WebSocket协议,发送WS数据。
  151. /// </summary>
  152. /// <param name="client"></param>
  153. /// <param name="dataFrame"></param>
  154. public static void SendWithWS(this HttpClientBase client, WSDataFrame dataFrame)
  155. {
  156. using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
  157. {
  158. dataFrame.BuildRequest(byteBlock);
  159. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  160. }
  161. }
  162. #endregion 同步发送
  163. #region 异步发送
  164. /// <summary>
  165. /// 采用WebSocket协议,发送二进制流数据。
  166. /// </summary>
  167. /// <param name="client"></param>
  168. /// <param name="buffer"></param>
  169. /// <param name="offset"></param>
  170. /// <param name="length"></param>
  171. public static void SendWithWSAsync(this HttpClientBase client, byte[] buffer, int offset, int length)
  172. {
  173. using (ByteBlock byteBlock = new ByteBlock(length + 1024))
  174. {
  175. WSDataFrame dataFrame = new WSDataFrame();
  176. dataFrame.AppendBinary(buffer, offset, length).BuildRequest(byteBlock);
  177. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  178. }
  179. }
  180. /// <summary>
  181. /// 采用WebSocket协议,发送二进制流数据。
  182. /// </summary>
  183. /// <param name="client"></param>
  184. /// <param name="byteBlock"></param>
  185. public static void SendWithWSAsync(this HttpClientBase client, ByteBlock byteBlock)
  186. {
  187. SendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len);
  188. }
  189. /// <summary>
  190. /// 采用WebSocket协议,发送二进制流数据。
  191. /// </summary>
  192. /// <param name="client"></param>
  193. /// <param name="buffer"></param>
  194. public static void SendWithWSAsync(this HttpClientBase client, byte[] buffer)
  195. {
  196. SendWithWSAsync(client, buffer, 0, buffer.Length);
  197. }
  198. /// <summary>
  199. /// 采用WebSocket协议,发送文本数据。
  200. /// </summary>
  201. /// <param name="client"></param>
  202. /// <param name="text"></param>
  203. public static void SendWithWSAsync(this HttpClientBase client, string text)
  204. {
  205. using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
  206. {
  207. WSDataFrame dataFrame = new WSDataFrame();
  208. dataFrame.AppendText(text).BuildRequest(byteBlock);
  209. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  210. }
  211. }
  212. /// <summary>
  213. /// 采用WebSocket协议,发送WS数据。
  214. /// </summary>
  215. /// <param name="client"></param>
  216. /// <param name="dataFrame"></param>
  217. public static void SendWithWSAsync(this HttpClientBase client, WSDataFrame dataFrame)
  218. {
  219. using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
  220. {
  221. dataFrame.BuildRequest(byteBlock);
  222. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  223. }
  224. }
  225. #endregion 异步发送
  226. #region 同步分包发送
  227. /// <summary>
  228. /// 分包发送。
  229. /// <para>
  230. /// 按packageSize的值,每次发送数据包。
  231. /// </para>
  232. /// </summary>
  233. /// <param name="client"></param>
  234. /// <param name="buffer"></param>
  235. /// <param name="offset"></param>
  236. /// <param name="length"></param>
  237. /// <param name="packageSize"></param>
  238. public static void SubSendWithWS(this HttpClientBase client, byte[] buffer, int offset, int length, int packageSize)
  239. {
  240. lock (client)
  241. {
  242. if (packageSize >= length)
  243. {
  244. SendWithWS(client, buffer, offset, length);
  245. return;
  246. }
  247. int sentLen = 0;
  248. WSDataFrame dataFrame = new WSDataFrame();
  249. dataFrame.SetMaskString("RRQM");
  250. dataFrame.Mask = true;
  251. dataFrame.FIN = false;
  252. dataFrame.Opcode = WSDataType.Binary;
  253. int count;
  254. if (length % packageSize == 0)
  255. {
  256. count = length / packageSize;
  257. }
  258. else
  259. {
  260. count = length / packageSize + 1;
  261. }
  262. for (int i = 0; i < count; i++)
  263. {
  264. if (i > 0)
  265. {
  266. dataFrame.Opcode = WSDataType.Cont;
  267. }
  268. if (i == count - 1)//最后
  269. {
  270. dataFrame.FIN = true;
  271. }
  272. using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
  273. {
  274. int thisLen = Math.Min(packageSize, length - sentLen);
  275. WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
  276. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  277. sentLen += thisLen;
  278. offset += thisLen;
  279. }
  280. }
  281. }
  282. }
  283. /// <summary>
  284. /// 分包发送。
  285. /// <para>
  286. /// 按packageSize的值,每次发送数据包。
  287. /// </para>
  288. /// </summary>
  289. /// <param name="client"></param>
  290. /// <param name="buffer"></param>
  291. /// <param name="packageSize"></param>
  292. public static void SubSendWithWS(this HttpClientBase client, byte[] buffer, int packageSize)
  293. {
  294. SubSendWithWS(client, buffer, 0, buffer.Length, packageSize);
  295. }
  296. /// <summary>
  297. /// 分包发送。
  298. /// <para>
  299. /// 按packageSize的值,每次发送数据包。
  300. /// </para>
  301. /// </summary>
  302. /// <param name="client"></param>
  303. /// <param name="byteBlock"></param>
  304. /// <param name="packageSize"></param>
  305. public static void SubSendWithWS(this HttpClientBase client, ByteBlock byteBlock, int packageSize)
  306. {
  307. SubSendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
  308. }
  309. #endregion 同步分包发送
  310. #region 异步分包发送
  311. /// <summary>
  312. /// 分包发送。
  313. /// <para>
  314. /// 按packageSize的值,每次发送数据包。
  315. /// </para>
  316. /// </summary>
  317. /// <param name="client"></param>
  318. /// <param name="buffer"></param>
  319. /// <param name="offset"></param>
  320. /// <param name="length"></param>
  321. /// <param name="packageSize"></param>
  322. public static void SubSendWithWSAsync(this HttpClientBase client, byte[] buffer, int offset, int length, int packageSize)
  323. {
  324. lock (client)
  325. {
  326. if (packageSize >= length)
  327. {
  328. SendWithWSAsync(client, buffer, offset, length);
  329. return;
  330. }
  331. int sentLen = 0;
  332. WSDataFrame dataFrame = new WSDataFrame();
  333. dataFrame.SetMaskString("RRQM");
  334. dataFrame.Mask = true;
  335. dataFrame.FIN = false;
  336. dataFrame.Opcode = WSDataType.Binary;
  337. int count;
  338. if (length % packageSize == 0)
  339. {
  340. count = length / packageSize;
  341. }
  342. else
  343. {
  344. count = length / packageSize + 1;
  345. }
  346. for (int i = 0; i < count; i++)
  347. {
  348. if (i > 0)
  349. {
  350. dataFrame.Opcode = WSDataType.Cont;
  351. }
  352. if (i == count - 1)//最后
  353. {
  354. dataFrame.FIN = true;
  355. }
  356. using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
  357. {
  358. int thisLen = Math.Min(packageSize, length - sentLen);
  359. WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
  360. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  361. sentLen += thisLen;
  362. offset += thisLen;
  363. }
  364. }
  365. }
  366. }
  367. /// <summary>
  368. /// 分包发送。
  369. /// <para>
  370. /// 按packageSize的值,每次发送数据包。
  371. /// </para>
  372. /// </summary>
  373. /// <param name="client"></param>
  374. /// <param name="buffer"></param>
  375. /// <param name="packageSize"></param>
  376. public static void SubSendWithWSAsync(this HttpClientBase client, byte[] buffer, int packageSize)
  377. {
  378. SubSendWithWSAsync(client, buffer, 0, buffer.Length, packageSize);
  379. }
  380. /// <summary>
  381. /// 分包发送。
  382. /// <para>
  383. /// 按packageSize的值,每次发送数据包。
  384. /// </para>
  385. /// </summary>
  386. /// <param name="client"></param>
  387. /// <param name="byteBlock"></param>
  388. /// <param name="packageSize"></param>
  389. public static void SubSendWithWSAsync(this HttpClientBase client, ByteBlock byteBlock, int packageSize)
  390. {
  391. SubSendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
  392. }
  393. #endregion 异步分包发送
  394. #endregion 客户端
  395. #region 服务器
  396. /// <summary>
  397. /// 发送WebSocket协议的Ping报文。
  398. /// </summary>
  399. /// <param name="client"></param>
  400. /// <returns></returns>
  401. public static bool PingWS(this HttpSocketClient client)
  402. {
  403. try
  404. {
  405. SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Ping });
  406. return true;
  407. }
  408. catch
  409. {
  410. return false;
  411. }
  412. }
  413. /// <summary>
  414. /// 发送WebSocket协议的Pong报文。
  415. /// </summary>
  416. /// <param name="client"></param>
  417. /// <returns></returns>
  418. public static bool PongWS(this HttpSocketClient client)
  419. {
  420. try
  421. {
  422. SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Pong });
  423. return true;
  424. }
  425. catch
  426. {
  427. return false;
  428. }
  429. }
  430. /// <summary>
  431. /// 发送WebSocket协议的Close报文。
  432. /// </summary>
  433. /// <param name="client"></param>
  434. /// <param name="msg"></param>
  435. /// <returns></returns>
  436. public static bool CloseWithWS(this HttpSocketClient client, string msg)
  437. {
  438. try
  439. {
  440. SendWithWS(client, new WSDataFrame() { FIN = true, Opcode = WSDataType.Close }.AppendText(msg));
  441. return true;
  442. }
  443. catch
  444. {
  445. return false;
  446. }
  447. }
  448. #region 同步发送
  449. /// <summary>
  450. /// 采用WebSocket协议,发送二进制流数据。
  451. /// </summary>
  452. /// <param name="client"></param>
  453. /// <param name="buffer"></param>
  454. /// <param name="offset"></param>
  455. /// <param name="length"></param>
  456. public static void SendWithWS(this HttpSocketClient client, byte[] buffer, int offset, int length)
  457. {
  458. using (ByteBlock byteBlock = new ByteBlock(length + 1024))
  459. {
  460. WSDataFrame dataFrame = new WSDataFrame();
  461. dataFrame.AppendBinary(buffer, offset, length).BuildResponse(byteBlock);
  462. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  463. }
  464. }
  465. /// <summary>
  466. /// 采用WebSocket协议,发送二进制流数据。
  467. /// </summary>
  468. /// <param name="client"></param>
  469. /// <param name="byteBlock"></param>
  470. public static void SendWithWS(this HttpSocketClient client, ByteBlock byteBlock)
  471. {
  472. SendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len);
  473. }
  474. /// <summary>
  475. /// 采用WebSocket协议,发送二进制流数据。
  476. /// </summary>
  477. /// <param name="client"></param>
  478. /// <param name="buffer"></param>
  479. public static void SendWithWS(this HttpSocketClient client, byte[] buffer)
  480. {
  481. SendWithWS(client, buffer, 0, buffer.Length);
  482. }
  483. /// <summary>
  484. /// 采用WebSocket协议,发送文本数据。
  485. /// </summary>
  486. /// <param name="client"></param>
  487. /// <param name="text"></param>
  488. public static void SendWithWS(this HttpSocketClient client, string text)
  489. {
  490. using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
  491. {
  492. WSDataFrame dataFrame = new WSDataFrame();
  493. dataFrame.AppendText(text).BuildResponse(byteBlock);
  494. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  495. }
  496. }
  497. /// <summary>
  498. /// 采用WebSocket协议,发送WS数据。
  499. /// </summary>
  500. /// <param name="client"></param>
  501. /// <param name="dataFrame"></param>
  502. public static void SendWithWS(this HttpSocketClient client, WSDataFrame dataFrame)
  503. {
  504. using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
  505. {
  506. dataFrame.BuildResponse(byteBlock);
  507. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  508. }
  509. }
  510. #endregion 同步发送
  511. #region 异步发送
  512. /// <summary>
  513. /// 采用WebSocket协议,发送二进制流数据。
  514. /// </summary>
  515. /// <param name="client"></param>
  516. /// <param name="buffer"></param>
  517. /// <param name="offset"></param>
  518. /// <param name="length"></param>
  519. public static void SendWithWSAsync(this HttpSocketClient client, byte[] buffer, int offset, int length)
  520. {
  521. using (ByteBlock byteBlock = new ByteBlock(length + 1024))
  522. {
  523. WSDataFrame dataFrame = new WSDataFrame();
  524. dataFrame.AppendBinary(buffer, offset, length).BuildResponse(byteBlock);
  525. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  526. }
  527. }
  528. /// <summary>
  529. /// 采用WebSocket协议,发送二进制流数据。
  530. /// </summary>
  531. /// <param name="client"></param>
  532. /// <param name="byteBlock"></param>
  533. public static void SendWithWSAsync(this HttpSocketClient client, ByteBlock byteBlock)
  534. {
  535. SendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len);
  536. }
  537. /// <summary>
  538. /// 采用WebSocket协议,发送二进制流数据。
  539. /// </summary>
  540. /// <param name="client"></param>
  541. /// <param name="buffer"></param>
  542. public static void SendWithWSAsync(this HttpSocketClient client, byte[] buffer)
  543. {
  544. SendWithWSAsync(client, buffer, 0, buffer.Length);
  545. }
  546. /// <summary>
  547. /// 采用WebSocket协议,发送文本数据。
  548. /// </summary>
  549. /// <param name="client"></param>
  550. /// <param name="text"></param>
  551. public static void SendWithWSAsync(this HttpSocketClient client, string text)
  552. {
  553. using (ByteBlock byteBlock = new ByteBlock(text.Length + 1024))
  554. {
  555. WSDataFrame dataFrame = new WSDataFrame();
  556. dataFrame.AppendText(text).BuildResponse(byteBlock);
  557. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  558. }
  559. }
  560. /// <summary>
  561. /// 采用WebSocket协议,发送WS数据。
  562. /// </summary>
  563. /// <param name="client"></param>
  564. /// <param name="dataFrame"></param>
  565. public static void SendWithWSAsync(this HttpSocketClient client, WSDataFrame dataFrame)
  566. {
  567. using (ByteBlock byteBlock = new ByteBlock(dataFrame.PayloadLength + 1024))
  568. {
  569. dataFrame.BuildResponse(byteBlock);
  570. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  571. }
  572. }
  573. #endregion 异步发送
  574. #region 同步分包发送
  575. /// <summary>
  576. /// 分包发送。
  577. /// <para>
  578. /// 按packageSize的值,每次发送数据包。
  579. /// </para>
  580. /// </summary>
  581. /// <param name="client"></param>
  582. /// <param name="buffer"></param>
  583. /// <param name="offset"></param>
  584. /// <param name="length"></param>
  585. /// <param name="packageSize"></param>
  586. public static void SubSendWithWS(this HttpSocketClient client, byte[] buffer, int offset, int length, int packageSize)
  587. {
  588. lock (client)
  589. {
  590. if (packageSize >= length)
  591. {
  592. SendWithWS(client, buffer, offset, length);
  593. return;
  594. }
  595. int sentLen = 0;
  596. WSDataFrame dataFrame = new WSDataFrame();
  597. dataFrame.FIN = false;
  598. dataFrame.Opcode = WSDataType.Binary;
  599. int count;
  600. if (length % packageSize == 0)
  601. {
  602. count = length / packageSize;
  603. }
  604. else
  605. {
  606. count = length / packageSize + 1;
  607. }
  608. for (int i = 0; i < count; i++)
  609. {
  610. if (i > 0)
  611. {
  612. dataFrame.Opcode = WSDataType.Cont;
  613. }
  614. if (i == count - 1)//最后
  615. {
  616. dataFrame.FIN = true;
  617. }
  618. using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
  619. {
  620. int thisLen = Math.Min(packageSize, length - sentLen);
  621. WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
  622. client.DefaultSend(byteBlock.Buffer, 0, byteBlock.Len);
  623. sentLen += thisLen;
  624. offset += thisLen;
  625. }
  626. }
  627. }
  628. }
  629. /// <summary>
  630. /// 分包发送。
  631. /// <para>
  632. /// 按packageSize的值,每次发送数据包。
  633. /// </para>
  634. /// </summary>
  635. /// <param name="client"></param>
  636. /// <param name="buffer"></param>
  637. /// <param name="packageSize"></param>
  638. public static void SubSendWithWS(this HttpSocketClient client, byte[] buffer, int packageSize)
  639. {
  640. SubSendWithWS(client, buffer, 0, buffer.Length, packageSize);
  641. }
  642. /// <summary>
  643. /// 分包发送。
  644. /// <para>
  645. /// 按packageSize的值,每次发送数据包。
  646. /// </para>
  647. /// </summary>
  648. /// <param name="client"></param>
  649. /// <param name="byteBlock"></param>
  650. /// <param name="packageSize"></param>
  651. public static void SubSendWithWS(this HttpSocketClient client, ByteBlock byteBlock, int packageSize)
  652. {
  653. SubSendWithWS(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
  654. }
  655. #endregion 同步分包发送
  656. #region 异步分包发送
  657. /// <summary>
  658. /// 分包发送。
  659. /// <para>
  660. /// 按packageSize的值,每次发送数据包。
  661. /// </para>
  662. /// </summary>
  663. /// <param name="client"></param>
  664. /// <param name="buffer"></param>
  665. /// <param name="offset"></param>
  666. /// <param name="length"></param>
  667. /// <param name="packageSize"></param>
  668. public static void SubSendWithWSAsync(this HttpSocketClient client, byte[] buffer, int offset, int length, int packageSize)
  669. {
  670. lock (client)
  671. {
  672. if (packageSize >= length)
  673. {
  674. SendWithWSAsync(client, buffer, offset, length);
  675. return;
  676. }
  677. int sentLen = 0;
  678. WSDataFrame dataFrame = new WSDataFrame();
  679. dataFrame.FIN = false;
  680. dataFrame.Opcode = WSDataType.Binary;
  681. int count;
  682. if (length % packageSize == 0)
  683. {
  684. count = length / packageSize;
  685. }
  686. else
  687. {
  688. count = length / packageSize + 1;
  689. }
  690. for (int i = 0; i < count; i++)
  691. {
  692. if (i > 0)
  693. {
  694. dataFrame.Opcode = WSDataType.Cont;
  695. }
  696. if (i == count - 1)//最后
  697. {
  698. dataFrame.FIN = true;
  699. }
  700. using (ByteBlock byteBlock = new ByteBlock(packageSize + 1024))
  701. {
  702. int thisLen = Math.Min(packageSize, length - sentLen);
  703. WSTools.Build(byteBlock, dataFrame, buffer, offset, thisLen);
  704. client.DefaultSendAsync(byteBlock.Buffer, 0, byteBlock.Len);
  705. sentLen += thisLen;
  706. offset += thisLen;
  707. }
  708. }
  709. }
  710. }
  711. /// <summary>
  712. /// 分包发送。
  713. /// <para>
  714. /// 按packageSize的值,每次发送数据包。
  715. /// </para>
  716. /// </summary>
  717. /// <param name="client"></param>
  718. /// <param name="buffer"></param>
  719. /// <param name="packageSize"></param>
  720. public static void SubSendWithWSAsync(this HttpSocketClient client, byte[] buffer, int packageSize)
  721. {
  722. SubSendWithWSAsync(client, buffer, 0, buffer.Length, packageSize);
  723. }
  724. /// <summary>
  725. /// 分包发送。
  726. /// <para>
  727. /// 按packageSize的值,每次发送数据包。
  728. /// </para>
  729. /// </summary>
  730. /// <param name="client"></param>
  731. /// <param name="byteBlock"></param>
  732. /// <param name="packageSize"></param>
  733. public static void SubSendWithWSAsync(this HttpSocketClient client, ByteBlock byteBlock, int packageSize)
  734. {
  735. SubSendWithWSAsync(client, byteBlock.Buffer, 0, byteBlock.Len, packageSize);
  736. }
  737. #endregion 异步分包发送
  738. #endregion 服务器
  739. }
  740. }