Skip to content

Commit 5267b18

Browse files
authored
Merge pull request #189228 from zackliu/fixquickstart
[AzureSignalR] Fix Quickstart
2 parents 25b2133 + 9eeb810 commit 5267b18

4 files changed

+69
-25
lines changed

articles/azure-signalr/signalr-quickstart-azure-functions-csharp.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
5454
```csharp
5555
using System;
5656
using System.IO;
57+
using System.Linq;
5758
using System.Net.Http;
5859
using System.Threading.Tasks;
5960
using Microsoft.AspNetCore.Http;
@@ -62,15 +63,17 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
6263
using Microsoft.Azure.WebJobs.Extensions.Http;
6364
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
6465
using Newtonsoft.Json;
65-
66+
6667
namespace CSharp
6768
{
6869
public static class Function
6970
{
7071
private static HttpClient httpClient = new HttpClient();
71-
72+
private static string Etag = string.Empty;
73+
private static string StarCount = "0";
74+
7275
[FunctionName("index")]
73-
public static IActionResult Index([HttpTrigger(AuthorizationLevel.Anonymous)]HttpRequest req, ExecutionContext context)
76+
public static IActionResult GetHomePage([HttpTrigger(AuthorizationLevel.Anonymous)]HttpRequest req, ExecutionContext context)
7477
{
7578
var path = Path.Combine(context.FunctionAppDirectory, "content", "index.html");
7679
return new ContentResult
@@ -79,31 +82,41 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
7982
ContentType = "text/html",
8083
};
8184
}
82-
85+
8386
[FunctionName("negotiate")]
84-
public static SignalRConnectionInfo Negotiate(
87+
public static SignalRConnectionInfo Negotiate(
8588
[HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req,
86-
[SignalRConnectionInfo(HubName = "serverlessSample")] SignalRConnectionInfo connectionInfo)
89+
[SignalRConnectionInfo(HubName = "serverless")] SignalRConnectionInfo connectionInfo)
8790
{
8891
return connectionInfo;
8992
}
90-
93+
9194
[FunctionName("broadcast")]
9295
public static async Task Broadcast([TimerTrigger("*/5 * * * * *")] TimerInfo myTimer,
93-
[SignalR(HubName = "serverlessSample")] IAsyncCollector<SignalRMessage> signalRMessages)
96+
[SignalR(HubName = "serverless")] IAsyncCollector<SignalRMessage> signalRMessages)
9497
{
9598
var request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/repos/azure/azure-signalr");
9699
request.Headers.UserAgent.ParseAdd("Serverless");
100+
request.Headers.Add("If-None-Match", Etag);
97101
var response = await httpClient.SendAsync(request);
98-
var result = JsonConvert.DeserializeObject<GitResult>(await response.Content.ReadAsStringAsync());
102+
if (response.Headers.Contains("Etag"))
103+
{
104+
Etag = response.Headers.GetValues("Etag").First();
105+
}
106+
if (response.StatusCode == System.Net.HttpStatusCode.OK)
107+
{
108+
var result = JsonConvert.DeserializeObject<GitResult>(await response.Content.ReadAsStringAsync());
109+
StarCount = result.StarCount;
110+
}
111+
99112
await signalRMessages.AddAsync(
100113
new SignalRMessage
101114
{
102115
Target = "newMessage",
103-
Arguments = new[] { $"Current star count of https://github.com/Azure/azure-signalr is: {result.StarCount}" }
116+
Arguments = new[] { $"Current star count of https://github.com/Azure/azure-signalr is: {StarCount}" }
104117
});
105118
}
106-
119+
107120
private class GitResult
108121
{
109122
[JsonRequired]

articles/azure-signalr/signalr-quickstart-azure-functions-java.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
6969

7070
```java
7171
package com.signalr;
72-
72+
7373
import com.google.gson.Gson;
7474
import com.microsoft.azure.functions.ExecutionContext;
7575
import com.microsoft.azure.functions.HttpMethod;
@@ -97,6 +97,9 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
9797
import java.util.Optional;
9898
9999
public class Function {
100+
private static String Etag = "";
101+
private static String StarCount;
102+
100103
@FunctionName("index")
101104
public HttpResponseMessage run(
102105
@HttpTrigger(
@@ -109,7 +112,7 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
109112
String text = IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
110113
return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "text/html").body(text).build();
111114
}
112-
115+
113116
@FunctionName("negotiate")
114117
public SignalRConnectionInfo negotiate(
115118
@HttpTrigger(
@@ -127,13 +130,21 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
127130
@SignalROutput(name = "$return", hubName = "serverless")
128131
public SignalRMessage broadcast(
129132
@TimerTrigger(name = "timeTrigger", schedule = "*/5 * * * * *") String timerInfo) throws IOException, InterruptedException {
130-
131133
HttpClient client = HttpClient.newHttpClient();
132-
HttpRequest req = HttpRequest.newBuilder().uri(URI.create("https://api.github.com/repos/azure/azure-signalr")).header("User-Agent", "serverless").build();
134+
HttpRequest req = HttpRequest.newBuilder().uri(URI.create("https://api.github.com/repos/azure/azure-signalr")).header("User-Agent", "serverless").header("If-None-Match", Etag).build();
133135
HttpResponse<String> res = client.send(req, BodyHandlers.ofString());
134-
Gson gson = new Gson();
135-
GitResult result = gson.fromJson(res.body(), GitResult.class);
136-
return new SignalRMessage("newMessage", "Current star count of https://github.com/Azure/azure-signalr is:".concat(result.stargazers_count));
136+
if (res.headers().firstValue("Etag").isPresent())
137+
{
138+
Etag = res.headers().firstValue("Etag").get();
139+
}
140+
if (res.statusCode() == 200)
141+
{
142+
Gson gson = new Gson();
143+
GitResult result = gson.fromJson(res.body(), GitResult.class);
144+
StarCount = result.stargazers_count;
145+
}
146+
147+
return new SignalRMessage("newMessage", "Current start count of https://github.com/Azure/azure-signalr is:".concat(StarCount));
137148
}
138149
139150
class GitResult {

articles/azure-signalr/signalr-quickstart-azure-functions-javascript.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,32 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
176176
```javascript
177177
var https = require('https');
178178
179+
var etag = '';
180+
var star = 0;
181+
179182
module.exports = function (context) {
180183
var req = https.request("https://api.github.com/repos/azure/azure-signalr", {
181184
method: 'GET',
182-
headers: {'User-Agent': 'serverless'}
185+
headers: {'User-Agent': 'serverless', 'If-None-Match': etag}
183186
}, res => {
187+
if (res.headers['etag']) {
188+
etag = res.headers['etag']
189+
}
190+
184191
var body = "";
185192
186193
res.on('data', data => {
187194
body += data;
188195
});
189196
res.on("end", () => {
190-
var jbody = JSON.parse(body);
197+
if (res.statusCode === 200) {
198+
var jbody = JSON.parse(body);
199+
star = jbody['stargazers_count'];
200+
}
201+
191202
context.bindings.signalRMessages = [{
192203
"target": "newMessage",
193-
"arguments": [ `Current star count of https://github.com/Azure/azure-signalr is: ${jbody['stargazers_count']}` ]
204+
"arguments": [ `Current star count of https://github.com/Azure/azure-signalr is: ${star}` ]
194205
}]
195206
context.done();
196207
});
@@ -203,7 +214,7 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
203214
context.done();
204215
});
205216
req.end();
206-
}
217+
}
207218
```
208219
209220
3. The client interface of this sample is a web page. Considered we read HTML content from `content/index.html` in `index` function, create a new file `index.html` in `content` directory under your project root folder. And copy the following content.

articles/azure-signalr/signalr-quickstart-azure-functions-python.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,24 @@ Having issues? Try the [troubleshooting guide](signalr-howto-troubleshoot-guide.
180180
181181
import azure.functions as func
182182
183+
etag = ''
184+
start_count = 0
183185
184186
def main(myTimer: func.TimerRequest, signalRMessages: func.Out[str]) -> None:
185-
headers = {'User-Agent': 'serverless'}
187+
global etag
188+
global start_count
189+
headers = {'User-Agent': 'serverless', 'If-None-Match': etag}
186190
res = requests.get('https://api.github.com/repos/azure/azure-signalr', headers=headers)
187-
jres = res.json()
191+
if res.headers.get('ETag'):
192+
etag = res.headers.get('ETag')
188193
194+
if res.status_code == 200:
195+
jres = res.json()
196+
start_count = jres['stargazers_count']
197+
189198
signalRMessages.set(json.dumps({
190199
'target': 'newMessage',
191-
'arguments': [ 'Current star count of https://github.com/Azure/azure-signalr is: ' + str(jres['stargazers_count']) ]
200+
'arguments': [ 'Current star count of https://github.com/Azure/azure-signalr is: ' + str(start_count) ]
192201
}))
193202
```
194203

0 commit comments

Comments
 (0)