Skip to content

Commit 7b755de

Browse files
committed
fix integration tests
1 parent b2361ed commit 7b755de

File tree

2 files changed

+44
-147
lines changed

2 files changed

+44
-147
lines changed

src/SocketIOClient.Test/SocketIOTests/EmitTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ public virtual async Task EmitWith2ParamsTrueTrue()
456456
await client.EmitAsync("2 params", true, true);
457457
};
458458
await client.ConnectAsync();
459-
await Task.Delay(200);
459+
await Task.Delay(1000);
460460
await client.DisconnectAsync();
461461

462462
Assert.AreEqual(JsonValueKind.True, result0);
@@ -688,7 +688,7 @@ await client.EmitAsync("1 params | cb: 1 params", response =>
688688
}, "str1");
689689
};
690690
await client.ConnectAsync();
691-
await Task.Delay(200);
691+
await Task.Delay(800);
692692
await client.DisconnectAsync();
693693

694694
Assert.AreEqual("str1", result);

src/SocketIOClient/Transport/TransportRouter.cs

Lines changed: 42 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Diagnostics;
6-
using System.Net;
76
using System.Net.Http;
87
using System.Net.WebSockets;
98
using System.Threading;
@@ -31,12 +30,9 @@ public TransportRouter(HttpClient httpClient, Func<IClientWebSocket> clientWebSo
3130
HttpTransport _httpTransport;
3231
WebSocketTransport _webSocketTransport;
3332
CancellationTokenSource _pollingTokenSource;
34-
CancellationToken _pollingToken;
3533
string _httpUri;
36-
//int _pingInterval;
3734
OpenedMessage _openedMessage;
3835
CancellationTokenSource _pingTokenSource;
39-
CancellationToken _pingToken;
4036
DateTime _pingTime;
4137

4238
public Uri ServerUri { get; set; }
@@ -65,60 +61,6 @@ public async Task ConnectAsync()
6561
{
6662
await ConnectByPollingAsync().ConfigureAwait(false);
6763
}
68-
69-
// if (_webSocketTransport != null)
70-
// {
71-
// _webSocketTransport.Dispose();
72-
// }
73-
//Handshake:
74-
// Uri uri = UriConverter.GetHandshakeUri(ServerUri, EIO, _options.Path, _options.Query);
75-
76-
// var req = new HttpRequestMessage(HttpMethod.Get, uri);
77-
// SetHeaders(req);
78-
79-
// var resMsg = await _httpClient.SendAsync(req, new CancellationTokenSource(_options.ConnectionTimeout).Token).ConfigureAwait(false);
80-
// if (!resMsg.IsSuccessStatusCode)
81-
// {
82-
// if (resMsg.StatusCode == HttpStatusCode.NotFound)
83-
// {
84-
// string errMsg = await resMsg.Content.ReadAsStringAsync().ConfigureAwait(false);
85-
// if (errMsg.Contains("Transport unknown"))
86-
// {
87-
88-
// }
89-
// }
90-
// throw new HttpRequestException($"Response status code does not indicate success: {resMsg.StatusCode}");
91-
// }
92-
// string text = await resMsg.Content.ReadAsStringAsync().ConfigureAwait(false);
93-
// var openedMessage = MessageFactory.CreateOpenedMessage(text);
94-
95-
// if (openedMessage.EIO == 3 && EIO == 4)
96-
// {
97-
// EIO = 3;
98-
// goto Handshake;
99-
// }
100-
101-
// Sid = openedMessage.Sid;
102-
// EIO = openedMessage.EIO;
103-
// uri = UriConverter.GetHandshakeUri(ServerUri, EIO, _options.Path, _options.Query);
104-
// _pingInterval = openedMessage.PingInterval;
105-
// if (openedMessage.Upgrades.Contains("websocket") && _options.AutoUpgrade)
106-
// {
107-
// _clientWebSocket = _clientWebSocketProvider();
108-
// _webSocketTransport = new WebSocketTransport(_clientWebSocket, EIO)
109-
// {
110-
// ConnectionTimeout = _options.ConnectionTimeout
111-
// };
112-
// await WebSocketConnectAsync().ConfigureAwait(false);
113-
// Protocol = TransportProtocol.WebSocket;
114-
// }
115-
// else
116-
// {
117-
// _httpUri = uri + "&sid=" + Sid;
118-
// _httpTransport = new HttpTransport(_httpClient, EIO);
119-
// await HttpConnectAsync().ConfigureAwait(false);
120-
// Protocol = TransportProtocol.Polling;
121-
// }
12264
}
12365

