แพ็กเกจอย่างเป็นทางการ · เฟรมเวิร์ก

ฮับแบบ SignalR สำหรับ ASP.NET Core และ Blazor

เขียนฮับด้วย SignalR API ที่คุณคุ้นเคย ทั้ง Clients.All กลุ่ม ผู้ใช้ ฮับแบบมีชนิด และ IHubContext เซิร์ฟเวอร์ของคุณยังจัดการการเรียกฮับเหมือนเดิม โดยไม่ต้องใช้เซิร์ฟเวอร์ WebSocket, backplane หรือ Azure SignalR Service

  • HibiraHub ใช้งานแบบเดียวกับ Hub ของ SignalR และ MapHibiraHub<T>() แมปไว้ข้างเอนด์พอยต์อื่นของคุณ
  • Hibira.Client มี SignalR client API สำหรับ Blazor WebAssembly, Blazor Server, MAUI และแอปคอนโซล ส่วนในเบราว์เซอร์ใช้ hibira-hub.js
  • แพ็กเกจ Hibira ส่งอีเวนต์ ลงนามแชนเนล ตรวจสอบ webhook และขอการอนุมัติได้ รองรับ .NET 8 ขึ้นไป

ลงชื่อเข้าใช้เพื่อดูโค้ดตัวอย่างเหล่านี้พร้อมโฮสต์คลัสเตอร์ของคุณเอง ลงชื่อเข้าใช้

  1. ติดตั้ง

    dotnet add package Hibira.AspNetCore --version 0.1.0
    dotnet add package Hibira.Client --version 0.1.0   # Blazor or other .NET clients
    
    dotnet user-secrets set Hibira:AppId YOUR_APP_ID
    dotnet user-secrets set Hibira:Key YOUR_APP_KEY
    dotnet user-secrets set Hibira:Secret YOUR_APP_SECRET
    dotnet user-secrets set Hibira:Host ws.hibira.io
  2. ตั้งค่า

    using Hibira.AspNetCore;
    
    public class ChatHub : HibiraHub
    {
        public Task SendMessage(string user, string message) =>
            Clients.All.SendAsync("ReceiveMessage", user, message);
    }
    
    // Program.cs
    builder.Services.AddHibira(); // reads the "Hibira" section
    var app = builder.Build();
    app.UseStaticFiles();          // serves hibira-hub.js
    app.MapHibiraHub<ChatHub>("/hubs/chat");
  3. ใช้งาน

    // A Blazor component: Microsoft's SignalR tutorial with one using changed
    @using Hibira.Client
    @inject NavigationManager Navigation
    
    @code {
        private HubConnection? hub;
        private readonly List<string> messages = new();
    
        protected override async Task OnInitializedAsync()
        {
            hub = new HubConnectionBuilder()
                .WithUrl(Navigation.ToAbsoluteUri("/hubs/chat"))
                .WithAutomaticReconnect()
                .Build();
            hub.On<string, string>("ReceiveMessage", (user, message) =>
            {
                messages.Add($"{user}: {message}");
                InvokeAsync(StateHasChanged);
            });
            await hub.StartAsync();
        }
    }

ก่อนเริ่ม

  • ซีเคร็ตของแอปจะแสดงเพียงครั้งเดียว ตอนสร้างแอปหรือตอนหมุนเวียนซีเคร็ต เก็บไว้บนเซิร์ฟเวอร์เท่านั้น ห้ามใส่ในเบราว์เซอร์หรือไฟล์บิลด์ของเกม
  • ในแพ็กเกจ Free ให้ใส่ทุก origin ที่หน้าเว็บโหลดมาใน Origin ที่อนุญาตของแอป (https://localhost:5001 ระหว่างพัฒนา แล้วตามด้วยโดเมนของคุณ) ส่วน "*" ต้องใช้แพ็กเกจแบบชำระเงิน

ใช้ app key จากแดชบอร์ดของคุณ ส่วน secret ให้เก็บไว้บนเซิร์ฟเวอร์เท่านั้น ช่อง อีเวนต์ และ endpoint การยืนยันสิทธิ์ทำงานเหมือนกับที่ใช้กับ Pusher ทุกประการ สร้างบัญชีฟรีของคุณ →