enet/test/main-client-websocket.cpp

74 lines
2.0 KiB
C++
Raw Normal View History

2016-06-19 22:05:34 +02:00
/** @file
* @author Edouard DUPIN
* @copyright 2014, Edouard DUPIN, all right reserved
* @license MPL v2.0 (see license file)
2016-06-19 22:05:34 +02:00
*/
2016-09-30 22:28:36 +02:00
#include <test-debug/debug.hpp>
#include <enet/enet.hpp>
2016-09-30 22:28:36 +02:00
#include <enet/Tcp.hpp>
#include <enet/TcpClient.hpp>
#include <enet/Http.hpp>
#include <enet/WebSocket.hpp>
#include <etk/etk.hpp>
2016-06-19 22:05:34 +02:00
2016-09-30 22:28:36 +02:00
#include <etk/stdTools.hpp>
2016-10-13 21:29:18 +02:00
2016-06-19 22:05:34 +02:00
namespace appl {
2017-08-28 00:04:49 +02:00
void onReceiveData(etk::Vector<uint8_t>& _data, bool _isString) {
2016-06-19 22:05:34 +02:00
TEST_INFO("Receive Datas : " << _data.size() << " bytes");
if (_isString == true) {
_data.resize(_data.size()+1);
_data[_data.size()-1] = '\0';
TEST_INFO("string data: '" << (char*)&_data[0] << "'");
} else {
TEST_INFO("binary data: ... ");
}
}
}
int main(int _argc, const char *_argv[]) {
etk::init(_argc, _argv);
enet::init(_argc, _argv);
2016-06-19 22:05:34 +02:00
for (int32_t iii=0; iii<_argc ; ++iii) {
2017-08-28 00:04:49 +02:00
etk::String data = _argv[iii];
2016-06-19 22:05:34 +02:00
if ( data == "-h"
|| data == "--help") {
TEST_PRINT(etk::getApplicationName() << " - help : ");
TEST_PRINT(" " << _argv[0] << " [options]");
TEST_PRINT(" No options ...");
return -1;
}
}
TEST_INFO("==================================");
TEST_INFO("== Test WebSocket client ==");
TEST_INFO("==================================");
// connect on TCP server:
2017-08-28 00:04:49 +02:00
enet::Tcp tcpConnection = etk::move(enet::connectTcpClient("127.0.0.1", 12345));
2016-06-19 22:05:34 +02:00
// TODO : Check if connection is valid ...
// Create a HTTP connection in Client mode
2017-08-28 00:04:49 +02:00
enet::WebSocket connection(etk::move(tcpConnection), false);
2016-06-19 22:05:34 +02:00
// Set callbacks:
connection.connect(appl::onReceiveData);
// start http connection (the actual state is just TCP start ...)
2017-08-28 00:04:49 +02:00
etk::Vector<etk::String> protocols;
protocols.pushBack("test1526/1.0");
protocols.pushBack("test1526/1.5");
protocols.pushBack("Hello");
connection.start("/plop.txt", protocols);
2016-06-19 22:05:34 +02:00
// send some data to play ...
connection.write("coucou comment ca vas ???");
int32_t timeout = 20;
while (connection.isAlive() == true
&& timeout > 0) {
2017-09-14 00:59:21 +02:00
ethread::sleepMilliSeconds((100));
2016-06-19 22:05:34 +02:00
timeout--;
}
return 0;
}