12466
private async Task ConnectByWebsocketAsync()
@@ -140,10 +82,12 @@ private async Task ConnectByWebsocketAsync()
14082
{
14183
ConnectionTimeout = _options.ConnectionTimeout
14284
};
143-
await _webSocketTransport.ConnectAsync(uri).ConfigureAwait(false);
14485
_webSocketTransport.OnTextReceived = OnTextReceived;
14586
_webSocketTransport.OnBinaryReceived = OnBinaryReceived;
14687
_webSocketTransport.OnAborted = OnAborted;
88+
Debug.WriteLine($"[Websocket] Connecting");
89+
await _webSocketTransport.ConnectAsync(uri).ConfigureAwait(false);
90+
Debug.WriteLine($"[Websocket] Connected");
14791
}
14892

14993
private async Task ConnectByPollingAsync()
@@ -163,56 +107,25 @@ private async Task ConnectByPollingAsync()
163107
OnBinaryReceived = OnBinaryReceived
164108
};
165109
await _httpTransport.SendAsync(req, new CancellationTokenSource(_options.ConnectionTimeout).Token).ConfigureAwait(false);
166-
//_openedMessage = MessageFactory.CreateOpenedMessage(text);
167-
//_httpUri = uri + "&sid=" + _openedMessage.Sid;
168-
await HttpConnectAsync().ConfigureAwait(false);
169-
}
170-
171-
//private async Task WebSocketConnectAsync()
172-
//{
173-
// Uri uri = UriConverter.GetWebSocketUri(ServerUri, EIO, _options.Path, _options.Query, Sid);
174-
// await _webSocketTransport.ConnectAsync(uri).ConfigureAwait(false);
175-
// _webSocketTransport.OnTextReceived = OnWebSocketTextReceived;
176-
// _webSocketTransport.OnBinaryReceived = OnBinaryReceived;
177-
// _webSocketTransport.OnAborted = OnAborted;
178-
// await _webSocketTransport.SendAsync("2probe", CancellationToken.None);
179-
//}
180-
181-
private async Task HttpConnectAsync()
182-
{
110+
if (_pollingTokenSource != null)
111+
{
112+
_pollingTokenSource.Cancel();
113+
}
183114
_pollingTokenSource = new CancellationTokenSource();
184-
_pollingToken = _pollingTokenSource.Token;
185115

186-
StartPolling();
187-
188-
//if (!(EIO == 3 && string.IsNullOrEmpty(Namespace)))
189-
//{
190-
// var msg = new ConnectedMessage
191-
// {
192-
// Namespace = Namespace,
193-
// Eio = EIO,
194-
// Protocol = TransportProtocol.Polling,
195-
// Query = _options.Query
196-
// };
197-
// await SendAsync(msg.Write(), CancellationToken.None).ConfigureAwait(false);
198-
//}
116+
StartPolling(_pollingTokenSource.Token);
199117
}
200118

