Skip to content

Commit f8416df

Browse files
committed
auto upgrade transport protocol
1 parent a05c4e8 commit f8416df

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using SocketIOClient.UriConverters;
3+
using SocketIOClient.Routers;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using SocketIOClient.Transport;
10+
using RichardSzalay.MockHttp;
11+
12+
namespace SocketIOClient.UnitTest
13+
{
14+
[TestClass]
15+
public class RouterTest
16+
{
17+
[TestMethod]
18+
public async Task V2_Server_Only_Support_Polling_ReturnsPolling()
19+
{
20+
string uri = "ws://localhost:11002/socket.io/?token=V2&EIO=3&transport=polling";
21+
var mockHttp = new MockHttpMessageHandler();
22+
mockHttp
23+
.When(uri)
24+
.Respond("application/json", "85:0{\"sid\":\"LvT3j_n54ltd7NtZAAAA\",\"upgrades\":[],\"pingInterval\":10000,\"pingTimeout\":5000}");
25+
var httpClient = mockHttp.ToHttpClient();
26+
TransportProtocol protocol = await Router.GetProtocolAsync(httpClient, new Uri(uri));
27+
Assert.AreEqual(TransportProtocol.Polling, protocol);
28+
}
29+
30+
[TestMethod]
31+
public async Task V2_Server_Support_Polling_And_WebSocket_ReturnsWebSocket()
32+
{
33+
string uri = "ws://localhost:11002/socket.io/?token=V2&EIO=3&transport=polling";
34+
var mockHttp = new MockHttpMessageHandler();
35+
mockHttp
36+
.When(uri)
37+
.Respond("application/json", "85:0{\"sid\":\"LvT3j_n54ltd7NtZAAAA\",\"upgrades\":[\"websocket\"],\"pingInterval\":10000,\"pingTimeout\":5000}");
38+
var httpClient = mockHttp.ToHttpClient();
39+
40+
TransportProtocol protocol = await Router.GetProtocolAsync(httpClient, new Uri(uri));
41+
Assert.AreEqual(TransportProtocol.WebSocket, protocol);
42+
}
43+
}
44+
}

src/SocketIOClient/Routers/Router.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public Router(HttpClient httpClient, Func<IClientWebSocket> clientWebSocketProvi
2727
protected SocketIOOptions Options { get; }
2828

2929
protected OpenedMessage OpenedMessage { get; set; }
30+
3031
CancellationTokenSource _pingTokenSource;
3132
DateTime _pingTime;
3233

@@ -254,5 +255,15 @@ public virtual void Dispose()
254255
{
255256
_messageQueue.Clear();
256257
}
258+
259+
public static async Task<TransportProtocol> GetProtocolAsync(HttpClient httpClient, Uri uri)
260+
{
261+
string text = await httpClient.GetStringAsync(uri);
262+
if (text.Contains("websocket"))
263+
{
264+
return TransportProtocol.WebSocket;
265+
}
266+
return TransportProtocol.Polling;
267+
}
257268
}
258269
}

src/SocketIOClient/SocketIO.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using SocketIOClient.Messages;
1010
using SocketIOClient.Routers;
1111
using SocketIOClient.Transport;
12+
using SocketIOClient.UriConverters;
1213

1314
namespace SocketIOClient
1415
{
@@ -162,10 +163,16 @@ private void Initialize()
162163
};
163164
}
164165

165-
private void CreateRouterIfNull()
166+
private async Task CreateRouterAsync()
166167
{
167168
if (Router == null)
168169
{
170+
if (Options.AutoUpgrade && Options.Transport == TransportProtocol.Polling)
171+
{
172+
var uriConverter = new UriConverter();
173+
Uri uri = uriConverter.GetServerUri(false, ServerUri, Options.EIO, Options.Path, Options.Query);
174+
Options.Transport = await Routers.Router.GetProtocolAsync(HttpClient, uri);
175+
}
169176
if (Options.Transport == TransportProtocol.Polling)
170177
{
171178
Router = new HttpRouter(HttpClient, ClientWebSocketProvider, Options);
@@ -183,7 +190,7 @@ private void CreateRouterIfNull()
183190

184191
public async Task ConnectAsync()
185192
{
186-
CreateRouterIfNull();
193+
await CreateRouterAsync();
187194
_reconnectionDelay = Options.ReconnectionDelay;
188195
while (true)
189196
{

src/SocketIOClient/SocketIOOptions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ public SocketIOOptions()
1515
Path = "/socket.io";
1616
ConnectionTimeout = TimeSpan.FromSeconds(20);
1717
Reconnection = true;
18-
Transport = TransportProtocol.WebSocket;
18+
Transport = TransportProtocol.Polling;
1919
EIO = 4;
20+
AutoUpgrade = true;
2021
}
2122

2223
public string Path { get; set; }
@@ -56,5 +57,7 @@ public double RandomizationFactor
5657
public TransportProtocol Transport { get; set; }
5758

5859
public int EIO { get; set; }
60+
61+
public bool AutoUpgrade { get; set; }
5962
}
6063
}

0 commit comments

Comments
 (0)