Skip to content

Instantly share code, notes, and snippets.

@tuxmartin
Last active January 5, 2022 20:34
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save tuxmartin/1218851b7e025f68ecc50f949c9dd332 to your computer and use it in GitHub Desktop.
Save tuxmartin/1218851b7e025f68ecc50f949c9dd332 to your computer and use it in GitHub Desktop.
Minimal Poco websocket C++ client

Stazeni a kompilace POCO

wget http://pocoproject.org/releases/poco-1.7.3/poco-1.7.3.tar.gz
tar xzf poco-1.7.3.tar.gz
cd poco-1.7.3
./configure --minimal --static --no-samples --no-tests
time make -j4 -s
cd lib/Linux/x86_64/
for f in *.a; do "strip $f"; done
du -sh ../ 

Cesty

-Ipoco-1.7.3/Util/include/Util
-Ipoco-1.7.3/Net/include/Net
-Ipoco-1.7.3/XML/include/XML
-Ipoco-1.7.3/Foundation/include/Foundation

-Ipoco-1.7.3/CppUnit/include/CppUnit
-Ipoco-1.7.3/JSON/include/JSON

-Lpoco-1.7.3/lib/Linux/x86_64 -lPocoNet -lPocoUtil -lPocoFoundation

-lpthread -static

g++ ws_client.cpp -Ipoco-1.7.3/Util/include/Util -Ipoco-1.7.3/Net/include/Net -Ipoco-1.7.3/XML/include/XML -Ipoco-1.7.3/Foundation/include/Foundation -Lpoco-1.7.3/lib/Linux/x86_64 -lPocoNet -lPocoUtil -lPocoFoundation -lpthread -o ws_client

Kompilace

g++ ws_client.cpp -Lpoco-1.7.3/lib/Linux/x86_64 -lPocoNet -lPocoFoundation -lpthread -o ws_client
martin@martin:~/pocotest$ g++ ws_client.cpp -Lpoco-1.7.3/lib/Linux/x86_64 -lPocoNet -lPocoFoundation -lpthread -o ws_client
martin@martin:~/pocotest$ strip ws_client
martin@martin:~/pocotest$ ls -lh | grep ws_client
-rwxr-xr-x  1 martin martin 877K kvě 30 00:04 ws_client
-rw-r--r--  1 martin martin 1,2K kvě 30 00:03 ws_client.cpp
martin@martin:~/pocotest$ ldd ws_client
	linux-vdso.so.1 =>  (0x00007fff8cffe000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fe4e8c5b000)
	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fe4e8957000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fe4e8650000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fe4e843a000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe4e8075000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fe4e8ea9000)
martin@martin:~/pocotest$ 
martin@martin:~/pocotest$ ./ws_client 
Sent bytes 21
Received bytes 21
Hello echo websocket!
martin@martin:~/pocotest$ file ws_client
ws_client: ELF 64-bit LSB  executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=8cf14391ff99dac98b66de18ae92c887122a7a13, stripped
martin@martin:~/pocotest$ 
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/Net/HTTPMessage.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/HTTPClientSession.h"
#include <iostream>
using Poco::Net::HTTPClientSession;
using Poco::Net::HTTPRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPMessage;
using Poco::Net::WebSocket;
int main(int args,char **argv)
{
HTTPClientSession cs("echo.websocket.org",80);
HTTPRequest request(HTTPRequest::HTTP_GET, "/?encoding=text",HTTPMessage::HTTP_1_1);
request.set("origin", "http://www.websocket.org");
HTTPResponse response;
try {
WebSocket* m_psock = new WebSocket(cs, request, response);
char const *testStr="Hello echo websocket!";
char receiveBuff[256];
int len=m_psock->sendFrame(testStr,strlen(testStr),WebSocket::FRAME_TEXT);
std::cout << "Sent bytes " << len << std::endl;
int flags=0;
int rlen=m_psock->receiveFrame(receiveBuff,256,flags);
std::cout << "Received bytes " << rlen << std::endl;
std::cout << receiveBuff << std::endl;
m_psock->close();
} catch (std::exception &e) {
std::cout << "Exception " << e.what();
}
}
@carrot-cookie
Copy link

Thanks to you, I realized how to work with a WebSocket in POCO. Thank you very much. Very clear and simple example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment