2018-12-30 08:15:27 +01:00
|
|
|
/*
|
|
|
|
* echo_server.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <ixwebsocket/IXWebSocketServer.h>
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int port = 8080;
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << argv[1];
|
|
|
|
ss >> port;
|
|
|
|
}
|
|
|
|
|
|
|
|
ix::WebSocketServer server(port);
|
2018-12-31 07:12:13 +01:00
|
|
|
|
|
|
|
server.setOnConnectionCallback(
|
2019-01-01 22:53:13 +01:00
|
|
|
[&server](std::shared_ptr<ix::WebSocket> webSocket)
|
2018-12-31 07:12:13 +01:00
|
|
|
{
|
2019-01-01 22:53:13 +01:00
|
|
|
webSocket->setOnMessageCallback(
|
|
|
|
[webSocket, &server](ix::WebSocketMessageType messageType,
|
2018-12-31 07:12:13 +01:00
|
|
|
const std::string& str,
|
|
|
|
size_t wireSize,
|
|
|
|
const ix::WebSocketErrorInfo& error,
|
2019-01-04 02:44:10 +01:00
|
|
|
const ix::WebSocketOpenInfo& openInfo,
|
|
|
|
const ix::WebSocketCloseInfo& closeInfo)
|
2018-12-31 07:12:13 +01:00
|
|
|
{
|
2018-12-31 21:43:47 +01:00
|
|
|
if (messageType == ix::WebSocket_MessageType_Open)
|
2018-12-31 07:12:13 +01:00
|
|
|
{
|
2018-12-31 21:47:42 +01:00
|
|
|
std::cerr << "New connection" << std::endl;
|
2019-01-04 02:44:10 +01:00
|
|
|
std::cerr << "Uri: " << openInfo.uri << std::endl;
|
2018-12-31 21:47:42 +01:00
|
|
|
std::cerr << "Headers:" << std::endl;
|
2019-01-04 02:44:10 +01:00
|
|
|
for (auto it : openInfo.headers)
|
2018-12-31 21:43:47 +01:00
|
|
|
{
|
|
|
|
std::cerr << it.first << ": " << it.second << std::endl;
|
|
|
|
}
|
|
|
|
}
|
2018-12-31 21:47:42 +01:00
|
|
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
|
|
|
{
|
|
|
|
std::cerr << "Closed connection" << std::endl;
|
|
|
|
}
|
2018-12-31 21:43:47 +01:00
|
|
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
|
|
|
{
|
2019-01-02 17:10:39 +01:00
|
|
|
webSocket->send(str);
|
2018-12-31 07:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-12-31 07:00:49 +01:00
|
|
|
auto res = server.listen();
|
2018-12-30 08:15:27 +01:00
|
|
|
if (!res.first)
|
|
|
|
{
|
|
|
|
std::cerr << res.second << std::endl;
|
2018-12-31 07:00:49 +01:00
|
|
|
return 1;
|
2018-12-30 08:15:27 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 23:52:14 +01:00
|
|
|
server.start();
|
2019-01-02 01:11:27 +01:00
|
|
|
server.wait();
|
2018-12-31 07:00:49 +01:00
|
|
|
|
2018-12-30 08:15:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|