开始在 .NET 中使用中继混合连接 WebSocketGet started with Relay Hybrid Connections WebSockets in .NET

在本快速入门中,请创建 .NET 发送者和接收者应用程序,以便在 Azure 中继中通过混合连接 WebSocket 发送和接收消息。In this quickstart, you create .NET sender and receiver applications that send and receive messages by using Hybrid Connections WebSockets in Azure Relay. 若要了解 Azure 中继的常规信息,请参阅 Azure 中继To learn about Azure Relay in general, see Azure Relay.

在本快速入门中,你将执行以下步骤:In this quickstart, you take the following steps:

  1. 使用 Azure 门户创建中继命名空间。Create a Relay namespace by using the Azure portal.
  2. 使用 Azure 门户在该命名空间中创建混合连接。Create a hybrid connection in that namespace by using the Azure portal.
  3. 编写服务器(侦听器)控制台应用程序,用于接收消息。Write a server (listener) console application to receive messages.
  4. 编写客户端(发送方)控制台应用程序,用于发送消息。Write a client (sender) console application to send messages.
  5. 运行应用程序。Run applications.

先决条件Prerequisites

若要完成本教程,需要满足以下先决条件:To complete this tutorial, you need the following prerequisites:

创建命名空间Create a namespace

  1. 登录 Azure 门户Sign in to the Azure portal.

  2. 选择“创建资源”。Select Create a resource. 然后,选择“集成” > “中继”。Then, select Integration > Relay. 如果未在列表中看到“中继”,请选择右上角的“查看全部”。If you don't see Relay in the list, select See All in the top-right corner.

  3. 选择“创建”,然后在“名称”字段中输入命名空间名称。Select Create, and enter a namespace name in the Name field. Azure 门户会检查该名称是否可用。Azure portal checks to see if the name is available.

  4. 选择要在其中创建命名空间的 Azure 订阅。Choose an Azure subscription in which to create the namespace.

  5. 对于资源组,选择一个要在其中放置命名空间的现有资源组,或创建一个新资源组。For Resource group, choose an existing resource group in which to place the namespace, or create a new one.

  6. 选择应托管该命名空间的国家或地区。Select the country or region in which your namespace should be hosted.

    创建命名空间

  7. 选择“创建” 。Select Create. Azure 门户将创建该命名空间并启用它。The Azure portal creates your namespace and enables it. 几分钟后,系统将为用户的帐户预配资源。After a few minutes, the system provisions resources for your account.

获取管理凭据Get management credentials

  1. 选择“所有资源”,然后选择新建的命名空间名称。Select All resources, and then choose the newly created namespace name.

  2. 选择“共享访问策略”。Select Shared access policies.

  3. 在“共享访问策略”下,选择“RootManageSharedAccessKey”。Under Shared access policies, select RootManageSharedAccessKey.

  4. 在“SAS 策略:RootManageSharedAccessKey”下,选择“主连接字符串”旁边的“复制”按钮。Under SAS Policy: RootManageSharedAccessKey, select the Copy button next to Primary Connection String. 此操作会将连接字符串复制到剪贴板,供以后使用。This action copies the connection string to your clipboard for later use. 将此值粘贴到记事本或其他某个临时位置。Paste this value into Notepad or some other temporary location.

  5. 重复上述步骤,将主密钥的值复制和粘贴到临时位置,供以后使用。Repeat the preceding step to copy and paste the value of Primary key to a temporary location for later use.

    connection-string

创建混合连接Create a hybrid connection

确保已创建中继命名空间Ensure that you have already created a Relay namespace.

  1. 登录到 Azure 门户Sign in to the Azure portal.

  2. 在左侧菜单中,选择“所有资源” 。In the left menu, select All resources.

  3. 选择要在其中创建混合连接的命名空间。Select the namespace where you want to create the hybrid connection. 在本示例中,该命名空间为 mynewnsIn this case, it is mynewns.

  4. 在“中继命名空间”下选择“混合连接”。 Under Relay namespace, select Hybrid Connections.

    创建混合连接

  5. 在命名空间概览窗口中,选择“+ 混合连接” In the namespace overview window, select + Hybrid Connection

    选择混合连接

  6. 在“创建混合连接”下输入一个值,作为混合连接名称。 Under Create Hybrid Connection, enter a value for the hybrid connection name. 保留其他默认值。Leave the other default values.

    选择“新建”

  7. 选择“创建” 。Select Create.

创建服务器应用程序(侦听程序)Create a server application (listener)

在 Visual Studio 中编写可侦听和接收来自中继的消息的 C# 控制台应用程序。In Visual Studio, write a C# console application to listen for and receive messages from the relay.

创建控制台应用程序Create a console application

在 Visual Studio 中创建新的控制台应用 (.NET Framework) 项目。In Visual Studio, create a new Console App (.NET Framework) project.

添加中继 NuGet 包Add the Relay NuGet package

  1. 右键单击新创建的项目,然后选择“管理 NuGet 包”。Right-click the newly created project, and then select Manage NuGet Packages.

  2. 选择“包括预发行版”选项。Select Include prerelease option.

  3. 选择“浏览”,然后搜索 Microsoft.Azure.RelaySelect Browse, and then search for Microsoft.Azure.Relay. 在搜索结果中,选择“Microsoft Azure 中继”。In the search results, select Microsoft Azure Relay.

  4. 对于版本,选择 2.0.0-preview1-20180523For the version, select 2.0.0-preview1-20180523.

  5. 选择“安装”即可完成安装。Select Install to complete the installation. 关闭对话框。Close the dialog box.

编写接收消息的代码Write code to receive messages

  1. 将 Program.cs 文件顶部的现有 using 语句替换为以下 using 语句:At the top of the Program.cs file, replace the existing using statements with the following using statements:

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Net;
    using Microsoft.Azure.Relay;
    
  2. 将常量添加到 Program 类,用于保存混合连接的连接详细信息。Add constants to the Program class for the hybrid connection details. 将括号中的占位符替换为在创建混合连接时获得的值。Replace the placeholders in brackets with the values that you obtained when you created the hybrid connection. 请务必使用完全限定的命名空间名称。Be sure to use the fully qualified namespace name.

    private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn";
    private const string ConnectionName = "{HybridConnectionName}";
    private const string KeyName = "{SASKeyName}";
    private const string Key = "{SASKey}";
    
  3. ProcessMessagesOnConnection 方法添加到 Program 类:Add the ProcessMessagesOnConnection method to the Program class:

    // The method initiates the connection.
    private static async void ProcessMessagesOnConnection(HybridConnectionStream relayConnection, CancellationTokenSource cts)
    {
        Console.WriteLine("New session");
    
        // The connection is a fully bidrectional stream. 
        // Put a stream reader and a stream writer over it.  
        // This allows you to read UTF-8 text that comes from 
        // the sender, and to write text replies back.
        var reader = new StreamReader(relayConnection);
        var writer = new StreamWriter(relayConnection) { AutoFlush = true };
        while (!cts.IsCancellationRequested)
        {
            try
            {
                // Read a line of input until a newline is encountered.
                var line = await reader.ReadLineAsync();
    
                if (string.IsNullOrEmpty(line))
                {
                    // If there's no input data, signal that 
                    // you will no longer send data on this connection,
                    // and then break out of the processing loop.
                    await relayConnection.ShutdownAsync(cts.Token);
                    break;
                }
    
                // Write the line on the console.
                Console.WriteLine(line);
    
                // Write the line back to the client, prepended with "Echo:"
                await writer.WriteLineAsync($"Echo: {line}");
            }
            catch (IOException)
            {
                // Catch an I/O exception. This likely occurred when
                // the client disconnected.
                Console.WriteLine("Client closed connection");
                break;
            }
        }
    
        Console.WriteLine("End session");
    
        // Close the connection.
        await relayConnection.CloseAsync(cts.Token);
    }
    
  4. RunAsync 方法添加到 Program 类:Add the RunAsync method to the Program class:

    private static async Task RunAsync()
    {
        var cts = new CancellationTokenSource();
    
        var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key);
        var listener = new HybridConnectionListener(new Uri(string.Format("sb://{0}/{1}", RelayNamespace, ConnectionName)), tokenProvider);
    
        // Subscribe to the status events.
        listener.Connecting += (o, e) => { Console.WriteLine("Connecting"); };
        listener.Offline += (o, e) => { Console.WriteLine("Offline"); };
        listener.Online += (o, e) => { Console.WriteLine("Online"); };
    
        // Opening the listener establishes the control channel to
        // the Azure Relay service. The control channel is continuously 
        // maintained, and is reestablished when connectivity is disrupted.
        await listener.OpenAsync(cts.Token);
        Console.WriteLine("Server listening");
    
        // Provide callback for the cancellation token that will close the listener.
        cts.Token.Register(() => listener.CloseAsync(CancellationToken.None));
    
        // Start a new thread that will continuously read the console.
        new Task(() => Console.In.ReadLineAsync().ContinueWith((s) => { cts.Cancel(); })).Start();
    
        // Accept the next available, pending connection request. 
        // Shutting down the listener allows a clean exit. 
        // This method returns null.
        while (true)
        {
            var relayConnection = await listener.AcceptConnectionAsync();
            if (relayConnection == null)
            {
                break;
            }
    
            ProcessMessagesOnConnection(relayConnection, cts);
        }
    
        // Close the listener after you exit the processing loop.
        await listener.CloseAsync(cts.Token);
    }
    
  5. Program 类的 Main 方法中添加以下代码行:Add the following line of code to the Main method in the Program class:

    RunAsync().GetAwaiter().GetResult();
    

    完成的 Program.cs 文件应如下所示:The completed Program.cs file should look like this:

    namespace Server
    {
        using System;
        using System.IO;
        using System.Threading;
        using System.Threading.Tasks;
        using Microsoft.Azure.Relay;
    
        public class Program
        {
            private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn";
            private const string ConnectionName = "{HybridConnectionName}";
            private const string KeyName = "{SASKeyName}";
            private const string Key = "{SASKey}";
    
            public static void Main(string[] args)
            {
                RunAsync().GetAwaiter().GetResult();
            }
    
            private static async Task RunAsync()
            {
                var cts = new CancellationTokenSource();
    
                var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key);
                var listener = new HybridConnectionListener(new Uri(string.Format("sb://{0}/{1}", RelayNamespace, ConnectionName)), tokenProvider);
    
                // Subscribe to the status events.
                listener.Connecting += (o, e) => { Console.WriteLine("Connecting"); };
                listener.Offline += (o, e) => { Console.WriteLine("Offline"); };
                listener.Online += (o, e) => { Console.WriteLine("Online"); };
    
                // Opening the listener establishes the control channel to
                // the Azure Relay service. The control channel is continuously 
                // maintained, and is reestablished when connectivity is disrupted.
                await listener.OpenAsync(cts.Token);
                Console.WriteLine("Server listening");
    
                // Provide callback for a cancellation token that will close the listener.
                cts.Token.Register(() => listener.CloseAsync(CancellationToken.None));
    
                // Start a new thread that will continuously read the console.
                new Task(() => Console.In.ReadLineAsync().ContinueWith((s) => { cts.Cancel(); })).Start();
    
                // Accept the next available, pending connection request. 
                // Shutting down the listener allows a clean exit. 
                // This method returns null.
                while (true)
                {
                    var relayConnection = await listener.AcceptConnectionAsync();
                    if (relayConnection == null)
                    {
                        break;
                    }
    
                    ProcessMessagesOnConnection(relayConnection, cts);
                }
    
                // Close the listener after you exit the processing loop.
                await listener.CloseAsync(cts.Token);
            }
    
            private static async void ProcessMessagesOnConnection(HybridConnectionStream relayConnection, CancellationTokenSource cts)
            {
                Console.WriteLine("New session");
    
                // The connection is a fully bidrectional stream. 
                // Put a stream reader and a stream writer over it.  
                // This allows you to read UTF-8 text that comes from 
                // the sender, and to write text replies back.
                var reader = new StreamReader(relayConnection);
                var writer = new StreamWriter(relayConnection) { AutoFlush = true };
                while (!cts.IsCancellationRequested)
                {
                    try
                    {
                        // Read a line of input until a newline is encountered.
                        var line = await reader.ReadLineAsync();
    
                        if (string.IsNullOrEmpty(line))
                        {
                            // If there's no input data, signal that 
                            // you will no longer send data on this connection.
                            // Then, break out of the processing loop.
                            await relayConnection.ShutdownAsync(cts.Token);
                            break;
                        }
    
                        // Write the line on the console.
                        Console.WriteLine(line);
    
                        // Write the line back to the client, prepended with "Echo:"
                        await writer.WriteLineAsync($"Echo: {line}");
                    }
                    catch (IOException)
                    {
                        // Catch an I/O exception. This likely occurred when
                        // the client disconnected.
                        Console.WriteLine("Client closed connection");
                        break;
                    }
                }
    
                Console.WriteLine("End session");
    
                // Close the connection.
                await relayConnection.CloseAsync(cts.Token);
            }
        }
    }
    

