您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

快速入门:从控制台应用广播实时消息Quickstart: Broadcast real-time messages from console app

Azure SignalR 服务提供了 REST API 来支持服务器到客户端通信方案,例如广播。Azure SignalR Service provides REST API to support server to client communication scenarios, such as broadcasting. 你可以选择可以进行 REST API 调用的任何编程语言。You can choose any programming language that can make REST API call. 你可以将消息发布到所有已连接的客户端、通过名称指定的特定客户端或者一组客户端。You can post messages to all connected clients, a specific client by name, or a group of clients.

在本快速入门中,你将学习如何从命令行应用使用 C# 将消息发送到已连接的客户端应用。In this quickstart, you will learn how to send messages from a command-line app to connected client apps in C#.

先决条件Prerequisites

本快速入门可以在 macOS、Windows 或 Linux 上运行。This quickstart can be run on macOS, Windows, or Linux.

如果还没有 Azure 订阅,可以在开始前创建一个免费帐户If you don't have an Azure subscription, create a free account before you begin.

登录 AzureSign in to Azure

使用 Azure 帐户登录到 https://portal.azure.com/ 的 Azure 门户。Sign in to the Azure portal at https://portal.azure.com/ with your Azure account.

创建 Azure SignalR 服务实例Create an Azure SignalR Service instance

你的应用程序将连接到 Azure 中的 SignalR 服务实例。Your application will connect to a SignalR Service instance in Azure.

  1. 选择 Azure 门户左上角的“新建”按钮。Select the New button found on the upper left-hand corner of the Azure portal. 在“新建”屏幕中,在搜索框中键入“SignalR 服务” ,然后按 Enter。In the New screen, type SignalR Service in the search box and press enter.

    搜索“SignalR 服务”

  2. 从搜索结果中选择“SignalR 服务” ,然后选择“创建” 。Select SignalR Service from the search results, then select Create.

  3. 输入以下设置。Enter the following settings.

    设置Setting 建议的值Suggested value 说明Description
    资源名称Resource name 全局唯一名称Globally unique name 用于标识新的 SignalR 服务实例的名称。Name that identifies your new SignalR Service instance. 有效的字符是 a-z0-9-Valid characters are a-z, 0-9, and -.
    订阅Subscription 订阅Your subscription 在其下创建此新 SignalR 服务实例的订阅。The subscription under which this new SignalR Service instance is created.
    资源组Resource Group myResourceGroupmyResourceGroup 要在其中创建 SignalR 服务实例的新资源组的名称。Name for the new resource group in which to create your SignalR Service instance.
    位置Location 美国西部West US 选择你附近的区域Choose a region near you.
    定价层Pricing tier 免费Free 免费试用 Azure SignalR 服务。Try Azure SignalR Service for free.
    单位计数Unit count 不适用Not applicable 单位计数指定 SignalR 服务实例可以接受的连接数。Unit count specifies how many connections your SignalR Service instance can accept. 它只能在标准层中配置。It is only configurable in the Standard tier.
    服务模式Service mode 无服务器Serverless 用于 Azure Functions 或 REST API。For use with Azure Functions or REST API.

    创建 SignalR 服务

  4. 选择“创建” 以开始部署 SignalR 服务实例。Select Create to start deploying the SignalR Service instance.

  5. 部署该实例后,在门户中打开它并找到其“设置”页。After the instance is deployed, open it in the portal and locate its Settings page. 仅当通过 Azure Functions 绑定或 REST API 使用 Azure SignalR 服务时,才将“服务模式”设置更改为“无服务器” 。Change the Service Mode setting to Serverless only if you are using Azure SignalR Service through Azure Functions binding or REST API. 否则,将其保留在“经典” 或“默认” 中。Leave it in Classic or Default otherwise.

克隆示例应用程序Clone the sample application

在该服务进行部署时,让我们先去准备代码。While the service is deploying, let's switch to prepare the code. 克隆来自 GitHub 的示例应用,设置 SignalR 服务连接字符串,并在本地运行该应用程序。Clone the sample app from GitHub, set the SignalR Service connection string, and run the application locally.

  1. 打开 git 终端窗口。Open a git terminal window. 切换到要克隆示例项目的文件夹。Change to a folder where you want to clone the sample project.

  2. 运行下列命令以克隆示例存储库。Run the following command to clone the sample repository. 此命令在计算机上创建示例应用程序的副本。This command creates a copy of the sample app on your computer.

    git clone https://github.com/aspnet/AzureSignalR-samples.git
    

生成并运行示例Build and run the sample

此示例是一个控制台应用,展示了如何使用 Azure SignalR 服务。This sample is a console app showing the use of Azure SignalR Service. 它提供了两种模式:It provides two modes:

  • 服务器模式:使用简单的命令调用 Azure SignalR 服务 REST API。Server Mode: use simple commands to call Azure SignalR Service REST API.
  • 客户端模式:连接到 Azure SignalR 服务,并从服务器接收消息。Client Mode: connect to Azure SignalR Service and receive messages from server.

另外,你还可以查明如何生成访问令牌来向 Azure SignalR 服务证明身份。Also you can find how to generate an access token to authenticate with Azure SignalR Service.

生成可执行文件Build the executable file

我们使用 macOS osx.10.13-x64 作为示例。We use macOS osx.10.13-x64 as example. 你可以找到有关如何在其他平台上进行生成的参考资料You can find reference on how to build on other platforms.

