Skip to content

Commit 78db722

Browse files
Merge pull request #233279 from JialinXin/patch-3
[WebPubSub] Fix a name
2 parents 3c61c35 + d1f288b commit 78db722

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

articles/azure-web-pubsub/reference-functions-bindings.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: vicancy
55
ms.author: lianwei
66
ms.service: azure-web-pubsub
77
ms.topic: conceptual
8-
ms.date: 07/04/2022
8+
ms.date: 04/04/2023
99
---
1010

1111
# Azure Web PubSub trigger and bindings for Azure Functions
@@ -40,7 +40,7 @@ Working with the trigger and bindings requires you reference the appropriate pac
4040
> Install the client library from [NuGet](https://www.nuget.org/) with specified package and version.
4141
>
4242
> ```bash
43-
> func extensions install --package Microsoft.Azure.WebJobs.Extensions.WebPubSub --version 1.0.0
43+
> func extensions install --package Microsoft.Azure.WebJobs.Extensions.WebPubSub
4444
> ```
4545
4646
[NuGet package]: https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.WebPubSub
@@ -79,34 +79,22 @@ Use the function trigger to handle requests from Azure Web PubSub service.
7979
```cs
8080
[FunctionName("WebPubSubTrigger")]
8181
public static void Run(
82-
[WebPubSubTrigger("<hub>", WebPubSubEventType.User, "message")]
83-
UserEventRequest request,
84-
WebPubSubConnectionContext context,
85-
string data,
86-
WebPubSubDataType dataType)
82+
[WebPubSubTrigger("<hub>", WebPubSubEventType.User, "message")] UserEventRequest request)
8783
{
88-
Console.WriteLine($"Request from: {context.UserId}");
89-
Console.WriteLine($"Request message data: {data}");
90-
Console.WriteLine($"Request message dataType: {dataType}");
84+
Console.WriteLine($"Request from: {request.ConnectionContext.UserId}");
85+
Console.WriteLine($"Request message data: {request.Data}");
86+
Console.WriteLine($"Request message dataType: {request.DataType}");
9187
}
9288
```
9389
9490
`WebPubSubTrigger` binding also supports return value in synchronize scenarios, for example, system `Connect` and user event, when server can check and deny the client request, or send messages to the caller directly. `Connect` event respects `ConnectEventResponse` and `EventErrorResponse`, and user event respects `UserEventResponse` and `EventErrorResponse`, rest types not matching current scenario will be ignored. And if `EventErrorResponse` is returned, service will drop the client connection.
9591
9692
```cs
97-
[FunctionName("WebPubSubTriggerReturnValue")]
98-
public static MessageResponse Run(
99-
[WebPubSubTrigger("<hub>", WebPubSubEventType.User, "message")]
100-
UserEventRequest request,
101-
ConnectionContext context,
102-
string data,
103-
WebPubSubDataType dataType)
93+
[FunctionName("WebPubSubTriggerReturnValueFunction")]
94+
public static UserEventResponse Run(
95+
[WebPubSubTrigger("hub", WebPubSubEventType.User, "message")] UserEventRequest request)
10496
{
105-
return new UserEventResponse
106-
{
107-
Data = BinaryData.FromString("ack"),
108-
DataType = WebPubSubDataType.Text
109-
};
97+
return request.CreateResponse(BinaryData.FromString("ack"), WebPubSubDataType.Text);
11098
}
11199
```
112100
@@ -163,7 +151,7 @@ Here's an `WebPubSubTrigger` attribute in a method signature:
163151
```csharp
164152
[FunctionName("WebPubSubTrigger")]
165153
public static void Run([WebPubSubTrigger("<hub>", <WebPubSubEventType>, "<event-name>")]
166-
WebPubSubConnectionContext context, ILogger log)
154+
WebPubSubConnectionContext context, ILogger log)
167155
{
168156
...
169157
}
@@ -203,6 +191,9 @@ In weakly typed language like JavaScript, `name` in `function.json` will be used
203191
|clientCertificates|`IList<ClientCertificate>`|A list of certificate thumbprint from clients in system `connect` request|-|
204192
|reason|`string`|Reason in system `disconnected` request|-|
205193

194+
> [!IMPORTANT]
195+
> In C#, multiple types supported parameter __MUST__ be put in the first, i.e. `request` or `data` that other than the default `BinaryData` type to make the function binding correctly.
196+
206197
### Return response
207198

208199
`WebPubSubTrigger` will respect customer returned response for synchronous events of `connect` and user event. Only matched response will be sent back to service, otherwise, it will be ignored. Besides, `WebPubSubTrigger` return object supports users to `SetState()` and `ClearStates()` to manage the metadata for the connection. And the extension will merge the results from return value with the original ones from request `WebPubSubConnectionContext.States`. Value in existing key will be overwrite and value in new key will be added.

0 commit comments

Comments
 (0)