201-
private void StartPolling()
119+
private void StartPolling(CancellationToken cancellationToken)
202120
{
203121
Task.Factory.StartNew(async () =>
204122
{
205-
while (!_pollingToken.IsCancellationRequested)
123+
while (!cancellationToken.IsCancellationRequested)
206124
{
207125
try
208126
{
209127
await _httpTransport.GetAsync(_httpUri, CancellationToken.None).ConfigureAwait(false);
210128
}
211-
catch (TaskCanceledException e)
212-
{
213-
Debug.WriteLine(e);
214-
break;
215-
}
216129
catch
217130
{
218131
OnTransportClosed();
@@ -222,53 +135,37 @@ private void StartPolling()
222135
}, TaskCreationOptions.LongRunning);
223136
}
224137

225-
private async Task PingAsync()
138+
private void StartPing(CancellationToken cancellationToken)
226139
{
227-
Debug.WriteLine($"PingInterval: {_openedMessage.PingInterval}");
228-
while (!_pingToken.IsCancellationRequested)
140+
Debug.WriteLine($"[Ping] Interval: {_openedMessage.PingInterval}");
141+
Task.Factory.StartNew(async () =>
229142
{
230-
await Task.Delay(_openedMessage.PingInterval);
231-
try
232-
{
233-
var ping = new PingMessage();
234-
Debug.WriteLine($"Send Ping");
235-
await SendAsync(ping, CancellationToken.None).ConfigureAwait(false);
236-
_pingTime = DateTime.Now;
237-
OnMessageReceived(ping);
238-
}
239-
catch
143+
while (!cancellationToken.IsCancellationRequested)
240144
{
241-
OnTransportClosed();
242-
throw;
145+
await Task.Delay(_openedMessage.PingInterval);
146+
if (cancellationToken.IsCancellationRequested)
147+
{
148+
break;
149+
}
150+
try
151+
{
152+
var ping = new PingMessage();
153+
Debug.WriteLine($"[Ping] Sending");
154+
await SendAsync(ping, CancellationToken.None).ConfigureAwait(false);
155+
Debug.WriteLine($"[Ping] Has been sent");
156+
_pingTime = DateTime.Now;
157+
OnMessageReceived(ping);
158+
}
159+
catch (Exception e)
160+
{
161+
Debug.WriteLine($"[Ping] Failed to send, {e.Message}");
162+
OnTransportClosed();
163+
throw;
164+
}
243165
}
244-
}
166+
}, TaskCreationOptions.LongRunning);
245167
}
246168

247-
//private async void OnWebSocketTextReceived(string text)
248-
//{
249-
// if (text == "3probe")
250-
// {
251-
// await _webSocketTransport.SendAsync("5", CancellationToken.None);
252-
253-
// if (EIO == 3 && string.IsNullOrEmpty(Namespace))
254-
// {
255-
// return;
256-
// }
257-
// var msg = new ConnectedMessage
258-
// {
259-
// Namespace = Namespace,
260-
// Eio = EIO,
261-
// Sid = Sid,
262-
// Protocol = TransportProtocol.WebSocket,
263-
// Query = _options.Query
264-
// };
265-
// await _webSocketTransport.SendAsync(msg.Write(), CancellationToken.None);
266-
// }
267-
// else
268-
// {
269-
// OnTextReceived(text);
270-
// }
271-
//}
272169
private async Task OnOpened(OpenedMessage msg)
273170
{
274171
_openedMessage = msg;
@@ -314,10 +211,9 @@ private async void OnTextReceived(string text)
314211
if (_pingTokenSource != null)
315212
{
316213
_pingTokenSource.Cancel();
317-
_pingTokenSource = new CancellationTokenSource();
318-
_pingToken = _pingTokenSource.Token;
319214
}
320-
_ = Task.Factory.StartNew(PingAsync, TaskCreationOptions.LongRunning);
215+
_pingTokenSource = new CancellationTokenSource();
216+
StartPing(_pingTokenSource.Token);
321217
}
322218
else
323219
{
@@ -376,6 +272,7 @@ private void OnBinaryReceived(byte[] bytes)
376272

377273
private void OnAborted(Exception e)
378274
{
275+
Debug.WriteLine($"[Websocket] Aborted, " + e.Message);
379276
OnTransportClosed();
380277
}
381278

@@ -409,6 +306,10 @@ public async Task DisconnectAsync()
409306
}
410307
_clientWebSocket.Dispose();
411308
}
309+
if (_pingTokenSource != null)
310+
{
311+
_pingTokenSource.Cancel();
312+
}
412313
}
413314

414315
private async Task SendAsync(string text, CancellationToken cancellationToken)
@@ -450,10 +351,6 @@ public void Dispose()
450351
{
451352
_webSocketTransport.Dispose();
452353
}
453-
if (_pingTokenSource != null)
454-
{
455-
_pingTokenSource.Cancel();
456-
}
457354
}
458355
}
459356
}

0 commit comments

Comments
 (0)