diff --git a/samples/EventGridIntegration/README.md b/samples/EventGridIntegration/README.md index 3a3a399e..15893821 100644 --- a/samples/EventGridIntegration/README.md +++ b/samples/EventGridIntegration/README.md @@ -13,7 +13,7 @@ A step by step tutorial to build a chat room with real-time online counting usin The following softwares are required to build this tutorial. -* [Node.js](https://nodejs.org/en/download/) (Version 10.x) +* [Node.js](https://nodejs.org/en/download/) (Version 10.x, required for JavaScript sample) * [.NET SDK](https://www.microsoft.com/net/download) (Version 2.x, required for Functions extensions) * [Azure Functions Core Tools](https://github.com/Azure/azure-functions-core-tools) (Version 2) * [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest) @@ -51,15 +51,25 @@ An Azure Storage account is required by a function app using Event Grid trigger. ```bash git clone git@github.com:aspnet/AzureSignalR-samples.git + ``` + +- In the repository, there're two Event Grid integration samples using different languages. For the JavaScript sample, navigate to - cd AzureSignalR-samples/samples/EventGridIntegration/javascript + ```bash + AzureSignalR-samples/samples/EventGridIntegration/javascript + ``` + +- If you want to use C# sample, navigate to + + ```bash + AzureSignalR-samples/samples/EventGridIntegration/csharp ``` ### Configure application settings When running and debugging the Azure Functions runtime locally, application settings are read from **local.settings.json**. Also, you can upload there settings to remote when you try to deploy Function App to Azure. Update this file with the connection string of the SignalR Service instance that you created earlier. -1. Open the file **local.settings.json** and update the settings. +1. Open the file **local.settings.json** and update the settings. (The file shown below is used by JavaScript sample, and the C# sample is similar) ```json { @@ -232,7 +242,25 @@ App Service Authentication supports authentication with Azure Active Directory, ### Update negotiate function -1. Update in `userId` in `negotiate/function.json` to `"{headers.x-ms-client-principal-name}"` +1. Update the attribute parameter of negotiate function. + + - If you're using JavaScript sample, update in `userId` in `negotiate/function.json` to `"{headers.x-ms-client-principal-name}"`. + + ```json + { + "type": "signalRConnectionInfo", + "name": "connectionInfo", + "userId": "{headers.x-ms-client-principal-name}", + "hubName": "EventGridIntegrationSampleChat", + "direction": "in" + } + ``` + + - If you're using C# sample, add parameter `UserId = "{headers.x-ms-signalr-userid}"` to `Negotiate` function. + + ```C# + [SignalRConnectionInfo(HubName = HubName, UserId = "{headers.x-ms-signalr-userid}")] SignalRConnectionInfo connectionInfo + ``` 1. Deploy the function to Azure again diff --git a/samples/EventGridIntegration/csharp/.gitignore b/samples/EventGridIntegration/csharp/.gitignore new file mode 100644 index 00000000..ff5b00c5 --- /dev/null +++ b/samples/EventGridIntegration/csharp/.gitignore @@ -0,0 +1,264 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# Azure Functions localsettings file +local.settings.json + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/samples/EventGridIntegration/csharp/Functions.cs b/samples/EventGridIntegration/csharp/Functions.cs new file mode 100644 index 00000000..8def85dc --- /dev/null +++ b/samples/EventGridIntegration/csharp/Functions.cs @@ -0,0 +1,170 @@ +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.SignalR; +using Microsoft.Azure.EventGrid.Models; +using Microsoft.Azure.WebJobs; +using Microsoft.Azure.WebJobs.Extensions.EventGrid; +using Microsoft.Azure.WebJobs.Extensions.Http; +using Microsoft.Azure.WebJobs.Extensions.SignalRService; +using Microsoft.Extensions.Logging; +using Microsoft.WindowsAzure.Storage.Table; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace SignalR.Sample +{ + public static class Functions + { + private const string TableName = "connection"; + private const string EventGridConnectedEventName = "Microsoft.SignalRService.ClientConnectionConnected"; + private const string HubName = "EventGridIntegrationSampleChat"; + + [FunctionName("negotiate")] + public static SignalRConnectionInfo Negotiate( + [HttpTrigger(AuthorizationLevel.Anonymous)] HttpRequest req, + [SignalRConnectionInfo(HubName = HubName)] SignalRConnectionInfo connectionInfo) + { + return connectionInfo; + } + + [FunctionName("messages")] + public static Task SendMessage( + [HttpTrigger(AuthorizationLevel.Anonymous, "post")]HttpRequest req, + [SignalR(HubName = HubName)]IAsyncCollector signalRMessages) + { + var message = new JsonSerializer().Deserialize(new JsonTextReader(new StreamReader(req.Body))); + message.Sender = req.Headers?["x-ms-client-principal-name"] ?? ""; + var recipientUserId = ""; + if (!string.IsNullOrEmpty(message.Recipient)) + { + recipientUserId = message.Recipient; + message.IsPrivate = true; + } + + return signalRMessages.AddAsync(new SignalRMessage + { + UserId = message.Recipient, + Target = "newMessage", + Arguments = new[] { message } + }); + } + + [FunctionName("OnConnection")] + public static async Task EventGridTest([EventGridTrigger]EventGridEvent eventGridEvent, + [SignalR(HubName = HubName)]IAsyncCollector signalRMessages, + [Table(TableName)]CloudTable cloudTable, + ILogger log) + { + var message = ((JObject) eventGridEvent.Data).ToObject(); + var partitionKey = GetLastPart(eventGridEvent.Topic); + var rowKey = message.HubName; + var isSuccess = true; + var newConnectionCount = 0; + + while (isSuccess) + { + try + { + ConnectionCountEntity entity; + var operation = TableOperation.Retrieve(partitionKey, rowKey); + var result = await cloudTable.ExecuteAsync(operation); + + if (result.Result == null) + { + entity = new ConnectionCountEntity(partitionKey, rowKey) + { + Count = newConnectionCount = IsConnectedEvent(eventGridEvent.EventType) ? 1 : 0 + }; + operation = TableOperation.Insert(entity); + } + else + { + entity = (ConnectionCountEntity)result.Result; + entity.Count = newConnectionCount = entity.Count + (IsConnectedEvent(eventGridEvent.EventType) ? 1 : -1); + operation = TableOperation.Replace(entity); + } + + await cloudTable.ExecuteAsync(operation); + isSuccess = false; + } + catch (Exception ex) + { + log.LogError(ex, "Failed to complete operation with storage"); + } + } + + if (IsConnectedEvent(eventGridEvent.EventType)) + { + await signalRMessages.AddAsync(new SignalRMessage + { + ConnectionId = message.ConnectionId, + Target = "newMessage", + Arguments = new[] { new ChatMessage + { + Text = "Welcome to Serverless Chat", + Sender = "__SYSTEM__", + }} + }); + } + + await signalRMessages.AddAsync(new SignalRMessage + { + Target = "connectionCount", + Arguments = new[] { (object)newConnectionCount }, + }); + } + + private static string GetLastPart(string data) + { + var index = data.LastIndexOf('/'); + if (index == -1) + { + return data; + } + else + { + return data.Substring(index + 1); + } + } + + private static bool IsConnectedEvent(string name) => name == EventGridConnectedEventName; + + public class ChatMessage + { + [JsonProperty("sender")] + public string Sender { get; set; } + [JsonProperty("text")] + public string Text { get; set; } + [JsonProperty("recipient")] + public string Recipient { get; set; } + [JsonProperty("isPrivate")] + public bool IsPrivate { get; set; } + } + + public class SignalREvent + { + public DateTime Timestamp { get; set; } + public string HubName { get; set; } + public string ConnectionId { get; set; } + public string UserId { get; set; } + } + + public class ConnectionCountEntity : TableEntity + { + public int Count { get; set; } + + public ConnectionCountEntity() + { + } + + public ConnectionCountEntity(string partitionKey, string rowKey) + { + PartitionKey = partitionKey; + RowKey = rowKey; + } + } + } +} diff --git a/samples/EventGridIntegration/csharp/csharp.csproj b/samples/EventGridIntegration/csharp/csharp.csproj new file mode 100644 index 00000000..e7ee1e12 --- /dev/null +++ b/samples/EventGridIntegration/csharp/csharp.csproj @@ -0,0 +1,21 @@ + + + netcoreapp2.1 + v2 + + + + + + + + + + PreserveNewest + + + PreserveNewest + Never + + + \ No newline at end of file diff --git a/samples/EventGridIntegration/csharp/host.json b/samples/EventGridIntegration/csharp/host.json new file mode 100644 index 00000000..b9f92c0d --- /dev/null +++ b/samples/EventGridIntegration/csharp/host.json @@ -0,0 +1,3 @@ +{ + "version": "2.0" +} \ No newline at end of file diff --git a/samples/EventGridIntegration/csharp/local.settings.json b/samples/EventGridIntegration/csharp/local.settings.json new file mode 100644 index 00000000..8fda0c73 --- /dev/null +++ b/samples/EventGridIntegration/csharp/local.settings.json @@ -0,0 +1,14 @@ +{ + "IsEncrypted": false, + "Values": { + "AzureSignalRConnectionString": "", + "FUNCTIONS_WORKER_RUNTIME": "dotnet", + "AzureWebJobsStorage": "", + "AZURE_STORAGE_CONNECTION_STRING": "" + }, + "Host": { + "LocalHttpPort": 7071, + "CORS": "http://127.0.0.1:5500", + "CORSCredentials": true + } +} \ No newline at end of file