Aspnetcore.docs: How to use Websocket client to connect SignalR Server?

Created on 16 May 2018  路  5Comments  路  Source: dotnet/AspNetCore.Docs

My Client is a normal Websocket Client, so what should I do

Source - Docs.ms

Most helpful comment

@Rick-Anderson
I resolve it.
First, configure json protocol for signalr in startup.cs
Func ConfigureServices

services.AddSignalR(options =>
            {
                // Faster pings for testing
                options.KeepAliveInterval = TimeSpan.FromSeconds(5);
            }).AddJsonProtocol(options =>
            {
                //options.PayloadSerializerSettings.Converters.Add(JsonConver);
                //the next settings are important in order to serialize and deserialize date times as is and not convert time zones
                options.PayloadSerializerSettings.Converters.Add(new IsoDateTimeConverter());
                options.PayloadSerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Unspecified;
                options.PayloadSerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
            });

And Client(My Client is ClientWebSocket) send the handshake Request

        var handshakes = new List<byte>(Encoding.UTF8.GetBytes(@"{""protocol"":""json"", ""version"":1}"))
                {
                    0x1e
                };
        await ws.SendAsync(new ArraySegment<byte>(handshakes.ToArray()), WebSocketMessageType.Text, true, CancellationToken.None);

end of every package is a Separator, the separator is 0x1e for Json Protocol.
https://github.com/aspnet/SignalR/blob/dev/specs/HubProtocol.md#json-payload-encoding

SignalR Version: Abp.AspNetCore.SignalR 3.6.1-preview5

All 5 comments

Hello @LGinC! You can start here https://docs.microsoft.com/en-us/aspnet/core/signalr/get-started?view=aspnetcore-2.1&tabs=visual-studio. SignalR uses WebSockets under the hood, so you wouldn't want to use both together. The recommended approach is to use the Hubs model as described here, so that all the important things like scaling and resource management are taken care of for you. You can choose to remain on WebSockets and not upgrade or use SignalR.

@rachelappel My client is running on WeChatMiniProgram, cannot use signalR client, just websocket client.
I try to connect SignalR server, but it disconnect when I send message

@LGinC can you post this question to Stack Overflow and report the link back here?

@Rick-Anderson
I resolve it.
First, configure json protocol for signalr in startup.cs
Func ConfigureServices

services.AddSignalR(options =>
            {
                // Faster pings for testing
                options.KeepAliveInterval = TimeSpan.FromSeconds(5);
            }).AddJsonProtocol(options =>
            {
                //options.PayloadSerializerSettings.Converters.Add(JsonConver);
                //the next settings are important in order to serialize and deserialize date times as is and not convert time zones
                options.PayloadSerializerSettings.Converters.Add(new IsoDateTimeConverter());
                options.PayloadSerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Unspecified;
                options.PayloadSerializerSettings.DateParseHandling = DateParseHandling.DateTimeOffset;
            });

And Client(My Client is ClientWebSocket) send the handshake Request

        var handshakes = new List<byte>(Encoding.UTF8.GetBytes(@"{""protocol"":""json"", ""version"":1}"))
                {
                    0x1e
                };
        await ws.SendAsync(new ArraySegment<byte>(handshakes.ToArray()), WebSocketMessageType.Text, true, CancellationToken.None);

end of every package is a Separator, the separator is 0x1e for Json Protocol.
https://github.com/aspnet/SignalR/blob/dev/specs/HubProtocol.md#json-payload-encoding

SignalR Version: Abp.AspNetCore.SignalR 3.6.1-preview5

@LGinC great. Thanks for posting back the solution.

Was this page helpful?
0 / 5 - 0 ratings