-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
48 lines (42 loc) · 1.11 KB
/
main.cpp
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
//
// main.cpp
// cppwebsocket
//
// Created by dawenhing on 27/07/2017.
// Copyright © 2017 dawenhing. All rights reserved.
//
#include <iostream>
#include <string>
#include "WebSocketClient.hpp"
std::string getlineWithReturn() {
std::string str;
char ch;
static const size_t maxCommandLength = 64;
while ((ch = std::cin.get()) != '\n' && str.length() < maxCommandLength) {
str.push_back(ch);
}
return str;
}
int main(int argc, const char * argv[]) {
auto ws = std::unique_ptr<cppws::WebSocketClient>(new cppws::WebSocketClient({"ws://127.0.0.1:12345/chat"}));
ws->onOpen = [] {
std::cout << ":open" << std::endl;
};
ws->onClosed = [] {
std::cout << ":closed" << std::endl;
};
ws->onMessage = [&ws](const std::string& message) {
std::cout << ":" << message << std::endl;
};
ws->open();
std::thread finish([&ws]{
while(true) {
std::string str = getlineWithReturn();
if (str != "stop") {
ws->sendMessage(str);
}
}
});
finish.join();
return 0;
}