الشروع في العمل باستخدام Relay Hybrid Connections WebSockets في .NET

في هذا التشغيل السريع، يمكنك إنشاء تطبيقات .NET المرسل والمستقبل التي ترسل وتتلقى الرسائل باستخدام اتصالات مختلطة WebSockets في Azure Relay. للتعرف على Azure Relay بشكل عام، راجع Azure Relay.

في هذه البداية السريعة، يمكنك اتخاذ الخطوات التالية:

  1. إنشاء مساحة اسم ترحيل باستخدام مدخل Azure.
  2. قم بإنشاء اتصال مختلط في مساحة الاسم هذه باستخدام مدخل Azure.
  3. اكتب تطبيق وحدة تحكم خادم (مستمع) لتلقي الرسائل.
  4. اكتب تطبيق وحدة تحكم عميل (مرسل) لإرسال الرسائل.
  5. تشغيل التطبيقات.

المتطلبات الأساسية

لاستكمال هذا البرنامج التعليمي، ستحتاج إلى المتطلبات الأساسية التالية:

إنشاء مساحة اسم

  1. سجل الدخول إلى مدخل Azure.

  2. حدد Create a resource. ومن ثم، حدد ترحيل>التكامل. إذا لم تطلع على الترحيل المذكور في القائمة، فحدد الاطلاع على الكل في الزاوية العلوية اليمنى.

  3. حدد إنشاء، ثم أدخل اسم namespace في حقل الاسم. يتحقق مدخل Azure لمعرفة ما إذا كان الاسم متوفراً.

  4. بالنسبة إلى Subscription، اختر اشتراك Azure لإنشاءnamespace.

  5. بالنسبة لـ "مجموعة الموارد"، اختر مجموعة موارد موجودة حيث توجد namespace، أو إنشاء مجموعة موارد جديدة.

  6. حدد البلد أو المنطقة التي يجب استضافة namespace فيها.

    Create namespace

  7. حدد Create. يقوم النظام الآن بإنشاء namespace وتمكينها. بعد بضع دقائق، يُوفر النظام الموارد لحسابك.

الحصول على بيانات اعتماد الإدارة

  1. حدد جميع الموارد، ثم حدد اسم namespace الذي تم إنشاؤه حديثاً.

  2. حدد سياسات الوصول المشترك.

  3. في إطارسياسات الوصول المشترك، حدد RootManageSharedAccessKey.

  4. في إطار سياسةSAS: RootManageSharedAccessKey، حدد زر نسخ سلسلة الاتصال الأساسية. يقوم هذا الإجراء بنسخ سلسلة الاتصال إلى الحافظة لاستخدامها لاحقاً. لصق هذه القيمة في المفكرة أو بعض المواقع المؤقتة الأخرى.

  5. كرر الخطوة السابقة، وهي نسخ قيمة المفتاح الأساسي ولصقها إلى موقع مؤقت للاستخدام لاحقاً.

    connection-string

إنشاء اتصال مختلط

تأكد من أنك قمت بالفعل بإنشاء مساحة اسم ترحيل.

  1. تسجيل الدخول إلى ⁧مدخل Microsoft Azure⁧.

  2. في القائمة اليمنى، حدد ⁧⁩جميع الموارد⁧⁩.

  3. حدد مساحة الاسم حيث تريد إنشاء الاتصال المختلط. في هذه الحالة ، هو mynewns.

  4. ضمن مساحة اسم الترحيل، حدد الاتصالات المختلطة.

    Create a hybrid connection

  5. في نافذة نظرة عامة على مساحة الاسم، حدد + اتصال مختلط

    Select the hybrid connection

  6. ضمن إنشاء اتصال مختلط، أدخل قيمة لاسم الاتصال المختلط. اترك القيم الافتراضية الأخرى.

    Select New

  7. حدد Create.

إنشاء تطبيق خادم (مستمع)

في Visual Studio ، اكتب تطبيق وحدة تحكم C # للاستماع إلى الرسائل وتلقيها من الترحيل.

قم بإنشاء تطبيق وحدة تحكم

في Visual Studio، قم بإنشاء مشروع تطبيق وحدة تحكم (.NET Framework) جديد.

إضافة حزمة Relay NuGet

  1. انقر بزر الماوس الأيمن فوق المشروع الذي تم إنشاؤه حديثا، ثم حدد إدارة حزم NuGet.
  2. حدد خيار تضمين الإصدار التجريبي .
  3. حدد استعراض، ثم ابحث عن Microsoft.Azure.Relay. في نتائج البحث، حدد Microsoft Azure Relay.
  4. بالنسبة للإصدار، حدد 2.0.0-preview1-20180523.
  5. حدد تثبيت لإكمال التثبيت. أغلق مربع الحوار.

