67 lines
2.4 KiB
C++
67 lines
2.4 KiB
C++
|
// Copyright (c) 2021 hiimgoodpack
|
||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||
|
// this software and associated documentation files (the "Software"), to deal in
|
||
|
// the Software without restriction, including without limitation the rights to
|
||
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||
|
// of the Software, and to permit persons to whom the Software is furnished to do
|
||
|
// so, subject to the following conditions:
|
||
|
// The above copyright notice and this permission notice shall be included in all
|
||
|
// copies or substantial portions of the Software.
|
||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
// SOFTWARE.
|
||
|
|
||
|
#include "http.h"
|
||
|
#include "websocket.h"
|
||
|
|
||
|
#include <string>
|
||
|
#include <cstring> // std::strncmp
|
||
|
#include <cstdint>
|
||
|
#include <stdexcept>
|
||
|
#include <iostream>
|
||
|
|
||
|
enum State {
|
||
|
WAIT_HELLO,
|
||
|
WAIT_ACK,
|
||
|
READY
|
||
|
};
|
||
|
int main(int argc, char* argv[]) {
|
||
|
curl_global_init(CURL_GLOBAL_ALL);
|
||
|
|
||
|
Http::Request tokenRequest("http://localhost:3005/api/v1/users/token/create");
|
||
|
tokenRequest.setPostFields("username=TestUser&password=12345678&alsoSetCookie=true}");
|
||
|
tokenRequest.perform();
|
||
|
std::cout << tokenRequest.status << " " << tokenRequest.buffer;
|
||
|
WebSocket socket("ws://localhost:3005/gateway");
|
||
|
State state = WAIT_HELLO;
|
||
|
socket.onReceive = [&](std::string message) {
|
||
|
std::cout << "Received: " << message << "\n";
|
||
|
switch (state) {
|
||
|
case WAIT_HELLO: {
|
||
|
if (std::strncmp(message.c_str(), "0@", 2) == 0) {
|
||
|
state = WAIT_ACK;
|
||
|
socket.send("1@{\"token\":\"my totally real token\"");
|
||
|
} else {
|
||
|
std::cerr << "Failed authentication: Expected packet type 0 (HELLO), received buffer: " << message << "\n";
|
||
|
socket.close();
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
case WAIT_ACK: {
|
||
|
if (std::strncmp(message.c_str(), "2@", 2) == 0) {
|
||
|
state = READY;
|
||
|
} else {
|
||
|
std::cerr << "Failed authentication: Expected packet type 2 (YOO_ACK), received buffer: " << message << "\n";
|
||
|
socket.close();
|
||
|
}
|
||
|
}
|
||
|
case READY: break;
|
||
|
}
|
||
|
};
|
||
|
socket.run();
|
||
|
}
|