Skip to content

Commit 3da6c00

Browse files
gedemgedem
authored andcommitted
ConnectorName and ConnectionId Browsers support
1 parent dd5efcb commit 3da6c00

File tree

20 files changed

+399
-37
lines changed

20 files changed

+399
-37
lines changed

src/NetCoreStack.WebSockets/ConnectionManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public ConnectionManager(IStreamCompressor compressor,
3737
_initState = initState;
3838
_loggerFactory = loggerFactory;
3939
_compressor = compressor;
40-
Connections = new ConcurrentDictionary<string, WebSocketTransport>();
40+
Connections = new ConcurrentDictionary<string, WebSocketTransport>(StringComparer.OrdinalIgnoreCase);
4141
}
4242

4343
private async Task<byte[]> PrepareBytesAsync(byte[] body, IDictionary<string, object> properties)

src/NetCoreStack.WebSockets/Extensions/WebSocketExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System;
55
using System.Collections.Generic;
66
using System.IO;
7-
using System.Linq;
87
using System.Net.WebSockets;
98
using System.Text;
109
using System.Threading.Tasks;
@@ -32,7 +31,7 @@ public static WebSocketMessageContext ToContext(this WebSocketReceiveResult resu
3231
webSocketContext.Value = content;
3332
webSocketContext.MessageType = result.MessageType;
3433
}
35-
34+
3635
webSocketContext.Length = result.Count;
3736
return webSocketContext;
3837
}

src/NetCoreStack.WebSockets/Internal/WebSocketMiddleware.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ public async Task Invoke(HttpContext httpContext,
3333
{
3434
connectorName = headerValue.ToString();
3535
}
36+
if (string.IsNullOrEmpty(connectorName))
37+
{
38+
if (httpContext.Request.Query.ContainsKey(SocketsConstants.ConnectorName))
39+
{
40+
connectorName = httpContext.Request.Query[SocketsConstants.ConnectorName];
41+
}
42+
}
3643

3744
var webSocket = await httpContext.WebSockets.AcceptWebSocketAsync();
3845
await manager.ConnectAsync(webSocket, connectorName);

src/NetCoreStack.WebSockets/WebSocketMessageContext.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Net.WebSockets;
34

45
namespace NetCoreStack.WebSockets
@@ -30,7 +31,8 @@ public string CommandText
3031

3132
public WebSocketMessageContext()
3233
{
33-
Header = new Dictionary<string, object>();
34+
Command = WebSocketCommands.DataSend;
35+
Header = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
3436
}
3537
}
3638
}

test/WebClientTestApp/Constants.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace WebClientTestApp
2+
{
3+
public static class Constants
4+
{
5+
public const string ConnectorName = "WebClientSPA";
6+
}
7+
}

test/WebClientTestApp/Views/Home/Index.cshtml

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424

2525
<br />
2626
<div id="connectionContainer">
27+
<div class="row" data-bind="visible: typeof(connectionId()) != 'undefined'">
28+
<div class="col-sm-12 col-md-12 col-lg-12">
29+
<div class="alert alert-success">
30+
ConnectionId: <span data-bind="text: connectionId"></span>
31+
</div>
32+
</div>
33+
</div>
2734
<div class="row">
2835
<div class="col-sm-12 col-md-12 col-lg-12">
2936
<div class="alert alert-warning" data-bind="html: logInfo">
@@ -36,8 +43,7 @@
3643
<div class="input-group">
3744
<input type="text" class="form-control" disabled="disabled" data-bind="value: connectionUrl">
3845
<div class="input-group-btn">
39-
<button type="button" data-bind="click: connect, text: connectionState, css: connectionStatus">
40-
</button>
46+
<button type="button" data-bind="click: connect, text: connectionState, css: connectionStatus"></button>
4147
</div>
4248
</div>
4349
<br />
@@ -80,11 +86,19 @@
8086
$(function () {
8187
'use strict';
8288
89+
var connectorName = "@Constants.ConnectorName"
8390
var containerId = "connectionContainer";
8491
var scheme = document.location.protocol == "https:" ? "wss" : "ws";
8592
var port = document.location.port ? (":" + document.location.port) : "";
8693
var socket;
8794
95+
var WebSocketCommands = {
96+
Connect: 1,
97+
DataSend: 2,
98+
Handshake: 4,
99+
All: 7
100+
}
101+
88102
var state = {
89103
connect: "Connect",
90104
connecting: "Connecting...",
@@ -93,9 +107,10 @@
93107
94108
var ViewModel = function () {
95109
var that = this;
110+
this.connectionId = ko.observable();
96111
this.data = ko.observable();
97112
this.dataRows = ko.observableArray([]);
98-
this.connectionUrl = ko.observable(scheme + "://" + document.location.hostname + port);
113+
this.connectionUrl = ko.observable(scheme + "://" + document.location.hostname + port + "?connectorName=" + connectorName);
99114
this.logInfo = ko.observable("Waiting for connection. To connect click the Connect button");
100115
this.connectionState = ko.observable(state.connect);
101116
this.connectionStatus = ko.pureComputed(function () {
@@ -139,14 +154,25 @@
139154
}
140155
};
141156
socket.onmessage = function (event) {
142-
var row = {
143-
dataFrom: "Server",
144-
dataTo: "Client",
145-
colorFrom: "row-server",
146-
colorTo: "row-client",
147-
context: event.data
148-
};
149-
vm.dataRows.push(row);
157+
158+
if (event.data) {
159+
160+
var context = JSON.parse(event.data);
161+
162+
console.log(vm.connectionId());
163+
if (context && context.Command == WebSocketCommands.Handshake) {
164+
vm.connectionId(context.Value);
165+
}
166+
167+
var row = {
168+
dataFrom: "Server",
169+
dataTo: "Client",
170+
colorFrom: "row-server",
171+
colorTo: "row-client",
172+
context: event.data
173+
};
174+
vm.dataRows.push(row);
175+
}
150176
};
151177
};
152178
@@ -162,7 +188,7 @@
162188
if (!socket || socket.readyState != WebSocket.OPEN) {
163189
vm.logInfo("Not connected!");
164190
}
165-
var data = vm.data();
191+
var data = JSON.stringify({ header: { "connectorName": connectorName, "connectionId": vm.connectionId(), }, value: vm.data() });
166192
socket.send(data);
167193
var row = {
168194
dataFrom: "Client",

test/WebClientTestApp/Views/Shared/_Layout.cshtml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>@ViewData["Title"] - Test App 1</title>
7-
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
7+
<link rel="stylesheet" href="~/css/bootstrap.min.css" />
88
</head>
99
<body>
1010
<div class="navbar navbar-inverse">
@@ -28,9 +28,9 @@
2828
<div class="container">
2929
@RenderBody()
3030
</div>
31-
<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"></script>
32-
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
33-
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.1/knockout-min.js"></script>
31+
<script src="~/js/jquery-2.2.0.min.js"></script>
32+
<script src="~/js/bootstrap.min.js"></script>
33+
<script src="~/js/knockout-min.js"></script>
3434

3535
@RenderSection("scripts", required: false)
3636
</body>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/// <autosync enabled="true" />
2+
/// <reference path="js/bootstrap.min.js" />
3+
/// <reference path="js/jquery-2.2.0.min.js" />
4+
/// <reference path="js/knockout-min.js" />

test/WebClientTestApp/wwwroot/css/bootstrap.min.css

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/WebClientTestApp/wwwroot/js/bootstrap.min.js

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)