Add C++ example README.md and add more documentation.
GitOrigin-RevId: c75b98548766f5cabe02dc8eeb63049bd0847e8c
This commit is contained in:
parent
51135ccd1e
commit
376f582803
@ -81,9 +81,9 @@ cmake -DCMAKE_BUILD_TYPE=Release -DOPENSSL_ROOT_DIR=/usr/local/opt/openssl/ ..
|
||||
C:\src\vcpkg> .\vcpkg install openssl zlib
|
||||
```
|
||||
* Build `TDLib` with CMake as explained in [building](#building), but instead of `cmake -DCMAKE_BUILD_TYPE=Release ..` use
|
||||
```
|
||||
cmake -DCMAKE_TOOLCHAIN_FILE=C:\src\vcpkg\scripts\buildsystems\vcpkg.cmake ..
|
||||
```
|
||||
```
|
||||
cmake -DCMAKE_TOOLCHAIN_FILE=C:\src\vcpkg\scripts\buildsystems\vcpkg.cmake ..
|
||||
```
|
||||
|
||||
<a name="linux"></a>
|
||||
#### Linux
|
||||
|
20
example/cpp/README.md
Normal file
20
example/cpp/README.md
Normal file
@ -0,0 +1,20 @@
|
||||
# TDLib cpp basic usage examples
|
||||
|
||||
TDLib should be prebuilt and installed to local subdirectory `td/`:
|
||||
```
|
||||
cd <path to TDLib sources>
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=../example/cpp/td ..
|
||||
cmake --build . --target install
|
||||
```
|
||||
Also see [building](https://github.com/tdlib/td#building) for additional details on TDLib building.
|
||||
|
||||
Then you can build the examples:
|
||||
```
|
||||
cd <path to TDLib sources>/example/cpp
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -DCMAKE_BUILD_TYPE=Release -DTd_DIR=<full path to TDLib sources>/example/cpp/td/lib/cmake/Td ..
|
||||
cmake --build .
|
||||
```
|
@ -20,6 +20,7 @@
|
||||
|
||||
// Simple single-threaded example of TDLib usage.
|
||||
// Real world programs should use separate thread for the user input.
|
||||
// Example includes user authentication, receiving updates, getting chat list and sending text messages.
|
||||
|
||||
// overloaded
|
||||
namespace detail {
|
||||
|
@ -5,17 +5,29 @@
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <td/telegram/td_json_client.h>
|
||||
|
||||
int main(void) {
|
||||
// Basic example of TDLib JSON interface usage.
|
||||
// Native interface should be preferred instead in C++, so here is only an example of
|
||||
// the main event cycle, which should be essentially the same for all languages.
|
||||
|
||||
int main() {
|
||||
void *client = td_json_client_create();
|
||||
// somehow share the client with other threads, which will be able to send requests via td_json_client_send
|
||||
|
||||
const double WAIT_TIMEOUT = 10.0; // seconds
|
||||
while (true) {
|
||||
std::string str = td_json_client_receive(client, 10);
|
||||
if (!str.empty()) {
|
||||
std::cout << str << std::endl;
|
||||
break;
|
||||
const char *result = td_json_client_receive(client, WAIT_TIMEOUT);
|
||||
if (result != nullptr) {
|
||||
// parse the result as JSON object and process it as an incoming update or an answer to a previously sent request
|
||||
|
||||
// if (result is UpdateAuthorizationState with authorizationStateClosed) {
|
||||
// break;
|
||||
// }
|
||||
|
||||
std::cout << result << std::endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
td_json_client_destroy(client);
|
||||
}
|
||||
|
Reference in New Issue
Block a user