{"id":19029293,"url":"https://github.com/sulmar/dotnet-core-websocket","last_synced_at":"2026-06-22T22:31:30.673Z","repository":{"id":94109965,"uuid":"253729509","full_name":"sulmar/dotnet-core-websocket","owner":"sulmar","description":"Przykład obsługi WebSocket w .NET Core","archived":false,"fork":false,"pushed_at":"2020-04-07T09:46:53.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-21T20:13:28.755Z","etag":null,"topics":["dotnet-core3","websocket"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sulmar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-07T08:14:08.000Z","updated_at":"2020-04-07T09:46:55.000Z","dependencies_parsed_at":"2023-03-11T14:15:26.451Z","dependency_job_id":null,"html_url":"https://github.com/sulmar/dotnet-core-websocket","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sulmar/dotnet-core-websocket","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdotnet-core-websocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdotnet-core-websocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdotnet-core-websocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdotnet-core-websocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sulmar","download_url":"https://codeload.github.com/sulmar/dotnet-core-websocket/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sulmar%2Fdotnet-core-websocket/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34668500,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-22T02:00:06.391Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dotnet-core3","websocket"],"created_at":"2024-11-08T21:13:47.518Z","updated_at":"2026-06-22T22:31:30.648Z","avatar_url":"https://github.com/sulmar.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Websocket w .NET Core 3 z użyciem Endpoints\n\n\n## Wprowadzenie\n\nOpis użycia WebSocket w .NET Core znajduje się w artykule [WebSockets support in ASP.NET Core](https://docs.microsoft.com/pl-pl/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1)\n\nPrzedstawiony tam sposób używa przechwytywania   żądań za pomocą metody *Use()*. Postanowiłem przerobić to na endpoint, które w .NET Core 3 są zalecanym sposobem mapowania żądań.\n\nPrzedstawię poszczególne sposoby mapowania.\n\n## Przykładowa metoda *Echo()*\n\n~~~ csharp\n\n  private async Task Echo(WebSocket webSocket)\n        {\n            var buffer = new byte[1024 * 4];\n\n            WebSocketReceiveResult result = await webSocket.ReceiveAsync(new ArraySegment\u003cbyte\u003e(buffer), CancellationToken.None);\n            while (!result.CloseStatus.HasValue)\n            {\n                await webSocket.SendAsync(new ArraySegment\u003cbyte\u003e(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None);\n\n                result = await webSocket.ReceiveAsync(new ArraySegment\u003cbyte\u003e(buffer), CancellationToken.None);\n            }\n            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);\n        }\n~~~\n\n\n## Mapowanie z użyciem IApplicationBuilder\n\n~~~ csharp\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{\n    app.Map(\"/ws\", builder =\u003e\n            {\n                builder.Use(async (context, next) =\u003e\n                {\n                    if (context.WebSockets.IsWebSocketRequest)\n                    {\n                        var webSocket = await context.WebSockets.AcceptWebSocketAsync();\n                        await Echo(webSocket);\n                        return;\n                    }\n                    await next();\n                });\n            });\n}\n~~~\n\n\n## Mapowanie z użyciem endpoints\n\n~~~ csharp\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{\n     app.UseEndpoints(endpoints =\u003e\n            {\n                endpoints.Map(\"/ws\", async (context) =\u003e\n                {\n                    if (context.WebSockets.IsWebSocketRequest)\n                    {\n                        var webSocket = await context.WebSockets.AcceptWebSocketAsync();\n\n                        await Echo(webSocket);\n                    }\n                });\n}\n~~~\n\n## Utworzenie metody rozszerzającej\n\n~~~ csharp\npublic static class WebSocketEndpointRouteBuilderExtensions\n    {\n        public static IEndpointConventionBuilder MapWebSocket(\n                 this IEndpointRouteBuilder endpoints,\n                 string pattern,\n                 Func\u003cWebSocket, Task\u003e execute = null)\n        {\n\n            return endpoints.Map(pattern, async (context) =\u003e\n            {\n                if (context.WebSockets.IsWebSocketRequest)\n                {\n                    var webSocket = await context.WebSockets.AcceptWebSocketAsync();\n\n                    await execute?.Invoke(webSocket);\n                }\n            });\n        }\n    }\n~~~\n\n~~~ csharp\npublic void Configure(IApplicationBuilder app, IWebHostEnvironment env)\n{\n      endpoints.MapWebSocket(\"/ws\", async websocket =\u003e await Echo(websocket));\n}\n~~~\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsulmar%2Fdotnet-core-websocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsulmar%2Fdotnet-core-websocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsulmar%2Fdotnet-core-websocket/lists"}