Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions aspnetcore/signalr/cpp-client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
---
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: 02/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 applications.

## Consume 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();
```

## 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

`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");
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");
connection.invoke("Echo", args)
.then([](const web::json::value& value)
{
ucout << value.serialize() << std::endl;
}).get();
```

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. The `web::json::value` parameter will be an array of values sent from the server.

```c++
connection.on("ReceiveMessage", [](const web::json::value& m)
{
ucout << m.at(0).as_string() << m.at(1).as_string() std::endl;
});
```

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)]

## 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;
}
}
```

When creating the `hub_connection`, pass a `shared_ptr<custom_logger>` into the constructor.

```c++
signalr::hub_connection connection("http://localhost:5000/default", signalr::trace_level::all, std::make_shared<custom_logger>());
```

## Additional resources

* [Hubs](xref:signalr/hubs)
* [.NET client](xref:signalr/dotnet-client)
* [Publish to Azure](xref:signalr/publish-to-azure-web-app)
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading