SignalR handshake was canceled ์งˆ๋ฌธ

์•ˆ๋…•ํ•˜์„ธ์š”.

ASP.NET Core Web API ์„œ๋น„์Šค๋ฅผ
UseKestrel ํ†ตํ•ด ์…€ํ”„ ํ˜ธ์ŠคํŒ…์œผ๋กœ ์‚ฌ์šฉํ•˜ ๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. (IIS ์‚ฌ์šฉ X)

Web API์— SignalR ์„œ๋น„์Šค๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„œ๋น„์Šค๋ฅผ ๋“ฑ๋กํ•˜๊ณ 
Hub๊ตฌ์„ฑ์„ ํ•˜์˜€๋Š”๋ฐ์š”โ€ฆ

[Startup.cs]

services.AddSignalR(option =>
{
                option.HandshakeTimeout = new TimeSpan(0, 1, 0);
                option.KeepAliveInterval = new TimeSpan(0, 1, 0);
                option.EnableDetailedErrors = true;
});
app.UseEndpoints(endpoints =>
{
                endpoints.MapControllers();
                endpoints.MapHub<PlaceHub>("/placeHub");
);

์œ„์™€ ๊ฐ™์ด ์„ค์ • ํ›„ ์›น์†Œ์ผ“ ์„œ๋ฒ„๊ฐ€ Open๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์›น์†Œ์ผ“ ํด๋ผ์ด์–ธํŠธ๋Š” ์ผ๋ฐ˜ C# Console ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด
signalR ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์•„๋‹Œ System.Net.WebSockets ์„ ์ด์šฉํ•ด์„œ ์ง์ ‘
ASP.NET Core Web API ์›น์†Œ์ผ“ ์„œ๋ฒ„์— ์ ‘์† ์‹œ์ผฐ์Šต๋‹ˆ๋‹ค.

์ฐธ๊ณ  : aspnetcore/Program.cs at main ยท dotnet/aspnetcore (github.com)

์›น์„œ๋ฒ„์— ์ ‘์† ๊นŒ์ง€๋Š” ๊ฐ€๋Šฅํ•˜๋‚˜ Receiving์—์„œ ReceiveAsync()ํ˜ธ์ถœ์„ ํ•˜๋ฉด
์œ„์— ์„œ๋ฒ„์—์„œ ์„ค์ •ํ•œ ํ•ธ๋“œ์‰์ดํฌ ํƒ€์ž„์•„์›ƒ ์‹œ๊ฐ„ ์ดํ›„์—

์„œ๋ฒ„์ธก์œผ๋กœ ๋ถ€ํ„ฐ handshake was canceled ์˜ค๋ฅ˜๊ฐ€ ๋ฉ”์„ธ์ง€๊ฐ€ ์ „๋‹ฌ ๋ฉ๋‹ˆ๋‹ค.

  • ์ฝ˜์†” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์•„๋‹Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ๋Š” ์ •์ƒ ํ†ต์‹ ์ด ๋ฉ๋‹ˆ๋‹ค.,
  • wss ์‚ฌ์šฉ์ด ์•„๋‹Œ ws๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

ํ˜น์‹œ ํ•ด๋‹น ์ด์Šˆ์— ๋Œ€ํ•ด ์›์ธ๊ณผ ํ•ด๊ฒฐ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ๊ณ„์‹ ๋ถ„ ๊ณ„์‹œ๋ฉด ๋‹ต๋ณ€ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

ํ˜น์‹œ๋‚˜ ํ•˜์—ฌ Web API ์„œ๋ฒ„์ธก Hub ์ฝ”๋“œ๋„ ์˜ฌ๋ฆฝ๋‹ˆ๋‹ค.

public class PlaceHub : Hub
{
        public override Task OnConnectedAsync()
        {
            return Clients.All.SendAsync("ServerMsg", "Connected");
        }

        public override Task OnDisconnectedAsync(Exception exception)
        {
            return Clients.All.SendAsync("ServerMsg", "Disconnecte");
        }

        public Task SendToOthers(string senderID, string tickerCode, string ticker, double price, double units, double weight, string type, string kind)
        {
            return Clients.Others.SendAsync("Place", senderID, tickerCode, ticker, price, units, weight, type, kind);
        }
}
1๊ฐœ์˜ ์ข‹์•„์š”

SignalR์„ ์•ˆ์“ด์ง€ ์ข€๋˜์–ด์„œ ์ž˜๋ชป๋œ ๋ฌผ์Œ์ผ ์ˆ˜ ๋„ ์žˆ๋Š”๋ฐ,

SignalR.Client๋ฅผ ์•ˆ์“ด ์ด์œ ๊ฐ€ Console ํ™˜๊ฒฝ์ด๋ผ๋Š”๊ฒŒ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š๊ณ , WebSocket์„ ์ด์šฉํ•ด ์ง์ ‘ SignalR ํ”„๋กœํ† ์ฝœ์„ ๊ตฌํ˜„ํ•˜์…จ๋‹ค๋Š” ๋ง์”€์ธ๊ฐ€์š”?

์ง€๊ธˆ ์žฌํ˜„ํ•ด๋ณด์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ž˜๋ชป๋œ ์ •๋ณด์ผ ์ˆ˜๋„ ์žˆ์ง€๋งŒ ์ œ ๊ฒฝํ—˜์œผ๋กœ๋Š” Console์—์„œ๋„ SignalR.Client๋ฅผ ์ด์šฉํ•ด WebSocket๋ฅผ ์ผ๋˜ ๊ธฐ์–ต์ž…๋‹ˆ๋‹ค.

2๊ฐœ์˜ ์ข‹์•„์š”

์ฝ˜์†” ํ™˜๊ฒฝ์ด๋ผ์„œ SignalR.Client์„ ์•ˆ์“ด๊ฑด ์•„๋‹ˆ๊ณ 

์ด์ „์— ๊ฐ„๋‹จํ•˜๊ฒŒ System.Net.WebSockets ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์›น์†Œ์ผ“ ํด๋ผ์ด์–ธํŠธ๋กœ ์‚ฌ์šฉํ–ˆ๋˜ ์ฝ”๋“œ๊ฐ€ ์žˆ์–ด

์žฌ์‚ฌ์šฉ ํ•˜๋ ค๊ณ  ํ–ˆ๋˜ ๊ฑฐ์˜€์–ด์š”

์ง€๊ธˆ ์ƒ๊ฐํ•ด๋ณด๋‹ˆ

์‹œ๊ทธ๋„R์€ ์ž์ฒด์ ์ธ ํ”„๋กœํ† ์ฝœ๋กœ ํ•ธ๋“œ์‰์ดํฌ ๊ณผ์ •์ด ๋“ค์–ด๊ฐ€์•ผ ์ •์ƒ ํ†ต์‹ ์ด ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
์ด์™€ ๊ฐ™์€ ํ–‰์œ„๋ฅผ ๋‚ด๋ถ€์ ์œผ๋กœ ์ž์ฒด ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”๊ฒƒ์ด
SignalR.Client ์ธ๊ฑฐ ๊ฐ™๊ตฌ์š”

์ด ๋ถ€๋ถ„์„ ์ œ๊ฐ€ ์ƒ๊ฐํ•˜์ง€ ๋ชปํ•˜๊ณ  ๋‹จ์ˆœํžˆ ์„œ๋ฒ„์— ์ ‘์†ํ•ด์„œ ๋ฉ”์„ธ์ง€๋งŒ ์ „์†ก/์ˆ˜์‹  ๋งŒ ํ•˜๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜ ํ•˜๊ณ  ์ฐฉ๊ฐ ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋ง์”€ํ•ด์ฃผ์‹  ๊ฒƒ ์ฒ˜๋Ÿผ SignalR.Client๋กœ ๋Œ€์ฒด ํ•˜๋˜๊ฐ€

์‹œ๊ทธ๋„R์„ ์“ฐ์ง€ ์•Š๊ณ  ์ง์ ‘ ์›น์†Œ์ผ“ ์„œ๋ฒ„๋ฅผ Openํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.
ASP.NET Core์—์„œ WebSocket ์ง€์› | Microsoft Docs

๋‹ต๋ณ€ ๊ฐ์‚ฌ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

2๊ฐœ์˜ ์ข‹์•„์š”

๋„ˆ๋ฌด ์˜ˆ์ „ ๊ธ€์ด๊ธด ํ•œ๋ฐโ€ฆ

๋ง์”€ํ•˜์‹  ๊ฒƒ ์ฒ˜๋Ÿผ SignalR ์„œ๋ฒ„์— ์ง์ ‘ WebSocket์œผ๋กœ ๋ถ™๊ธฐ ์œ„ํ•ด์„œ๋Š”
WebSocket ์—ฐ๊ฒฐ ์ดํ›„์— ์–ด๋–ค ํ”„๋กœํ† ์ฝœ๋กœ ํ†ต์‹ ์„ ํ•  ๊ฒƒ์ธ์ง€ ์ง€์ •ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค.

{โ€œprotocolโ€:โ€œjsonโ€,โ€œversionโ€:1}

๋ฅผ ๋ณด๋‚ด์ฃผ์‹œ๋ฉด SignalR ์—ฐ๊ฒฐ์ด ์™„๋ฃŒ๋˜๋ฉฐ ๊ทธ ๋‹ค์Œ๋ถ€ํ„ฐ ํ•˜ํŠธ๋น„ํŠธ๋ฅผ ๋ฐ›์œผ์‹ค ์ˆ˜ ์žˆ์„ ๊ฒ๋‹ˆ๋‹ค.

1๊ฐœ์˜ ์ข‹์•„์š”