|
4 | 4 | using System.Collections.Generic; |
5 | 5 | using System.IO.Pipelines; |
6 | 6 | using System.Net; |
| 7 | +using System.Net.Sockets; |
7 | 8 | using System.Security.Cryptography; |
8 | 9 | using System.Text; |
9 | 10 | using System.Threading; |
@@ -66,24 +67,18 @@ public Dictionary<string, string> Headers |
66 | 67 |
|
67 | 68 | public WebSocketState State { get; private set; } = WebSocketState.None; |
68 | 69 |
|
69 | | - public WebSocket(string url) |
70 | | - : this(url, NullLogger.Instance) |
| 70 | + static ConnectionOptions PrepareConnectionOptions(ConnectionOptions connectionOptions, ILogger logger) |
71 | 71 | { |
72 | | - |
73 | | - } |
74 | | - |
75 | | - public WebSocket(string url, ILogger logger) |
76 | | - : this(url, new ConnectionOptions { Logger = logger }) |
77 | | - { |
78 | | - |
| 72 | + connectionOptions ??= new ConnectionOptions(); |
| 73 | + connectionOptions.Logger = logger ?? NullLogger.Instance; |
| 74 | + return connectionOptions; |
79 | 75 | } |
80 | 76 |
|
81 | | - public WebSocket(string url, ConnectionOptions connectionOptions) |
82 | | - : base(new HandshakePipelineFilter(), connectionOptions) |
| 77 | + public WebSocket(string url, string origin = null, ILogger logger = null, ConnectionOptions connectionOptions = null) |
| 78 | + : base(new HandshakePipelineFilter(), PrepareConnectionOptions(connectionOptions, logger)) |
83 | 79 | { |
84 | 80 | Uri = new Uri(url); |
85 | | - |
86 | | - _origin = Uri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped); |
| 81 | + _origin = origin; |
87 | 82 |
|
88 | 83 | if ("ws".Equals(Uri.Scheme, StringComparison.OrdinalIgnoreCase)) |
89 | 84 | { |
@@ -113,7 +108,7 @@ private EndPoint ResolveUri(Uri uri, int defaultPort) |
113 | 108 | if (IPAddress.TryParse(uri.Host, out IPAddress ipAddress)) |
114 | 109 | remoteEndPoint = new IPEndPoint(ipAddress, port); |
115 | 110 | else |
116 | | - remoteEndPoint = new DnsEndPoint(uri.Host, port); |
| 111 | + remoteEndPoint = new DnsEndPoint(uri.Host, port, AddressFamily.InterNetwork); |
117 | 112 |
|
118 | 113 | return remoteEndPoint; |
119 | 114 | } |
@@ -201,7 +196,11 @@ private void WriteHandshakeRequest(PipeWriter writer, string secKey) |
201 | 196 | writer.Write($"{WebSocketConstant.ResponseUpgradeLine}", _asciiEncoding); |
202 | 197 | writer.Write($"{WebSocketConstant.ResponseConnectionLine}", _asciiEncoding); |
203 | 198 | writer.Write($"{WebSocketConstant.SecWebSocketKey}: {secKey}\r\n", _asciiEncoding); |
204 | | - writer.Write($"{WebSocketConstant.Origin}: {_origin}\r\n", _asciiEncoding); |
| 199 | + |
| 200 | + if (!string.IsNullOrEmpty(_origin)) |
| 201 | + { |
| 202 | + writer.Write($"{WebSocketConstant.Origin}: {_origin}\r\n", _asciiEncoding); |
| 203 | + } |
205 | 204 |
|
206 | 205 | var subProtocols = _subProtocols; |
207 | 206 |
|
|
0 commit comments