server code has a callback that takes a websocket

This commit is contained in:
Benjamin Sergeant 2018-12-30 22:12:13 -08:00
parent 379a845166
commit b2eb07db14
3 changed files with 43 additions and 20 deletions

View File

@ -19,6 +19,28 @@ int main(int argc, char** argv)
} }
ix::WebSocketServer server(port); ix::WebSocketServer server(port);
server.setOnConnectionCallback(
[](ix::WebSocket& webSocket)
{
webSocket.setOnMessageCallback(
[&webSocket](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo,
const ix::WebSocketHttpHeaders& headers)
{
if (messageType == ix::WebSocket_MessageType_Message)
{
std::cout << str << std::endl;
webSocket.send(str);
}
}
);
}
);
auto res = server.listen(); auto res = server.listen();
if (!res.first) if (!res.first)
{ {

View File

@ -9,7 +9,6 @@
#include "IXWebSocket.h" #include "IXWebSocket.h"
#include <sstream> #include <sstream>
#include <iostream>
#include <netdb.h> #include <netdb.h>
#include <stdio.h> #include <stdio.h>
@ -30,6 +29,11 @@ namespace ix
} }
void WebSocketServer::setOnConnectionCallback(const OnConnectionCallback& callback)
{
_onConnectionCallback = callback;
}
std::pair<bool, std::string> WebSocketServer::listen() std::pair<bool, std::string> WebSocketServer::listen()
{ {
struct sockaddr_in server; /* server address information */ struct sockaddr_in server; /* server address information */
@ -102,8 +106,7 @@ namespace ix
if ((clientFd = accept(_serverFd, (struct sockaddr *)&client, &addressLen)) == -1) if ((clientFd = accept(_serverFd, (struct sockaddr *)&client, &addressLen)) == -1)
{ {
std::cerr << "WebSocketServer::run() error accepting connection: " std::cerr << "WebSocketServer::run() error accepting connection: "
<< strerror(errno) << strerror(errno);
<< std::endl;
continue; continue;
} }
@ -111,29 +114,18 @@ namespace ix
} }
} }
//
// FIXME: make sure we never run into reconnectPerpetuallyIfDisconnected
//
void WebSocketServer::handleConnection(int fd) void WebSocketServer::handleConnection(int fd)
{ {
ix::WebSocket webSocket; ix::WebSocket webSocket;
webSocket.setSocketFileDescriptor(fd); webSocket.setSocketFileDescriptor(fd);
webSocket.setOnMessageCallback(
[&webSocket](ix::WebSocketMessageType messageType,
const std::string& str,
size_t wireSize,
const ix::WebSocketErrorInfo& error,
const ix::WebSocketCloseInfo& closeInfo,
const ix::WebSocketHttpHeaders& headers)
{
if (messageType == ix::WebSocket_MessageType_Message)
{
std::cout << str << std::endl;
webSocket.send(str);
}
}
);
webSocket.start(); webSocket.start();
_onConnectionCallback(webSocket);
// We can probably do better than this busy loop, with a condition variable.
for (;;) for (;;)
{ {
std::chrono::duration<double, std::milli> wait(10); std::chrono::duration<double, std::milli> wait(10);

View File

@ -10,14 +10,21 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <functional>
#include "IXWebSocket.h"
namespace ix namespace ix
{ {
using OnConnectionCallback = std::function<void(WebSocket&)>;
class WebSocketServer { class WebSocketServer {
public: public:
WebSocketServer(int port = 8080, int backlog = 5); WebSocketServer(int port = 8080, int backlog = 5);
virtual ~WebSocketServer(); virtual ~WebSocketServer();
void setOnConnectionCallback(const OnConnectionCallback& callback);
std::pair<bool, std::string> listen(); std::pair<bool, std::string> listen();
void run(); void run();
@ -27,6 +34,8 @@ namespace ix
int _port; int _port;
int _backlog; int _backlog;
OnConnectionCallback _onConnectionCallback;
// socket for accepting connections // socket for accepting connections
int _serverFd; int _serverFd;