-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathevents.cpp
More file actions
101 lines (81 loc) · 2.74 KB
/
Copy pathevents.cpp
File metadata and controls
101 lines (81 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <mage.h>
#include <future>
#include <iostream>
using namespace mage;
using namespace std;
class ExampleEventObserver : public mage::EventObserver {
public:
explicit ExampleEventObserver(mage::RPC* client) : m_pClient(client) {}
virtual void ReceiveEvent(const std::string& name,
const Json::Value& data = Json::Value::null) const {
std::cout << "Receive event: " << name << std::endl;
if (data != Json::Value::null) {
std::cout << "data: " << data.toStyledString() << std::endl;
}
// Set the session when it receive the session.set event
if (name == "session.set") {
HandleSessionSet(data);
}
}
void HandleSessionSet(const Json::Value& data) const {
m_pClient->SetSession(data["key"].asString());
}
private:
mage::RPC* m_pClient;
};
int main() {
mage::RPC client("game", "localhost:8080");
// Initialize the EventObserver
ExampleEventObserver eventObserver(&client);
// Attach our EventObserver to the MAGE RPC client
client.AddObserver(&eventObserver);
// Login using the anonymous engine
std::future<Json::Value> loginRes;
Json::Value auth;
auth["engineName"] = "anonymous";
auth["credentials"] = Json::Value::null;
auth["options"]["access"] = "user";
try {
loginRes = client.Call("ident.login", auth, true);
loginRes.wait();
} catch (mage::MageRPCError e) {
cerr << "Could not login, an RPC error has occured: " << e.what() << " (code " << e.code() << ")" << endl;
return 1;
} catch (mage::MageErrorMessage e) {
cerr << "Login failed: " << e.code() << endl;
return 1;
}
//
// Start the polling loop in a background thread
//
client.StartPolling();
//
// From here, all the calls you will be doing
// are authenticated.
//
std::future<Json::Value> res;
Json::Value params;
try {
res = client.Call("mymodule.mycommand", params, true);
// Trigger the event handlers to avoid breaking the output
// They will be proceded at the first call of wait() or get()
res.wait();
// Handle the command response
cout << "mymodule.mycommand (authenticated): " << res.get() << endl;
} catch (mage::MageRPCError e) {
cerr << "An RPC error has occured: " << e.what() << " (code " << e.code() << ")" << endl;
} catch (mage::MageErrorMessage e) {
cerr << "mymodule.mycommand responded with an error: " << e.code() << endl;
}
// Wait for an user input before closing the application
std::cout << "Type \"quit\" to quit the example." << std::endl;
std::string command;
do {
std::getline(std::cin, command);
} while (command != "quit");
std::cout << "The example will now stop." << std::endl
<< "Waiting for the end of the polling thread..." << std::endl;
// Stop the polling loop
client.StopPolling();
std::cout << "Now exiting" << std::endl;
}