Skip to content

Commit 9eb7874

Browse files
gedemgedem
authored andcommitted
temp branch tests, connectionId, improvements
1 parent 46ce194 commit 9eb7874

File tree

18 files changed

+848
-38
lines changed

18 files changed

+848
-38
lines changed

src/NetCoreStack.WebSockets/ConnectionManager.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,8 @@ await transport.WebSocket.SendAsync(segments,
110110
token);
111111
}
112112

113-
public async Task ConnectAsync(WebSocket webSocket, string connectorName = "")
113+
public async Task ConnectAsync(WebSocket webSocket, string connectionId, string connectorName = "")
114114
{
115-
WebSocketTransport transport = new WebSocketTransport(webSocket, connectorName);
116-
var connectionId = transport.ConnectionId;
117-
var context = new WebSocketMessageContext();
118-
context.Command = WebSocketCommands.Handshake;
119-
context.Value = connectionId;
120-
context.Header = await _initState.GetStateAsync();
121-
Connections.TryAdd(connectionId, transport);
122-
123-
await SendAsync(connectionId, context);
124-
125115
var receiverContext = new WebSocketReceiverContext
126116
{
127117
Compressor = _compressor,
@@ -131,6 +121,24 @@ public async Task ConnectAsync(WebSocket webSocket, string connectorName = "")
131121
Options = _options,
132122
WebSocket = webSocket
133123
};
124+
125+
WebSocketTransport transport = null;
126+
if (Connections.TryGetValue(connectionId, out transport))
127+
{
128+
transport.ReConnect(webSocket);
129+
}
130+
else
131+
{
132+
transport = new WebSocketTransport(webSocket, connectionId, connectorName);
133+
var context = new WebSocketMessageContext();
134+
context.Command = WebSocketCommands.Handshake;
135+
context.Value = connectionId;
136+
context.Header = await _initState.GetStateAsync();
137+
Connections.TryAdd(connectionId, transport);
138+
139+
await SendAsync(connectionId, context);
140+
}
141+
134142
var receiver = new WebSocketReceiver(receiverContext, CloseConnection);
135143
await receiver.ReceiveAsync();
136144
}
@@ -267,18 +275,34 @@ await transport.WebSocket.SendAsync(segments,
267275
}
268276
}
269277

270-
public void CloseConnection(string connectionId)
278+
public void CloseConnection(string connectionId, bool keepAlive)
271279
{
272280
WebSocketTransport transport = null;
273-
if (Connections.TryRemove(connectionId, out transport))
281+
if (keepAlive)
274282
{
275-
transport.Dispose();
283+
if (Connections.TryGetValue(connectionId, out transport))
284+
{
285+
transport.Dispose();
286+
}
287+
}
288+
else
289+
{
290+
if (Connections.TryRemove(connectionId, out transport))
291+
{
292+
transport.Dispose();
293+
}
276294
}
277295
}
278296

279297
public void CloseConnection(WebSocketReceiverContext context)
280298
{
281-
CloseConnection(context.ConnectionId);
299+
bool keepAlive = false;
300+
if (context.WebSocket.CloseStatus == WebSocketCloseStatus.EndpointUnavailable)
301+
{
302+
keepAlive = true;
303+
}
304+
305+
CloseConnection(context.ConnectionId, keepAlive);
282306
}
283307
}
284308
}

src/NetCoreStack.WebSockets/Interfaces/IConnectionManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public interface IConnectionManager
1010
{
1111
ConcurrentDictionary<string, WebSocketTransport> Connections { get; }
1212

13-
Task ConnectAsync(WebSocket webSocket, string connectorName = "");
13+
Task ConnectAsync(WebSocket webSocket, string connectionId, string connectorName = "");
1414

1515
/// <summary>
1616
/// Text message broadcaster
@@ -49,6 +49,6 @@ public interface IConnectionManager
4949
/// Close the specified connection
5050
/// </summary>
5151
/// <param name="connectionId"></param>
52-
void CloseConnection(string connectionId);
52+
void CloseConnection(string connectionId, bool keepAlive);
5353
}
5454
}

src/NetCoreStack.WebSockets/Internal/WebSocketMiddleware.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Microsoft.Extensions.Primitives;
55
using NetCoreStack.WebSockets.Interfaces;
66
using System;
7-
using System.Net.WebSockets;
87
using System.Threading.Tasks;
98

