.NET WPF 相当于 laravel-echo

.NET WPF equivalent of laravel-echo

我正在用 WPF 构建一个简单的餐厅管理系统。我的后端在 Laravel。我需要设置一个网络套接字,以便在客户从移动应用程序下订单时在 WPF 应用程序上获取实时通知。我使用 beyondcode/laravel-websockets 在 Laravel 中配置了网络套接字。为方便起见,我在客户端使用 laravel-echo 和 Vue 测试了 Web 套接字。那里一切正常,但我找不到任何解决方案来在 C# 中复制 laravel-echo

这是我在 Vue.js 和 laravel-echo 中使用的代码:

import Echo from "laravel-echo";
import Pusher from "pusher-js";
window.Pusher = Pusher;

const token = "1|CSaob3KZhU5UHiocBjPgzpazbceUKTLRLJO0ZIV0"

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'laravel_rdb',
    wsHost: '127.0.0.1',
    authEndpoint: 'http://localhost/may-app/public/broadcasting/auth',
    encrypted: false,
    forceTLS: false,
    wsPort: 6001,
    wssPort: 6001,
    disableStats: true,
    enabledTransports: ['ws', 'wss'],
    auth : {
        headers : {
            Authorization: "Bearer " + token,
            Accept: "application/json",
        }
    },
})

window.Echo.private('customer-order')
    .listen('OrderPlaced', (e) => {
    console.log(e)
})

我找到了 SocketIOClient is used to implement web socket functionality in .NET. I tried to use a solution I found but it didn't work for me. Also, I didn't find any way to set up my authentication URL in this package. I read socket.io 任何与身份验证相关的文档,但我找不到任何文档。

如何在 C# .NET 中实现与 laravel-echo 中等效的功能?

.NET 可能没有像 laravel-echo 这样的客户端。但是,您将能够使用 pusher client: pusher/pusher-websocket-dotnet 连接到您的套接字,这可能是您可以达到的最高级别的兼容性。但是你需要自己解析你的消息并订阅频道,不会有像 laravel-echo =(

我能够使用 PunyFlash in the answers. The NuGet package is available here and here 提到的包实施解决方案是 GitHub 存储库。

我的解决方案可能对将来的某些人有用,因此,我上面 laravel-echo 代码在 .NET 中的等效代码是:

internal class OrderSocket
    {
        public static async void Connect()
        {
            try
            {
                //Setting authentication
                var authorizer = new CustomAuthorizer("http://localhost/may-app/public/broadcasting/auth")
                {
                    AuthenticationHeader = new System.Net.Http.Headers.AuthenticationHeaderValue("Authorization", "Bearer " + "1|CSaob3KZhU5UHiocBjPgzpazbceUKTLRLJO0ZIV0"),
                };

                //Creating pusher object with authentication
                Pusher pusher = new Pusher("laravel_rdb", new PusherOptions
                {
                    Authorizer = authorizer,
                    Host = "127.0.0.1:6001",
                });

                //Connecting to web socket
                await pusher.ConnectAsync().ConfigureAwait(false);

                //Subscribing to channel
                Channel channel = await pusher.SubscribeAsync("private-customer-order").ConfigureAwait(false);
                if (channel.IsSubscribed)
                {
                    //Binding to an event
                    channel.Bind("App\Events\OrderPlaced", (PusherEvent eventResponse) =>
                    {
                        // Deserialize json if server returns json values
                        Debug.WriteLine(eventResponse.Data);
                    });
                }
            }
            catch (Exception)
            {
                Debug.WriteLine("An exception occurred.");
            }
        }
    }

    //HttpAuthorizer child class to set default headers
    internal class CustomAuthorizer : HttpAuthorizer
    {
        public CustomAuthorizer(string authEndpoint) : base(authEndpoint) { }

        public override void PreAuthorize(HttpClient httpClient)
        {
            base.PreAuthorize(httpClient);
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
        }
    }