4

尝试为 azure signalr 服务创建 POC。我找到了 github 示例,其中似乎有一些可靠的示例。我选择了这个。基本上,我的问题是,当我在本地运行代码时,就像使用 localhost url 的冠军一样,但是当我尝试使用 Azure SignalR 服务运行使用从 azure 门户密钥复制的 url 时,格式如下:Endpoint=<service_endpoint>;AccessKey=<access_key>;,我收到一条错误消息,指出“无效的 URI:URI 方案无效。”。如何从我从密钥复制的内容中转换 url 并使用它来连接到信号器服务?

class Program
{
    private const string DefaultHubEndpoint = "Endpoint=http://someFakesrsname.service.signlar.net;AccsssKey=thisseemslikeagoodaccesskeytouseformyquestion";//"http://localhost:5000/ManagementSampleHub";
    private const string Target = "Target";
    private const string DefaultUser = "User";

    static void Main(string[] args)
    {
        var app = new CommandLineApplication();
        app.FullName = "Azure SignalR Management Sample: SignalR Client Tool";
        app.HelpOption("--help");

        var hubEndpointOption = app.Option("-h|--hubEndpoint", $"Set hub endpoint. Default value: {DefaultHubEndpoint}", CommandOptionType.SingleValue, true);
        var userIdOption = app.Option("-u|--userIdList", "Set user ID list", CommandOptionType.MultipleValue, true);

        app.OnExecute(async () =>
        {
            var hubEndpoint = hubEndpointOption.Value() ?? DefaultHubEndpoint;
            var userIds = userIdOption.Values != null && userIdOption.Values.Count > 0 ? userIdOption.Values : new List<string>() { "User" };

            Console.WriteLine("hubEndpoint: " + hubEndpoint);
            Console.WriteLine("DefaultHubEndpoint: " + DefaultHubEndpoint);
            foreach (var userId in userIds)
            {
                Console.WriteLine("UserId: " + userId);
            }

            var connections = (from userId in userIds
                               select CreateHubConnection(hubEndpoint, userId)).ToList();

            await Task.WhenAll(from conn in connections
                               select conn.StartAsync());

            Console.WriteLine($"{connections.Count} Client(s) started...");
            Console.ReadLine();

            await Task.WhenAll(from conn in connections
                               select conn.StopAsync());
            return 0;
        });

        app.Execute(args);
    }

    static HubConnection CreateHubConnection(string hubEndpoint, string userId)
    {
        var url = hubEndpoint.TrimEnd('/') + $"?user={userId}";
        var connection = new HubConnectionBuilder().WithUrl(url).Build();
        connection.On(Target, (string message) =>
        {
            Console.WriteLine($"{userId}: gets message from service: '{message}'");
        });

        connection.Closed += async ex =>
        {
            Console.WriteLine(ex);
            Environment.Exit(1);
        };

        return connection;
    }
}
enter code here
4

0 回答 0