Skip to content

Commit 7b34976

Browse files
committed
implemented auth handshake for socket.io server v3
1 parent d5c8265 commit 7b34976

File tree

8 files changed

+82
-12
lines changed

8 files changed

+82
-12
lines changed

src/SocketIOClient.UnitTest/MessageTests/MessageWriteTest.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,36 @@ public void Eio3PollingWith2ParamConnected()
144144
Assert.AreEqual("40/razer?a=123&token=qwer,", text);
145145
}
146146

147+
[TestMethod]
148+
public void Eio4_Auth_Query_Namespace_Should_Include_Auth_Namespace(){
149+
var msg = new ConnectedMessage
150+
{
151+
Eio = 4,
152+
Protocol = TransportProtocol.Polling,
153+
Namespace = "/razer",
154+
Query = new Dictionary<string, string>
155+
{
156+
{ "a", "123" },
157+
{ "token", "qwer" }
158+
},
159+
AuthJsonStr = "{\"test\":\"vvs\"}"
160+
};
161+
string text = msg.Write();
162+
Assert.AreEqual("40/razer,{\"test\":\"vvs\"}", text);
163+
}
164+
165+
[TestMethod]
166+
public void Eio4_Auth_Should_Include_Auth(){
167+
var msg = new ConnectedMessage
168+
{
169+
Eio = 4,
170+
Protocol = TransportProtocol.Polling,
171+
AuthJsonStr = "{\"test\":\"vvs\"}"
172+
};
173+
string text = msg.Write();
174+
Assert.AreEqual("40{\"test\":\"vvs\"}", text);
175+
}
176+
147177
[TestMethod]
148178
public void Disconnected()
149179
{

src/SocketIOClient.UnitTest/RouterTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using SocketIOClient.Transport;
1010
using RichardSzalay.MockHttp;
11+
using SocketIOClient.JsonSerializer;
1112

1213
namespace SocketIOClient.UnitTest
1314
{
@@ -40,5 +41,22 @@ public async Task V2_Server_Support_Polling_And_WebSocket_ReturnsWebSocket()
4041
TransportProtocol protocol = await Router.GetProtocolAsync(httpClient, new Uri(uri));
4142
Assert.AreEqual(TransportProtocol.WebSocket, protocol);
4243
}
44+
45+
[TestMethod]
46+
public void AuthObject_ReturnsJson()
47+
{
48+
var options = new SocketIOOptions
49+
{
50+
Auth = new
51+
{
52+
user = "admin",
53+
pwd = "123"
54+
}
55+
};
56+
var serializer = new SystemTextJsonSerializer();
57+
var router = new WebSocketRouter(null, null, options, serializer);
58+
string actual = router.GetAuthJson();
59+
Assert.AreEqual("{\"user\":\"admin\",\"pwd\":\"123\"}", actual);
60+
}
4361
}
4462
}

src/SocketIOClient/Messages/ConnectedMessage.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ConnectedMessage : IMessage
2525
public TransportProtocol Protocol { get; set; }
2626

2727
public IEnumerable<KeyValuePair<string, string>> Query { get; set; }
28+
public string AuthJsonStr { get; set; }
2829

2930
public void Read(string msg)
3031
{
@@ -69,6 +70,7 @@ public string Eio4Write()
6970
{
7071
builder.Append(Namespace).Append(',');
7172
}
73+
builder.Append(AuthJsonStr);
7274
return builder.ToString();
7375
}
7476

src/SocketIOClient/Routers/HttpRouter.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
using SocketIOClient.Messages;
1+
using SocketIOClient.JsonSerializer;
2+
using SocketIOClient.Messages;
23
using SocketIOClient.Transport;
3-
using SocketIOClient.UriConverters;
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.Net.Http;
8-
using System.Text;
98
using System.Threading;
109
using System.Threading.Tasks;
1110

