[DEV] add websocket nearly correct
This commit is contained in:
72
test/main-client-websocket.cpp
Normal file
72
test/main-client-websocket.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <test-debug/debug.h>
|
||||
#include <enet/Tcp.h>
|
||||
#include <enet/TcpClient.h>
|
||||
#include <enet/Http.h>
|
||||
#include <enet/WebSocket.h>
|
||||
#include <etk/etk.h>
|
||||
|
||||
#include <etk/stdTools.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace appl {
|
||||
void onReceiveData(std::vector<uint8_t>& _data, bool _isString) {
|
||||
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);
|
||||
for (int32_t iii=0; iii<_argc ; ++iii) {
|
||||
std::string data = _argv[iii];
|
||||
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("==================================");
|
||||
#ifndef __TARGET_OS__Windows
|
||||
// connect on TCP server:
|
||||
enet::Tcp tcpConnection = std::move(enet::connectTcpClient("127.0.0.1", 12345));
|
||||
// TODO : Check if connection is valid ...
|
||||
|
||||
// Create a HTTP connection in Client mode
|
||||
enet::WebSocket connection(std::move(tcpConnection), false);
|
||||
// Set callbacks:
|
||||
connection.connect(appl::onReceiveData);
|
||||
|
||||
// start http connection (the actual state is just TCP start ...)
|
||||
connection.start("plop.txt");
|
||||
|
||||
// send some data to play ...
|
||||
connection.write("coucou comment ca vas ???");
|
||||
|
||||
int32_t timeout = 20;
|
||||
while (connection.isAlive() == true
|
||||
&& timeout > 0) {
|
||||
usleep(100000);
|
||||
timeout--;
|
||||
}
|
||||
|
||||
#else
|
||||
TEST_CRITICAL("not implemented");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
@@ -20,20 +20,20 @@ namespace appl {
|
||||
TEST_INFO("Receive Header data:");
|
||||
_data.display();
|
||||
if (_data.getType() == enet::HTTPReqType::GET) {
|
||||
if (_data.getUri() == "http://127.0.0.1:12345/plop.txt") {
|
||||
if (_data.getUri() == "plop.txt") {
|
||||
enet::HttpAnswer answer(enet::HTTPAnswerCode::c200_ok);
|
||||
std::string data = "<html><head></head></body>coucou</body></html>";
|
||||
answer.setKey("Content-Length", etk::to_string(data.size()));
|
||||
_interface->setHeader(answer);
|
||||
_interface->write(data);
|
||||
_interface->stop();
|
||||
_interface->stop(true);
|
||||
return;
|
||||
}
|
||||
}
|
||||
enet::HttpAnswer answer(enet::HTTPAnswerCode::c404_notFound);
|
||||
answer.setKey("Connection", "close");
|
||||
_interface->setHeader(answer);
|
||||
_interface->stop();
|
||||
_interface->stop(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
87
test/main-server-websocket.cpp
Normal file
87
test/main-server-websocket.cpp
Normal file
@@ -0,0 +1,87 @@
|
||||
/** @file
|
||||
* @author Edouard DUPIN
|
||||
* @copyright 2014, Edouard DUPIN, all right reserved
|
||||
* @license APACHE v2.0 (see license file)
|
||||
*/
|
||||
|
||||
#include <test-debug/debug.h>
|
||||
#include <enet/Tcp.h>
|
||||
#include <enet/Http.h>
|
||||
#include <enet/WebSocket.h>
|
||||
#include <enet/TcpServer.h>
|
||||
#include <etk/etk.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <etk/stdTools.h>
|
||||
namespace appl {
|
||||
void onReceiveData(enet::WebSocket* _interface, std::vector<uint8_t>& _data, bool _isString) {
|
||||
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] << "'");
|
||||
_interface->write("Très bien, merci ...");
|
||||
} else {
|
||||
TEST_INFO("binary data: ... ");
|
||||
}
|
||||
}
|
||||
bool onReceiveUri(enet::WebSocket* _interface, const std::string& _uri) {
|
||||
TEST_INFO("Receive Header uri: " << _uri);
|
||||
if (_uri == "plop.txt") {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int _argc, const char *_argv[]) {
|
||||
etk::init(_argc, _argv);
|
||||
for (int32_t iii=0; iii<_argc ; ++iii) {
|
||||
std::string data = _argv[iii];
|
||||
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 server ==");
|
||||
TEST_INFO("==================================");
|
||||
#ifndef __TARGET_OS__Windows
|
||||
//Wait on TCP connection:
|
||||
enet::TcpServer interface;
|
||||
// Configure server interface:
|
||||
interface.setHostNane("127.0.0.1");
|
||||
interface.setPort(12345);
|
||||
// Start listening ...
|
||||
interface.link();
|
||||
// Wait a new connection ..
|
||||
enet::Tcp tcpConnection = std::move(interface.waitNext());
|
||||
// Free Connected port
|
||||
interface.unlink();
|
||||
// TODO : Check if connection is valid ...
|
||||
|
||||
// Create a HTTP connection in Server mode
|
||||
enet::WebSocket connection(std::move(tcpConnection), true);
|
||||
enet::WebSocket* tmp = &connection;
|
||||
// Set callbacks:
|
||||
connection.connect([=](std::vector<uint8_t>& _value, bool _isString){
|
||||
appl::onReceiveData(tmp, _value, _isString);
|
||||
});
|
||||
connection.connectUri([=](const std::string& _value){
|
||||
return appl::onReceiveUri(tmp, _value);
|
||||
});
|
||||
|
||||
// start http connection (the actual state is just TCP start ...)
|
||||
connection.start();
|
||||
|
||||
while (connection.isAlive() == true) {
|
||||
usleep(100000);
|
||||
}
|
||||
#else
|
||||
TEST_CRITICAL("not implemented");
|
||||
#endif
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user