创建客户端应用程序(发送程序)Create a client application (sender)

在 Visual Studio 中编写可将消息发送到中继的 C# 控制台应用程序。In Visual Studio, write a C# console application to send messages to the relay.

创建控制台应用程序Create a console application

在 Visual Studio 中创建新的控制台应用 (.NET Framework) 项目。In Visual Studio, create a new Console App (.NET Framework) project.

添加中继 NuGet 包Add the Relay NuGet package

  1. 右键单击新创建的项目,然后选择“管理 NuGet 包” 。Right-click the newly created project, and then select Manage NuGet Packages.

  2. 选择“浏览”,然后搜索 Microsoft.Azure.RelaySelect Browse, and then search for Microsoft.Azure.Relay. 在搜索结果中,选择“Microsoft Azure 中继”。 In the search results, select Microsoft Azure Relay.

  3. 选择“安装” 即可完成安装。Select Install to complete the installation. 关闭对话框。Close the dialog box.

编写发送消息的代码Write code to send messages

  1. 将 Program.cs 文件顶部的现有 using 语句替换为以下 using 语句:At the top of the Program.cs file, replace the existing using statements with the following using statements:

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.Relay;
    
  2. 将常量添加到 Program 类,用于保存混合连接的连接详细信息。Add constants to the Program class for the hybrid connection details. 将括号中的占位符替换为在创建混合连接时获得的值。Replace the placeholders in brackets with the values that you obtained when you created the hybrid connection. 请务必使用完全限定的命名空间名称。Be sure to use the fully qualified namespace name.

    private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn";
    private const string ConnectionName = "{HybridConnectionName}";
    private const string KeyName = "{SASKeyName}";
    private const string Key = "{SASKey}";
    
  3. 将以下方法添加到 Program 类:Add the following method to the Program class:

    private static async Task RunAsync()
    {
        Console.WriteLine("Enter lines of text to send to the server with ENTER");
    
        // Create a new hybrid connection client.
        var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key);
        var client = new HybridConnectionClient(new Uri(String.Format("sb://{0}/{1}", RelayNamespace, ConnectionName)), tokenProvider);
    
        // Initiate the connection.
        var relayConnection = await client.CreateConnectionAsync();
    
        // Run two concurrent loops on the connection. One 
        // reads input from the console and writes it to the connection 
        // with a stream writer. The other reads lines of input from the 
        // connection with a stream reader and writes them to the console. 
        // Entering a blank line shuts down the write task after 
        // sending it to the server. The server then cleanly shuts down
        // the connection, which terminates the read task.
    
        var reads = Task.Run(async () => {
            // Initialize the stream reader over the connection.
            var reader = new StreamReader(relayConnection);
            var writer = Console.Out;
            do
            {
                // Read a full line of UTF-8 text up to newline.
                string line = await reader.ReadLineAsync();
                // If the string is empty or null, you are done.
                if (String.IsNullOrEmpty(line))
                    break;
                // Write to the console.
                await writer.WriteLineAsync(line);
            }
            while (true);
        });
    
        // Read from the console and write to the hybrid connection.
        var writes = Task.Run(async () => {
            var reader = Console.In;
            var writer = new StreamWriter(relayConnection) { AutoFlush = true };
            do
            {
                // Read a line from the console.
                string line = await reader.ReadLineAsync();
                // Write the line out, also when it's empty.
                await writer.WriteLineAsync(line);
                // Quit when the line is empty,
                if (String.IsNullOrEmpty(line))
                    break;
            }
            while (true);
        });
    
        // Wait for both tasks to finish.
        await Task.WhenAll(reads, writes);
        await relayConnection.CloseAsync(CancellationToken.None);
    }
    
  4. Program 类的 Main 方法中添加以下代码行。Add the following line of code to the Main method in the Program class.

    RunAsync().GetAwaiter().GetResult();
    

    Program.cs 应如下所示:The Program.cs should look like this:

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.Relay;
    
    namespace Client
    {
        class Program
        {
            private const string RelayNamespace = "{RelayNamespace}.servicebus.chinacloudapi.cn";
            private const string ConnectionName = "{HybridConnectionName}";
            private const string KeyName = "{SASKeyName}";
            private const string Key = "{SASKey}";
    
            static void Main(string[] args)
            {
                RunAsync().GetAwaiter().GetResult();
            }
    
            private static async Task RunAsync()
            {
                Console.WriteLine("Enter lines of text to send to the server with ENTER");
    
                // Create a new hybrid connection client.
                var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(KeyName, Key);
                var client = new HybridConnectionClient(new Uri(String.Format("sb://{0}/{1}", RelayNamespace, ConnectionName)), tokenProvider);
    
                // Initiate the connection.
                var relayConnection = await client.CreateConnectionAsync();
    
                // Run two concurrent loops on the connection. One 
                // reads input from the console and then writes it to the connection 
                // with a stream writer. The other reads lines of input from the 
                // connection with a stream reader and then writes them to the console. 
                // Entering a blank line shuts down the write task after 
                // sending it to the server. The server then cleanly shuts down
                // the connection, which terminates the read task.
    
                var reads = Task.Run(async () => {
                    // Initialize the stream reader over the connection.
                    var reader = new StreamReader(relayConnection);
                    var writer = Console.Out;
                    do
                    {
                        // Read a full line of UTF-8 text up to newline.
                        string line = await reader.ReadLineAsync();
                        // If the string is empty or null, you are done.
                        if (String.IsNullOrEmpty(line))
                            break;
                        // Write to the console.
                        await writer.WriteLineAsync(line);
                    }
                    while (true);
                });
    
                // Read from the console and write to the hybrid connection.
                var writes = Task.Run(async () => {
                    var reader = Console.In;
                    var writer = new StreamWriter(relayConnection) { AutoFlush = true };
                    do
                    {
                        // Read a line from the console.
                        string line = await reader.ReadLineAsync();
                        // Write the line out, also when it's empty.
                        await writer.WriteLineAsync(line);
                        // Quit when the line is empty.
                        if (String.IsNullOrEmpty(line))
                            break;
                    }
                    while (true);
                });
    
                // Wait for both tasks to finish.
                await Task.WhenAll(reads, writes);
                await relayConnection.CloseAsync(CancellationToken.None);
            }
        }
    }
    