109
namespace NetCoreStack.WebSockets.Internal
@@ -27,22 +26,44 @@ public async Task Invoke(HttpContext httpContext,
2726
{
2827
if (httpContext.WebSockets.IsWebSocketRequest)
2928
{
29+
string connectionId = string.Empty;
3030
string connectorName = string.Empty;
3131
StringValues headerValue = "";
3232
if (httpContext.Request.Headers.TryGetValue(SocketsConstants.ConnectorName, out headerValue))
3333
{
3434
connectorName = headerValue.ToString();
3535
}
36+
if (httpContext.Request.Headers.TryGetValue(SocketsConstants.ConnectionId, out headerValue))
37+
{
38+
connectionId = headerValue.ToString();
39+
}
40+
3641
if (string.IsNullOrEmpty(connectorName))
3742
{
3843
if (httpContext.Request.Query.ContainsKey(SocketsConstants.ConnectorName))
3944
{
4045
connectorName = httpContext.Request.Query[SocketsConstants.ConnectorName];
4146
}
4247
}
48+
if (string.IsNullOrEmpty(connectionId))
49+
{
50+
if (httpContext.Request.Query.ContainsKey(SocketsConstants.ConnectionId))
51+
{
52+
connectionId = httpContext.Request.Query[SocketsConstants.ConnectionId];
53+
Guid connectionIdGuid = Guid.Empty;
54+
if (!Guid.TryParse(connectionId, out connectionIdGuid))
55+
{
56+
connectionId = string.Empty;
57+
}
58+
}
59+
}
4360

4461
var webSocket = await httpContext.WebSockets.AcceptWebSocketAsync();
45-
await manager.ConnectAsync(webSocket, connectorName);
62+
if (string.IsNullOrEmpty(connectionId))
63+
{
64+
connectionId = Guid.NewGuid().ToString("N");
65+
}
66+
await manager.ConnectAsync(webSocket, connectionId: connectionId, connectorName: connectorName);
4667
}
4768
else
4869
{

src/NetCoreStack.WebSockets/Internal/WebSocketTransport.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ namespace NetCoreStack.WebSockets.Internal
55
{
66
public class WebSocketTransport : IDisposable
77
{
8-
public WebSocket WebSocket { get; }
8+
public WebSocket WebSocket { get; private set; }
99
public string ConnectionId { get; }
1010
public string ConnectorName { get; }
1111

12-
public WebSocketTransport(WebSocket webSocket, string connectorName)
12+
public WebSocketTransport(WebSocket webSocket, string connectionId, string connectorName)
1313
{
14-
ConnectionId = Guid.NewGuid().ToString();
14+
ConnectionId = connectionId;
1515
WebSocket = webSocket;
1616
ConnectorName = connectorName;
1717
}
1818

19+
public void ReConnect(WebSocket webSocket)
20+
{
21+
WebSocket = webSocket;
22+
}
23+
1924
public void Dispose()
2025
{
2126
WebSocket.Dispose();

test/WebClientTestApp/Controllers/HomeController.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Mvc;
62

73
namespace WebClientTestApp.Controllers
84
{
@@ -13,6 +9,11 @@ public IActionResult Index()
139
return View();
1410
}
1511

12+
public IActionResult ReConnectTest()
13+
{
14+
return View();
15+
}
16+
1617
public IActionResult About()
1718
{
1819
ViewData["Message"] = "Your application description page.";

test/WebClientTestApp/Views/Home/Index.cshtml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686
$(function () {
8787
'use strict';
8888
89-
var connectorName = "@Constants.ConnectorName"
89+
var connectionId = Cookies.get("connectionId");
90+
var connectorName = "@Constants.ConnectorName";
9091
var containerId = "connectionContainer";
9192
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
9293
var port = document.location.port ? (":" + document.location.port) : "";
@@ -110,7 +111,7 @@
110111
this.connectionId = ko.observable();
111112
this.data = ko.observable();
112113
this.dataRows = ko.observableArray([]);
113-
this.connectionUrl = ko.observable(scheme + "://" + document.location.hostname + port + "?connectorName=" + connectorName);
114+
this.connectionUrl = ko.observable(scheme + "://" + document.location.hostname + port + "?connectionId=" + connectionId + "&connectorName=" + connectorName);
114115
this.logInfo = ko.observable("Waiting for connection. To connect click the Connect button");
115116
this.connectionState = ko.observable(state.connect);
116117
this.connectionStatus = ko.pureComputed(function () {
@@ -162,6 +163,7 @@
162163
console.log(vm.connectionId());
163164
if (context && context.Command == WebSocketCommands.Handshake) {
164165
vm.connectionId(context.Value);
166+
Cookies.set('connectionId', context.Value);
165167
}
166168
167169
var row = {

0 commit comments

Comments
 (0)