cd AzureSignalR-samples/samples/Serverless/

dotnet publish -c Release -r osx.10.13-x64

启动客户端Start a client

cd bin/Release/netcoreapp2.1/osx.10.13-x64/

Serverless client <ClientName> -c "<ConnectionString>" -h <HubName>

启动服务器Start a server

cd bin/Release/netcoreapp2.1/osx.10.13-x64/

Serverless server -c "<ConnectionString>" -h <HubName>

运行示例而不发布Run the sample without publishing

你还可以运行以下命令来启动服务器或客户端You can also run the command below to start a server or client

# Start a server
dotnet run -- server -c "<ConnectionString>" -h <HubName>

# Start a client
dotnet run -- client <ClientName> -c "<ConnectionString>" -h <HubName>

使用 user-secrets 指定连接字符串Use user-secrets to specify Connection String

你可以运行示例的根目录中的 dotnet user-secrets set Azure:SignalR:ConnectionString "<ConnectionString>"You can run dotnet user-secrets set Azure:SignalR:ConnectionString "<ConnectionString>" in the root directory of the sample. 之后,将不再需要选项 -c "<ConnectionString>"After that, you don't need the option -c "<ConnectionString>" anymore.

使用情况Usage

在服务器启动后,使用以下命令发送消息:After the server started, use the command to send message:

send user <User Id>
send users <User List>
send group <Group Name>
send groups <Group List>
broadcast

可以启动具有不同客户端名称的多个客户端。You can start multiple clients with different client names.

与第三方服务集成 Integration with third-party services

Azure SignalR 服务允许第三方服务与系统集成。The Azure SignalR service allows third-party services to integrate with the system.

定义技术规范Definition of technical specifications

下表显示到目前为止受支持的 REST API 的所有版本。The following table shows all the versions of the REST APIs supported to date. 也可找到每个特定版本的定义文件You can also find the definition file for each specific version

版本Version API 状态API State Door 特定Specific
1.0-preview 可用Available 50025002 SwaggerSwagger
1.0 可用Available 标准Standard SwaggerSwagger

适用于每个特定版本的 API 的列表在下表中提供。The list of available APIs for each specific version is available in the following list.

APIAPI 1.0-preview 1.0
广播到所有对象Broadcast to all
广播到组Broadcast to a group
广播到某些组Broadcast to some groups (已弃用) (Deprecated) N / A
发送到特定用户Send to specific users
发送到某些用户Send to some users (已弃用) (Deprecated) N / A
将用户添加到组Adding a user to a group N / A
从组中删除用户Removing a user from a group N / A

广播到所有人Broadcast to everyone

版本Version API HTTP 方法API HTTP Method 请求 URLRequest URL 请求正文Request body
1.0-preview POST https://<instance-name>.service.signalr.net:5002/api/v1-preview/hub/<hub-name> {"target": "<method-name>", "arguments": [...]}
1.0 POST https://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name> 同上Same as above

广播到组Broadcast to a group

版本Version API HTTP 方法API HTTP Method 请求 URLRequest URL 请求正文Request body
1.0-preview POST https://<instance-name>.service.signalr.net:5002/api/v1-preview/hub/<hub-name>/group/<group-name> {"target": "<method-name>", "arguments": [...]}
1.0 POST https://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name> 同上Same as above

发送到特定用户Sending to specific users

版本Version API HTTP 方法API HTTP Method 请求 URLRequest URL 请求正文Request body
1.0-preview POST https://<instance-name>.service.signalr.net:5002/api/v1-preview/hub/<hub-name>/user/<user-id> {"target": "<method-name>", "arguments": [...]}
1.0 POST https://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/users/<user-id> 同上Same as above

将用户添加到组Adding a user to a group

版本Version API HTTP 方法API HTTP Method 请求 URLRequest URL
1.0 PUT https://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>/users/<userid>

从组中删除用户Removing a user from a group

版本Version API HTTP 方法API HTTP Method 请求 URLRequest URL
1.0 DELETE https://<instance-name>.service.signalr.net/api/v1/hubs/<hub-name>/groups/<group-name>/users/<userid>

清理资源Clean up resources

如果不打算继续使用此应用,请按照以下步骤删除本快速入门中创建的所有资源,以免产生任何费用:If you're not going to continue to use this app, delete all resources created by this quickstart with the following steps so you don't incur any charges:

  1. 在 Azure 门户的最左侧选择“资源组”,,然后选择创建的资源组。 In the Azure portal, select Resource groups on the far left, and then select the resource group you created. 或者,可以使用搜索框按名称查找资源组。Alternatively, you may use the search box to find the resource group by its name.

  2. 在打开的窗口中选择资源组,然后单击“删除资源组”。 In the window that opens, select the resource group, and then click Delete resource group.

  3. 在新窗口中键入要删除的资源组的名称,然后单击“删除” 。In the new window, type the name of the resource group to delete, and then click Delete.

后续步骤Next steps

在本快速入门中,你学习了如何使用 REST API 从 SignalR 服务向客户端广播实时消息。In this quickstart, you learned how to use REST API to broadcast real-time message from SignalR Service to clients. 接下来,了解有关如何使用基于 REST API 构建的 SignalR 服务绑定开发和部署 Azure Functions 的更多信息。Next, learn more about how to develop and deploy Azure Functions with SignalR Service binding, which is built on top of REST API.