From 32e4d49a3a1b923a3164208aeff72ed073145dd6 Mon Sep 17 00:00:00 2001 From: Brennan Conroy Date: Tue, 26 Feb 2019 15:55:41 -0800 Subject: [PATCH 1/6] stash --- aspnetcore/signalr/cpp-client.md | 54 ++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 aspnetcore/signalr/cpp-client.md diff --git a/aspnetcore/signalr/cpp-client.md b/aspnetcore/signalr/cpp-client.md new file mode 100644 index 000000000000..91bacb5a4753 --- /dev/null +++ b/aspnetcore/signalr/cpp-client.md @@ -0,0 +1,54 @@ +--- +title: ASP.NET Core SignalR C++ Client +author: bradygaster +description: Information about the ASP.NET Core SignalR C++ Client +monikerRange: '>= aspnetcore-3.0' +ms.author: bradyg +ms.custom: mvc +ms.date: 2/26/2019 +uid: signalr/cpp-client +--- + +# ASP.NET Core SignalR C++ Client + +The ASP.NET Core SignalR C++ client library lets you communicate with SignalR hubs from native apps. + +## Build the SignalR C++ client library + +TODO + +## Connect to a hub + +```c++ +signalr::hub_connection connection("http://localhost:5000/default", signalr::trace_level::all); +connection.start().get(); +``` + +## Call hub methods from client + +```c++ +connection.send("Echo").get(); +auto value = connection.invoke("Echo").get(); +``` +`invoke` calls methods on the hub and waits for a response. Pass the hub method name and any arguments defined in the hub method to `invoke`. +`send` calls methods on the hub and does not expect a response. Pass the hub method name and any arguments defined in the hub method to `invoke`. + +[!code-csharp[InvokeAsync method](dotnet-client/sample/signalrchatclient/MainWindow.xaml.cs?name=snippet_InvokeAsync)] + +## Call client methods from hub + +Define methods the hub calls using `connection.On` after creating the connection, but before starting it. +```c++ +connection.on("Send", [](const web::json::value& m) +{ + std::cout << m.at(0).as_string() << std::endl; +}); +``` + +The registered handler in `connection.On` runs when server-side code calls it using the `SendAsync` method. + +## Additional resources + +* [Hubs](xref:signalr/hubs) +* [.NET client](xref:signalr/dotnet-client) +* [Publish to Azure](xref:signalr/publish-to-azure-web-app) From ff8e12db05d394f8228026e9080451e3b85566a1 Mon Sep 17 00:00:00 2001 From: Brennan Conroy Date: Tue, 26 Feb 2019 16:04:48 -0800 Subject: [PATCH 2/6] fix --- aspnetcore/signalr/cpp-client.md | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/aspnetcore/signalr/cpp-client.md b/aspnetcore/signalr/cpp-client.md index 91bacb5a4753..5a01ba6d49e0 100644 --- a/aspnetcore/signalr/cpp-client.md +++ b/aspnetcore/signalr/cpp-client.md @@ -11,9 +11,9 @@ uid: signalr/cpp-client # ASP.NET Core SignalR C++ Client -The ASP.NET Core SignalR C++ client library lets you communicate with SignalR hubs from native apps. +The ASP.NET Core SignalR C++ client library lets you communicate with SignalR hubs from native applications. -## Build the SignalR C++ client library +## Consume the SignalR C++ client library TODO @@ -30,14 +30,12 @@ connection.start().get(); connection.send("Echo").get(); auto value = connection.invoke("Echo").get(); ``` -`invoke` calls methods on the hub and waits for a response. Pass the hub method name and any arguments defined in the hub method to `invoke`. -`send` calls methods on the hub and does not expect a response. Pass the hub method name and any arguments defined in the hub method to `invoke`. - -[!code-csharp[InvokeAsync method](dotnet-client/sample/signalrchatclient/MainWindow.xaml.cs?name=snippet_InvokeAsync)] +`invoke` calls methods on the hub and waits for a response. `send` calls methods on the hub and does not expect a response. +Pass the hub method name and any arguments defined in the hub method to `invoke` or `send`. ## Call client methods from hub -Define methods the hub calls using `connection.On` after creating the connection, but before starting it. +Define methods the hub calls using `connection.on` after creating the connection, but before starting it. ```c++ connection.on("Send", [](const web::json::value& m) { @@ -45,7 +43,7 @@ connection.on("Send", [](const web::json::value& m) }); ``` -The registered handler in `connection.On` runs when server-side code calls it using the `SendAsync` method. +The registered handler in `connection.on` runs when server-side code calls it using the `SendAsync` method. ## Additional resources From 11cc51f75b32e999eac25c9eac862596a9935b8e Mon Sep 17 00:00:00 2001 From: Brennan Conroy Date: Wed, 27 Feb 2019 09:21:54 -0800 Subject: [PATCH 3/6] disconnect --- aspnetcore/signalr/cpp-client.md | 42 +++++++++++++++---- .../sample/SignalRChat/Hubs/ChatHub.cs | 2 +- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/aspnetcore/signalr/cpp-client.md b/aspnetcore/signalr/cpp-client.md index 5a01ba6d49e0..e2b3e55aea67 100644 --- a/aspnetcore/signalr/cpp-client.md +++ b/aspnetcore/signalr/cpp-client.md @@ -24,26 +24,54 @@ signalr::hub_connection connection("http://localhost:5000/default", signalr::tra connection.start().get(); ``` +## Handle lost connection + +Register a handler for the disconnected event to respond to a lost connection. For example, you might want to automate reconnection. + +The disconnected event can be registered by calling `set_disconnected` and passing in a function; + +```c++ +connection.set_disconnected([]() +{ + // Do your close logic. +}); +``` + ## Call hub methods from client +`send` calls methods on the hub and does not expect or wait for a response. +```c++ +web::json::value args{}; +args[0] = web::json::value::string("some text"); +connection.send("Echo", args).get(); +``` + +`invoke` calls methods on the hub and waits for a response. ```c++ -connection.send("Echo").get(); -auto value = connection.invoke("Echo").get(); +web::json::value args{}; +args[0] = web::json::value::string("some text"); +connection.invoke("Echo", args) + .then([](const web::json::value& value) + { + // consume 'value' + }).get(); ``` -`invoke` calls methods on the hub and waits for a response. `send` calls methods on the hub and does not expect a response. + Pass the hub method name and any arguments defined in the hub method to `invoke` or `send`. ## Call client methods from hub -Define methods the hub calls using `connection.on` after creating the connection, but before starting it. +Define methods the hub calls using `connection.on` after creating the connection, but before starting it. The `json::value` parameter will be an array of values sent from the server. ```c++ -connection.on("Send", [](const web::json::value& m) +connection.on("ReceiveMessage", [](const web::json::value& m) { - std::cout << m.at(0).as_string() << std::endl; + ucout << m.at(0).as_string() << m.at(1).as_string() std::endl; }); ``` -The registered handler in `connection.on` runs when server-side code calls it using the `SendAsync` method. +The preceding code in `connection.on` runs when server-side code calls it using the `SendAsync` method. + +[!code-csharp[Call client method](dotnet-client/sample/signalrchat/hubs/chathub.cs?name=snippet_SendMessage)] ## Additional resources diff --git a/aspnetcore/signalr/dotnet-client/sample/SignalRChat/Hubs/ChatHub.cs b/aspnetcore/signalr/dotnet-client/sample/SignalRChat/Hubs/ChatHub.cs index 33129ec3944b..e37bba9dfcbf 100644 --- a/aspnetcore/signalr/dotnet-client/sample/SignalRChat/Hubs/ChatHub.cs +++ b/aspnetcore/signalr/dotnet-client/sample/SignalRChat/Hubs/ChatHub.cs @@ -8,7 +8,7 @@ public class ChatHub : Hub #region snippet_SendMessage public async Task SendMessage(string user, string message) { - await Clients.All.SendAsync("ReceiveMessage", user,message); + await Clients.All.SendAsync("ReceiveMessage", user, message); } #endregion } From a9e05525b5387630f5e8e2cb95d1867c6872c527 Mon Sep 17 00:00:00 2001 From: Brennan Conroy Date: Wed, 27 Feb 2019 09:57:10 -0800 Subject: [PATCH 4/6] nits --- aspnetcore/signalr/cpp-client.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/aspnetcore/signalr/cpp-client.md b/aspnetcore/signalr/cpp-client.md index e2b3e55aea67..44384bb49d25 100644 --- a/aspnetcore/signalr/cpp-client.md +++ b/aspnetcore/signalr/cpp-client.md @@ -39,21 +39,21 @@ connection.set_disconnected([]() ## Call hub methods from client -`send` calls methods on the hub and does not expect or wait for a response. +`connection.send` calls methods on the hub and does not expect or wait for a response. ```c++ web::json::value args{}; args[0] = web::json::value::string("some text"); connection.send("Echo", args).get(); ``` -`invoke` calls methods on the hub and waits for a response. +`connection.invoke` calls methods on the hub and waits for a response. ```c++ web::json::value args{}; args[0] = web::json::value::string("some text"); connection.invoke("Echo", args) .then([](const web::json::value& value) { - // consume 'value' + uout << value.serialize() << std::endl; }).get(); ``` @@ -61,7 +61,7 @@ Pass the hub method name and any arguments defined in the hub method to `invoke` ## Call client methods from hub -Define methods the hub calls using `connection.on` after creating the connection, but before starting it. The `json::value` parameter will be an array of values sent from the server. +Define methods the hub calls using `connection.on` after creating the connection, but before starting it. The `web::json::value` parameter will be an array of values sent from the server. ```c++ connection.on("ReceiveMessage", [](const web::json::value& m) { From a12d9bad2382a34dd5bc917b49f4cb1a8c1fe364 Mon Sep 17 00:00:00 2001 From: Brennan Conroy Date: Wed, 27 Feb 2019 14:39:34 -0800 Subject: [PATCH 5/6] log --- aspnetcore/signalr/cpp-client.md | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/aspnetcore/signalr/cpp-client.md b/aspnetcore/signalr/cpp-client.md index 44384bb49d25..70481af7d9c3 100644 --- a/aspnetcore/signalr/cpp-client.md +++ b/aspnetcore/signalr/cpp-client.md @@ -53,7 +53,7 @@ args[0] = web::json::value::string("some text"); connection.invoke("Echo", args) .then([](const web::json::value& value) { - uout << value.serialize() << std::endl; + ucout << value.serialize() << std::endl; }).get(); ``` @@ -73,6 +73,26 @@ The preceding code in `connection.on` runs when server-side code calls it using [!code-csharp[Call client method](dotnet-client/sample/signalrchat/hubs/chathub.cs?name=snippet_SendMessage)] +## Custom logging + +To add logging to the client create a class that inherits from `signalr::log_writer` and add a `write` method that overrides the pure virtual base class. + +```c++ +class custom_logger : public signalr::log_writer +{ + virtual void write(const utility::string_t& log) override + { + ucout << log << std::endl; + } +} +``` + +Then when creating the hub_connection pass a `shared_ptr` into the constructor. + +```c++ +signalr::hub_connection connection("http://localhost:5000/default", signalr::trace_level::all, std::make_shared()); +``` + ## Additional resources * [Hubs](xref:signalr/hubs) From a3966ac94cfd6a641df62eb5455b40e73bfeb5a4 Mon Sep 17 00:00:00 2001 From: Scott Addie <10702007+scottaddie@users.noreply.github.com> Date: Fri, 1 Mar 2019 09:26:34 -0800 Subject: [PATCH 6/6] Apply suggestions from code review Co-Authored-By: BrennanConroy --- aspnetcore/signalr/cpp-client.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/aspnetcore/signalr/cpp-client.md b/aspnetcore/signalr/cpp-client.md index 70481af7d9c3..f341111cd382 100644 --- a/aspnetcore/signalr/cpp-client.md +++ b/aspnetcore/signalr/cpp-client.md @@ -1,15 +1,15 @@ --- -title: ASP.NET Core SignalR C++ Client +title: ASP.NET Core SignalR C++ client author: bradygaster -description: Information about the ASP.NET Core SignalR C++ Client +description: Information about the ASP.NET Core SignalR C++ client monikerRange: '>= aspnetcore-3.0' ms.author: bradyg ms.custom: mvc -ms.date: 2/26/2019 +ms.date: 02/26/2019 uid: signalr/cpp-client --- -# ASP.NET Core SignalR C++ Client +# ASP.NET Core SignalR C++ client The ASP.NET Core SignalR C++ client library lets you communicate with SignalR hubs from native applications. @@ -28,7 +28,7 @@ connection.start().get(); Register a handler for the disconnected event to respond to a lost connection. For example, you might want to automate reconnection. -The disconnected event can be registered by calling `set_disconnected` and passing in a function; +The disconnected event can be registered by calling `set_disconnected` and passing in a function: ```c++ connection.set_disconnected([]() @@ -39,7 +39,8 @@ connection.set_disconnected([]() ## Call hub methods from client -`connection.send` calls methods on the hub and does not expect or wait for a response. +`connection.send` calls methods on the hub and doesn't expect or wait for a response. + ```c++ web::json::value args{}; args[0] = web::json::value::string("some text"); @@ -47,6 +48,7 @@ connection.send("Echo", args).get(); ``` `connection.invoke` calls methods on the hub and waits for a response. + ```c++ web::json::value args{}; args[0] = web::json::value::string("some text"); @@ -62,6 +64,7 @@ Pass the hub method name and any arguments defined in the hub method to `invoke` ## Call client methods from hub Define methods the hub calls using `connection.on` after creating the connection, but before starting it. The `web::json::value` parameter will be an array of values sent from the server. + ```c++ connection.on("ReceiveMessage", [](const web::json::value& m) { @@ -87,7 +90,7 @@ class custom_logger : public signalr::log_writer } ``` -Then when creating the hub_connection pass a `shared_ptr` into the constructor. +When creating the `hub_connection`, pass a `shared_ptr` into the constructor. ```c++ signalr::hub_connection connection("http://localhost:5000/default", signalr::trace_level::all, std::make_shared());