Skip to content

Commit 9886a30

Browse files
committed
fix #210 / better standalone example, an echo client
1 parent 4ed5206 commit 9886a30

File tree

2 files changed

+115
-18
lines changed

2 files changed

+115
-18
lines changed

README.md

+53-18
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,68 @@ IXWebSocket is a C++ library for WebSocket client and server development. It has
55
It is been used on big mobile video game titles sending and receiving tons of messages since 2017 (iOS and Android). It was tested on macOS, iOS, Linux, Android, Windows and FreeBSD. Two important design goals are simplicity and correctness.
66

77
```cpp
8+
/*
9+
* main.cpp
10+
* Author: Benjamin Sergeant
11+
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
12+
*
13+
* Super simple standalone example. See ws folder, unittest and doc/usage.md for more.
14+
*
15+
* On macOS
16+
* $ mkdir -p build ; cd build ; cmake -DUSE_TLS=1 .. ; make -j ; make install
17+
* $ clang++ --std=c++14 --stdlib=libc++ main.cpp -lixwebsocket -lz -framework Security -framework Foundation
18+
* $ ./a.out
19+
*/
20+
21+
#include <ixwebsocket/IXNetSystem.h>
822
#include <ixwebsocket/IXWebSocket.h>
23+
#include <iostream>
924

10-
// Required on Windows
11-
ix::initNetSystem();
25+
int main()
26+
{
27+
// Required on Windows
28+
ix::initNetSystem();
1229

13-
// Our websocket object
14-
ix::WebSocket webSocket;
30+
// Our websocket object
31+
ix::WebSocket webSocket;
1532

16-
std::string url("ws://localhost:8080/");
17-
webSocket.setUrl(url);
33+
std::string url("wss://echo.websocket.org");
34+
webSocket.setUrl(url);
1835

19-
// Setup a callback to be fired (in a background thread, watch out for race conditions !)
20-
// when a message or an event (open, close, error) is received
21-
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
22-
{
23-
if (msg->type == ix::WebSocketMessageType::Message)
36+
std::cout << "Connecting to " << url << "..." << std::endl;
37+
38+
// Setup a callback to be fired (in a background thread, watch out for race conditions !)
39+
// when a message or an event (open, close, error) is received
40+
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
2441
{
25-
std::cout << msg->str << std::endl;
42+
if (msg->type == ix::WebSocketMessageType::Message)
43+
{
44+
std::cout << "received message: " << msg->str << std::endl;
45+
}
46+
else if (msg->type == ix::WebSocketMessageType::Open)
47+
{
48+
std::cout << "Connection established" << std::endl;
49+
}
2650
}
27-
}
28-
);
51+
);
52+
53+
// Now that our callback is setup, we can start our background thread and receive messages
54+
webSocket.start();
2955

30-
// Now that our callback is setup, we can start our background thread and receive messages
31-
webSocket.start();
56+
// Send a message to the server (default to TEXT mode)
57+
webSocket.send("hello world");
58+
59+
while (true)
60+
{
61+
std::string text;
62+
std::cout << "> " << std::flush;
63+
std::getline(std::cin, text);
64+
65+
webSocket.send(text);
66+
}
3267

33-
// Send a message to the server (default to TEXT mode)
34-
webSocket.send("hello world");
68+
return 0;
69+
}
3570
```
3671

3772
Interested? Go read the [docs](https://machinezone.github.io/IXWebSocket/)! If things don't work as expected, please create an issue on GitHub, or even better a pull request if you know how to fix your problem.

main.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* main.cpp
3+
* Author: Benjamin Sergeant
4+
* Copyright (c) 2020 Machine Zone, Inc. All rights reserved.
5+
*
6+
* Super simple standalone example. See ws folder, unittest and doc/usage.md for more.
7+
*
8+
* On macOS
9+
* $ mkdir -p build ; cd build ; cmake -DUSE_TLS=1 .. ; make -j ; make install
10+
* $ clang++ --std=c++14 --stdlib=libc++ main.cpp -lixwebsocket -lz -framework Security -framework Foundation
11+
* $ ./a.out
12+
*/
13+
14+
#include <ixwebsocket/IXNetSystem.h>
15+
#include <ixwebsocket/IXWebSocket.h>
16+
#include <iostream>
17+
18+
int main()
19+
{
20+
// Required on Windows
21+
ix::initNetSystem();
22+
23+
// Our websocket object
24+
ix::WebSocket webSocket;
25+
26+
std::string url("wss://echo.websocket.org");
27+
webSocket.setUrl(url);
28+
29+
std::cout << "Connecting to " << url << "..." << std::endl;
30+
31+
// Setup a callback to be fired (in a background thread, watch out for race conditions !)
32+
// when a message or an event (open, close, error) is received
33+
webSocket.setOnMessageCallback([](const ix::WebSocketMessagePtr& msg)
34+
{
35+
if (msg->type == ix::WebSocketMessageType::Message)
36+
{
37+
std::cout << "received message: " << msg->str << std::endl;
38+
}
39+
else if (msg->type == ix::WebSocketMessageType::Open)
40+
{
41+
std::cout << "Connection established" << std::endl;
42+
}
43+
}
44+
);
45+
46+
// Now that our callback is setup, we can start our background thread and receive messages
47+
webSocket.start();
48+
49+
// Send a message to the server (default to TEXT mode)
50+
webSocket.send("hello world");
51+
52+
while (true)
53+
{
54+
std::string text;
55+
std::cout << "> " << std::flush;
56+
std::getline(std::cin, text);
57+
58+
webSocket.send(text);
59+
}
60+
61+
return 0;
62+
}

0 commit comments

Comments
 (0)