运行应用程序Run the applications

  1. 运行服务器应用程序。Run the server application.

  2. 运行客户端应用程序并输入一些文本。Run the client application and enter some text.

  3. 确保服务器应用程序控制台显示了客户端应用程序中输入的文本。Ensure that the server application console displays the text that was entered in the client application.

    控制台窗口测试服务器和客户端应用程序。

祝贺你,你已创建了一个完整的混合连接应用程序!Congratulations, you've created a complete Hybrid Connections application!

后续步骤Next steps

在本快速入门中,你创建了 .NET 客户端和服务器应用程序,此类程序使用 WebSocket 来发送和接收消息。In this quickstart, you created .NET client and server applications that used WebSockets to send and receive messages. Azure 中继的混合连接功能也支持使用 HTTP 发送和接收消息。The Hybrid Connections feature of Azure Relay also supports using HTTP to send and receive messages. 若要了解如何将 HTTP 与 Azure 中继混合连接配合使用,请参阅 HTTP 快速入门To learn how to use HTTP with Azure Relay Hybrid Connections, see the HTTP quickstart.

在本快速入门中,你使用了 .NET Framework 来创建客户端和服务器应用程序。In this quickstart, you used .NET Framework to create client and server applications. 若要了解如何使用 Node.js 编写客户端和服务器应用程序,请参阅 Node.js WebSocket 快速入门Node.js HTTP 快速入门To learn how to write client and server applications using Node.js, see the Node.js WebSockets quickstart or the Node.js HTTP quickstart.