Skip to content

Commit 61fbb7d

Browse files
authored
update
1 parent a02d6f0 commit 61fbb7d

File tree

1 file changed

+78
-65
lines changed

1 file changed

+78
-65
lines changed

articles/azure-signalr/signalr-tutorial-authenticate-azure-functions.md

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,19 @@ Your application will access an Azure SignalR Service instance. Use the followin
7676

7777
1. Run the following command in your terminal to create a new JavaScript Functions project:
7878

79-
# [Model v4](#tab/nodejs-v4)
79+
# [Model v4](#tab/nodejs-v4)
8080

81-
```bash
82-
func init --worker-runtime node --language javascript --name my-app --model V4
83-
```
81+
```bash
82+
func init --worker-runtime node --language javascript --name my-app --model V4
83+
```
8484

85-
# [Model v3](#tab/nodejs-v3)
85+
# [Model v3](#tab/nodejs-v3)
8686

87-
```bash
88-
func init --worker-runtime node --language javascript --name my-app --model V3
89-
```
87+
```bash
88+
func init --worker-runtime node --language javascript --name my-app --model V3
89+
```
90+
91+
---
9092

9193
By default, the generated project includes a _host.json_ file that contains the extension bundles that include the SignalR extension. For more information about extension bundles, see [Register Azure Functions binding extensions](../azure-functions/functions-bindings-register.md#extension-bundles).
9294

@@ -211,79 +213,90 @@ When the chat app first opens in the browser, it requires valid connection crede
211213
212214
This function takes the SignalR connection information from the input binding and returns it to the client in the HTTP response body. The SignalR client uses this information to connect to the Azure SignalR Service instance.
213215
216+
---
217+
214218
[Having issues? Let us know.](https://aka.ms/asrs/qsauth)
215219
216220
### Create a function to send chat messages
217221
218222
The web app also requires an HTTP API to send chat messages. Create an HTTP trigger function that sends messages to all connected clients that use Azure SignalR Service:
219223
220-
1. From the root project folder, create an HTTP trigger function named `sendMessage` from the template by using the following command:
224+
# [Model v4](#tab/nodejs-v4)
225+
1. From the root project folder, create an HTTP trigger function named `sendMessage` from the template by using the following command:
221226
222-
```bash
223-
func new --name sendMessage --template "Http trigger"
224-
```
227+
```bash
228+
func new --name sendMessage --template "Http trigger"
229+
```
225230
226-
1. To configure bindings for the function, replace the content of _sendMessage/function.json_ with the following code:
231+
# [Model v3](#tab/nodejs-v3)
227232
228-
```json
229-
{
230-
"disabled": false,
231-
"bindings": [
232-
{
233-
"authLevel": "anonymous",
234-
"type": "httpTrigger",
235-
"direction": "in",
236-
"name": "req",
237-
"route": "messages",
238-
"methods": ["post"]
239-
},
240-
{
241-
"type": "http",
242-
"direction": "out",
243-
"name": "res"
244-
},
245-
{
246-
"type": "signalR",
247-
"name": "$return",
248-
"hubName": "default",
249-
"direction": "out"
250-
}
251-
]
252-
}
253-
```
233+
1. From the root project folder, create an HTTP trigger function named `sendMessage` from the template by using the following command:
254234
255-
The preceding code makes two changes to the original file:
235+
```bash
236+
func new --name sendMessage --template "Http trigger"
237+
```
238+
239+
1. To configure bindings for the function, replace the content of _sendMessage/function.json_ with the following code:
240+
241+
```json
242+
{
243+
"disabled": false,
244+
"bindings": [
245+
{
246+
"authLevel": "anonymous",
247+
"type": "httpTrigger",
248+
"direction": "in",
249+
"name": "req",
250+
"route": "messages",
251+
"methods": ["post"]
252+
},
253+
{
254+
"type": "http",
255+
"direction": "out",
256+
"name": "res"
257+
},
258+
{
259+
"type": "signalR",
260+
"name": "$return",
261+
"hubName": "default",
262+
"direction": "out"
263+
}
264+
]
265+
}
266+
```
256267
257-
- It changes the route to `messages` and restricts the HTTP trigger to the `POST` HTTP method.
258-
- It adds an Azure SignalR Service output binding that sends a message returned by the function to all clients connected to an Azure SignalR Service hub named `default`.
268+
The preceding code makes two changes to the original file:
259269
260-
1. Replace the content of _sendMessage/index.js_ with the following code:
270+
- It changes the route to `messages` and restricts the HTTP trigger to the `POST` HTTP method.
271+
- It adds an Azure SignalR Service output binding that sends a message returned by the function to all clients connected to an Azure SignalR Service hub named `default`.
261272
262-
```javascript
263-
module.exports = async function (context, req) {
264-
const message = req.body;
265-
message.sender =
266-
(req.headers && req.headers["x-ms-client-principal-name"]) || "";
267-
268-
let recipientUserId = "";
269-
if (message.recipient) {
270-
recipientUserId = message.recipient;
271-
message.isPrivate = true;
272-
}
273-
274-
return {
275-
userId: recipientUserId,
276-
target: "newMessage",
277-
arguments: [message],
278-
};
279-
};
280-
```
273+
1. Replace the content of _sendMessage/index.js_ with the following code:
274+
275+
```javascript
276+
module.exports = async function (context, req) {
277+
const message = req.body;
278+
message.sender =
279+
(req.headers && req.headers["x-ms-client-principal-name"]) || "";
280+
281+
let recipientUserId = "";
282+
if (message.recipient) {
283+
recipientUserId = message.recipient;
284+
message.isPrivate = true;
285+
}
286+
287+
return {
288+
userId: recipientUserId,
289+
target: "newMessage",
290+
arguments: [message],
291+
};
292+
};
293+
```
281294
282-
This function takes the body from the HTTP request and sends it to clients connected to Azure SignalR Service. It invokes a function named `newMessage` on each client.
295+
This function takes the body from the HTTP request and sends it to clients connected to Azure SignalR Service. It invokes a function named `newMessage` on each client.
283296
284-
The function can read the sender's identity and can accept a `recipient` value in the message body to allow you to send a message privately to a single user. You'll use these functionalities later in the tutorial.
297+
The function can read the sender's identity and can accept a `recipient` value in the message body to allow you to send a message privately to a single user. You'll use these functionalities later in the tutorial.
285298
286-
1. Save the file.
299+
1. Save the file.
287300
288301
[Having issues? Let us know.](https://aka.ms/asrs/qsauth)
289302

0 commit comments

Comments
 (0)