1211
namespace SocketIOClient.Routers
1312
{
1413
public class HttpRouter : Router
1514
{
16-
public HttpRouter(HttpClient httpClient, Func<IClientWebSocket> clientWebSocketProvider, SocketIOOptions options) : base(httpClient, clientWebSocketProvider, options)
15+
public HttpRouter(HttpClient httpClient,
16+
Func<IClientWebSocket> clientWebSocketProvider,
17+
SocketIOOptions options,
18+
IJsonSerializer jsonSerializer) : base(httpClient, clientWebSocketProvider, options, jsonSerializer)
1719
{
1820
}
1921

src/SocketIOClient/Routers/Router.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SocketIOClient.Messages;
1+
using SocketIOClient.JsonSerializer;
2+
using SocketIOClient.Messages;
23
using SocketIOClient.Transport;
34
using SocketIOClient.UriConverters;
45
using System;
@@ -12,20 +13,25 @@ namespace SocketIOClient.Routers
1213
{
1314
public abstract class Router : IDisposable
1415
{
15-
public Router(HttpClient httpClient, Func<IClientWebSocket> clientWebSocketProvider, SocketIOOptions options)
16+
public Router(
17+
HttpClient httpClient,
18+
Func<IClientWebSocket> clientWebSocketProvider,
19+
SocketIOOptions options,
20+
IJsonSerializer jsonSerializer)
1621
{
1722
HttpClient = httpClient;
1823
ClientWebSocketProvider = clientWebSocketProvider;
1924
UriConverter = new UriConverter();
2025
_messageQueue = new Queue<IMessage>();
2126
Options = options;
27+
JsonSerializer = jsonSerializer;
2228
}
2329

2430
protected HttpClient HttpClient { get; }
2531
readonly Queue<IMessage> _messageQueue;
2632
protected Func<IClientWebSocket> ClientWebSocketProvider { get; }
2733
protected SocketIOOptions Options { get; }
28-
34+
protected IJsonSerializer JsonSerializer { get; }
2935
protected OpenedMessage OpenedMessage { get; set; }
3036

3137
CancellationTokenSource _pingTokenSource;
@@ -51,14 +57,20 @@ public virtual Task ConnectAsync()
5157
return Task.CompletedTask;
5258
}
5359

60+
public string GetAuthJson()
61+
{
62+
return JsonSerializer.Serialize(new[] { Options.Auth }).Json.TrimStart('[').TrimEnd(']');
63+
}
64+
5465
protected virtual async Task OpenAsync(OpenedMessage msg)
5566
{
5667
OpenedMessage = msg;
5768
var connectMsg = new ConnectedMessage
5869
{
5970
Namespace = Namespace,
6071
Eio = EIO,
61-
Query = Options.Query
72+
Query = Options.Query,
73+
AuthJsonStr = GetAuthJson()
6274
};
6375

6476
for (int i = 1; i <= 3; i++)

src/SocketIOClient/Routers/WebSocketRouter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using SocketIOClient.Messages;
1+
using SocketIOClient.JsonSerializer;
2+
using SocketIOClient.Messages;
23
using SocketIOClient.Transport;
34
using SocketIOClient.UriConverters;
45
using System;
@@ -14,7 +15,10 @@ namespace SocketIOClient.Routers
1415
{
1516
public class WebSocketRouter : Router
1617
{
17-
public WebSocketRouter(HttpClient httpClient, Func<IClientWebSocket> clientWebSocketProvider, SocketIOOptions options) : base(httpClient, clientWebSocketProvider, options)
18+
public WebSocketRouter(HttpClient httpClient,
19+
Func<IClientWebSocket> clientWebSocketProvider,
20+
SocketIOOptions options,
21+
IJsonSerializer jsonSerializer) : base(httpClient, clientWebSocketProvider, options, jsonSerializer)
1822
{
1923
}
2024

src/SocketIOClient/SocketIO.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ private async Task CreateRouterAsync()
175175
}
176176
if (Options.Transport == TransportProtocol.Polling)
177177
{
178-
Router = new HttpRouter(HttpClient, ClientWebSocketProvider, Options);
178+
Router = new HttpRouter(HttpClient, ClientWebSocketProvider, Options, JsonSerializer);
179179
}
180180
else
181181
{
182-
Router = new WebSocketRouter(HttpClient, ClientWebSocketProvider, Options);
182+
Router = new WebSocketRouter(HttpClient, ClientWebSocketProvider, Options, JsonSerializer);
183183
}
184184
Router.Namespace = Namespace;
185185
Router.ServerUri = ServerUri;

src/SocketIOClient/SocketIOOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,7 @@ public double RandomizationFactor
5959
public int EIO { get; set; }
6060

6161
public bool AutoUpgrade { get; set; }
62+
63+
public object Auth { get; set; }
6264
}
6365
}

0 commit comments

Comments
 (0)