WebSocketServerExtensions.cs 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.Threading.Tasks;
  14. using TouchSocket.Core;
  15. using TouchSocket.Sockets;
  16. namespace TouchSocket.Http.WebSockets
  17. {
  18. /// <summary>
  19. /// WebSocketServerExtensions
  20. /// </summary>
  21. public static class WebSocketServerExtensions
  22. {
  23. /// <summary>
  24. /// 转化Protocol协议标识为<see cref="Protocol.WebSocket"/>
  25. /// </summary>
  26. /// <param name="client"></param>
  27. /// <param name="httpContext">Http上下文</param>
  28. public static bool SwitchProtocolToWebSocket<TClient>(this TClient client, HttpContext httpContext) where TClient : HttpSocketClient
  29. {
  30. if (client.Protocol == Protocol.WebSocket)
  31. {
  32. return true;
  33. }
  34. if (client.Protocol == Protocol.Http)
  35. {
  36. if (WSTools.TryGetResponse(httpContext.Request, httpContext.Response))
  37. {
  38. HttpContextEventArgs args = new HttpContextEventArgs(new HttpContext(httpContext.Request, httpContext.Response))
  39. {
  40. IsPermitOperation = true
  41. };
  42. client.PluginsManager.Raise<IWebSocketPlugin>(nameof(IWebSocketPlugin.OnHandshaking), client, args);
  43. if (args.Context.Response.Responsed)
  44. {
  45. return false;
  46. }
  47. if (args.IsPermitOperation)
  48. {
  49. client.SetDataHandlingAdapter(new WebSocketDataHandlingAdapter());
  50. client.Protocol = Protocol.WebSocket;
  51. client.SetValue(WebSocketServerPlugin.HandshakedProperty, true);//设置握手状态
  52. using (ByteBlock byteBlock = new ByteBlock())
  53. {
  54. args.Context.Response.Build(byteBlock);
  55. client.DefaultSend(byteBlock);
  56. }
  57. client.PluginsManager.Raise<IWebSocketPlugin>(nameof(IWebSocketPlugin.OnHandshaked), client, new HttpContextEventArgs(httpContext));
  58. return true;
  59. }
  60. else
  61. {
  62. args.Context.Response.SetStatus("403", "Forbidden");
  63. using (ByteBlock byteBlock = new ByteBlock())
  64. {
  65. args.Context.Response.Build(byteBlock);
  66. client.DefaultSend(byteBlock);
  67. }
  68. client.Close("主动拒绝WebSocket连接");
  69. }
  70. }
  71. else
  72. {
  73. client.Close("WebSocket连接协议不正确");
  74. }
  75. }
  76. return false;
  77. }
  78. /// <summary>
  79. /// 转化Protocol协议标识为<see cref="Protocol.WebSocket"/>
  80. /// </summary>
  81. /// <param name="client"></param>
  82. /// <param name="httpContext">Http上下文</param>
  83. public static Task<bool> SwitchProtocolToWebSocketAsync<TClient>(this TClient client, HttpContext httpContext) where TClient : HttpSocketClient
  84. {
  85. return EasyTask.Run(() =>
  86. {
  87. return SwitchProtocolToWebSocket(client, httpContext);
  88. });
  89. }
  90. }
  91. }