كتابة التعليمات البرمجية لتلقي الرسائل

  1. في الجزء العلوي من ملف .cs البرنامج، استبدل العبارات الموجودة using بالعبارات التالية using :

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Net;
    using Microsoft.Azure.Relay;
    
  2. أضف ثوابت إلى Program الفصل للحصول على تفاصيل الاتصال المختلط. استبدل العناصر النائبة بين قوسين بالقيم التي حصلت عليها عند إنشاء الاتصال المختلط. تأكد من استخدام اسم مساحة الاسم المؤهل بالكامل.

    private const string RelayNamespace = "{RelayNamespace}.servicebus.windows.net";
    private const string ConnectionName = "{HybridConnectionName}";
    private const string KeyName = "{SASKeyName}";
    private const string Key = "{SASKey}";
    
  3. أضف الطريقة ProcessMessagesOnConnection إلى الفصل Program :

    // 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 :

    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. إضافة السطر التالي من التعليمات البرمجية إلى الأسلوب Main في الفصل Program :

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

    يجب أن يبدو ملف .cs البرنامج المكتمل كما يلي:

    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.windows.net";
            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);
            }
        }
    }
    

إنشاء تطبيق عميل (مرسل)

في Visual Studio، اكتب تطبيق وحدة تحكم C# لإرسال رسائل إلى الترحيل.

قم بإنشاء تطبيق وحدة تحكم

في Visual Studio، قم بإنشاء مشروع تطبيق وحدة تحكم (.NET Framework) جديد.

إضافة حزمة Relay NuGet

  1. انقر بزر الماوس الأيمن فوق المشروع الذي تم إنشاؤه حديثا، ثم حدد إدارة حزم NuGet.
  2. حدد استعراض، ثم ابحث عن Microsoft.Azure.Relay. في نتائج البحث، حدد Microsoft Azure Relay.
  3. حدد تثبيت لإكمال التثبيت. أغلق مربع الحوار.

كتابة التعليمات البرمجية لإرسال الرسائل

  1. في الجزء العلوي من ملف .cs البرنامج، استبدل العبارات الموجودة using بالعبارات التالية using :

    using System;
    using System.IO;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.Relay;
    
  2. أضف ثوابت إلى Program الفصل للحصول على تفاصيل الاتصال المختلط. استبدل العناصر النائبة بين قوسين بالقيم التي حصلت عليها عند إنشاء الاتصال المختلط. تأكد من استخدام اسم مساحة الاسم المؤهل بالكامل.

    // replace {RelayNamespace} with the name of your namespace
    private const string RelayNamespace = "{RelayNamespace}.servicebus.windows.net";
    
    // replace {HybridConnectionName} with the name of your hybrid connection
    private const string ConnectionName = "{HybridConnectionName}";
    
    // replace {SAKKeyName} with the name of your Shared Access Policies key, which is RootManageSharedAccessKey by default
    private const string KeyName = "{SASKeyName}";
    
    // replace {SASKey} with the primary key of the namespace you saved earlier
    private const string Key = "{SASKey}";
    
  3. إضافة الطريقة التالية إلى الفصل Program :

    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. إضافة السطر التالي من التعليمات البرمجية إلى الأسلوب Main في الفصل Program .

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

    يجب أن يبدو .cs البرنامج كما يلي:

    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.windows.net";
            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);
            }
        }
    }
    

تشغيل التطبيقات

  1. قم بتشغيل تطبيق الخادم.

  2. قم بتشغيل تطبيق العميل وأدخل بعض النص.

  3. تأكد من أن وحدة تحكم تطبيق الخادم تعرض النص الذي تم إدخاله في تطبيق العميل.

    Console windows testing both the server and client applications.

تهانينا ، لقد قمت بإنشاء تطبيق اتصالات مختلطة كامل!

الخطوات التالية

في هذه البداية السريعة، قمت بإنشاء عميل .NET وتطبيقات الخادم التي استخدمت WebSockets لإرسال الرسائل وتلقيها. تدعم ميزة "الاتصالات المختلطة" في Azure Relay أيضا استخدام HTTP لإرسال الرسائل وتلقيها. لمعرفة كيفية استخدام HTTP مع اتصالات Azure Relay المختلطة، راجع بدء تشغيل HTTP السريع.

في هذه البداية السريعة، استخدمت .NET Framework لإنشاء تطبيقات العميل والخادم. لمعرفة كيفية كتابة تطبيقات العميل والخادم باستخدام Node.js، راجع Node.js WebSockets السريع أو Node.js HTTP السريع.