mcpField
A
azure-messaging-webpubsubservice-py

azure-messaging-webpubsubservice-py

github.com/sickn33开发48.7k46 次安装今天更新
代码仓库

关于 azure-messaging-webpubsubservice-py

面向 Python 开发者的 Azure Web PubSub Service SDK 技能,用于管理 WebSocket 实时消息。它说明服务端消息发送、连接与群组管理,以及 Python 客户端连接操作。

主要功能

  1. 01生成客户端访问令牌
  2. 02向所有用户、用户、群组或连接发送消息
  3. 03管理用户与连接的群组成员关系
  4. 04检查连接、用户和群组状态
  5. 05支持异步服务端客户端

适用场景

  1. 01向实时连接广播通知
  2. 02向特定用户或群组发送消息
  3. 03管理 WebSocket 客户端的群组权限
  4. 04构建 Python WebSocket 消息客户端

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-messaging-webpubsubservice-py

安装后在 Claude Code 中输入/azure-messaging-webpubsubservice-py确认已加载。

详细介绍

Azure Web PubSub Service SDK for Python

通过 WebSocket 连接实现大规模实时消息传递。

安装

# Service SDK (server-side)
pip install azure-messaging-webpubsubservice

# Client SDK (for Python WebSocket clients)
pip install azure-messaging-webpubsubclient

环境变量

AZURE_WEBPUBSUB_CONNECTION_STRING=Endpoint=https://<name>.webpubsub.azure.com;AccessKey=...
AZURE_WEBPUBSUB_HUB=my-hub

服务客户端(服务器端)

身份验证
import os
from azure.messaging.webpubsubservice import WebPubSubServiceClient

# Connection string
client = WebPubSubServiceClient.from_connection_string(
    connection_string=os.environ["AZURE_WEBPUBSUB_CONNECTION_STRING"],
    hub="my-hub"
)

# Entra ID
from azure.identity import DefaultAzureCredential

client = WebPubSubServiceClient(
    endpoint="https://<name>.webpubsub.azure.com",
    hub="my-hub",
    credential=DefaultAzureCredential()
)
生成客户端访问令牌
# Token for anonymous user
token = client.get_client_access_token()
# Treat token["url"] as a bearer secret; return it only to the authenticated intended client.

# Token with user ID
token = client.get_client_access_token(
    user_id="user123",
    roles=["webpubsub.sendToGroup.my-group", "webpubsub.joinLeaveGroup.my-group"]
)

# Token with groups
token = client.get_client_access_token(
    user_id="user123",
    groups=["group1", "group2"]
)
向所有客户端发送消息
# Send text
client.send_to_all(message="Hello everyone!", content_type="text/plain")

# Send JSON
client.send_to_all(
    message={"type": "notification", "data": "Hello"},
    content_type="application/json"
)
向用户发送消息
client.send_to_user(
    user_id="user123",
    message="Hello user!",
    content_type="text/plain"
)
向组发送消息
client.send_to_group(
    group="my-group",
    message="Hello group!",
    content_type="text/plain"
)
向连接发送消息
client.send_to_connection(
    connection_id="abc123",
    message="Hello connection!",
    content_type="text/plain"
)
组管理
# Add user to group
client.add_user_to_group(group="my-group", user_id="user123")

# Remove user from group
client.remove_user_from_group(group="my-group", user_id="user123")

# Add connection to group
client.add_connection_to_group(group="my-group", connection_id="abc123")

# Remove connection from group
client.remove_connection_from_group(group="my-group", connection_id="abc123")
连接管理
# Check if connection exists
exists = client.connection_exists(connection_id="abc123")

# Check if user has connections
exists = client.user_exists(user_id="user123")

# Check if group has connections
exists = client.group_exists(group="my-group")

# Close connection
client.close_connection(connection_id="abc123", reason="Session ended")

# Close all connections for user
client.close_all_connections(user_id="user123")
授予/撤销权限
from azure.messaging.webpubsubservice import WebPubSubServiceClient

# Grant permission
client.grant_permission(
    permission="joinLeaveGroup",
    connection_id="abc123",
    target_name="my-group"
)

# Revoke permission
client.revoke_permission(
    permission="joinLeaveGroup",
    connection_id="abc123",
    target_name="my-group"
)

# Check permission
has_permission = client.check_permission(
    permission="joinLeaveGroup",
    connection_id="abc123",
    target_name="my-group"
)

客户端 SDK(Python WebSocket 客户端)

from azure.messaging.webpubsubclient import WebPubSubClient
from azure.messaging.webpubsubclient.models import CallbackType, WebPubSubDataType

with WebPubSubClient(token["url"]) as ws_client:
    ws_client.subscribe(CallbackType.CONNECTED, lambda e: print("Connected"))
    ws_client.subscribe(CallbackType.GROUP_MESSAGE, lambda e: print("Group message received"))
    ws_client.join_group("my-group")
    ws_client.send_to_group("my-group", "Hello from Python!", WebPubSubDataType.TEXT)

在服务器上授权令牌的签发;根据经过身份验证的会话派生用户 ID 和允许访问的组,切勿使用不受信任的请求字段。使用固定 SDK 支持的较短明确过期时间,避免记录访问 URL 或消息正文,并在使用完毕后关闭服务客户端和凭据。这些发送操作和组变更会影响实时客户端;示例请使用测试中心。

异步服务客户端

from azure.messaging.webpubsubservice.aio import WebPubSubServiceClient
from azure.identity.aio import DefaultAzureCredential

async def broadcast():
    credential = DefaultAzureCredential()
    client = WebPubSubServiceClient(
        endpoint="https://<name>.webpubsub.azure.com",
        hub="my-hub",
        credential=credential
    )
    
    await client.send_to_all("Hello async!", content_type="text/plain")
    
    await client.close()
    await credential.close()

客户端操作

操作描述
get_client_access_token生成 WebSocket 连接 URL
send_to_all向所有连接广播
send_to_user向指定用户发送
send_to_group向组成员发送
send_to_connection向指定连接发送
add_user_to_group将用户添加到组
remove_user_from_group从组中移除用户
close_connection断开客户端连接
connection_exists检查连接状态

最佳实践

  1. 使用角色限制客户端权限
  2. 使用组进行定向消息传递
  3. 生成短期令牌以确保安全
  4. 使用用户 ID向跨连接的用户发送消息
  5. 在客户端应用程序中处理重新连接
  6. 对结构化数据使用 JSON 内容类型
  7. 使用原因妥善关闭连接

适用场景

当任务与概述中描述的工作流或操作明确匹配时,可使用此技能。

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为环境特定验证、测试或专家审查的替代品。
  • 如果缺少必要的输入、权限、安全边界或成功标准,请停止并请求澄清。

评论

支持 Markdown