WebsocketConnection.cpp 893 Bytes
Newer Older
1
#include "WebsocketConnection.h"
2
3
#include "TcpProtocol.h"
#include "MsgPackProtocol.h"
Hubert Denkmair's avatar
Hubert Denkmair committed
4
#include "JsonProtocol.h"
5

Hubert Denkmair's avatar
Hubert Denkmair committed
6
7
WebsocketConnection::WebsocketConnection(uWS::WebSocket<uWS::SERVER> *websocket)
	: _websocket(websocket)
8
9
10
11
12
{
}

void WebsocketConnection::FrameComplete(uint64_t frame_id, const TcpProtocol &proto)
{
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
13
	(void) frame_id;
Hubert Denkmair's avatar
Hubert Denkmair committed
14
15
	if (!_firstFrameSent)
	{
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
16
		sendInitialData(proto);
Hubert Denkmair's avatar
Hubert Denkmair committed
17
18
		_firstFrameSent = true;
	}
19
}
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
20

21
22
23
24
25
26
void WebsocketConnection::LogMessage(uint64_t frame_id, const std::string &message)
{
	json j = { { "t", "Log" }, { "frame", frame_id }, { "msg", message} };
	sendString(j.dump());
}

Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
27
28
void WebsocketConnection::sendInitialData(const TcpProtocol &proto)
{
Hubert Denkmair's avatar
Hubert Denkmair committed
29
30
31
	sendString(json(proto.GetGameInfo()).dump());
	sendString(json(proto.GetWorldUpdate()).dump());
}
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
32

Hubert Denkmair's avatar
Hubert Denkmair committed
33
34
35
void WebsocketConnection::sendString(std::string data)
{
	_websocket->send(data.data(), data.length(), uWS::OpCode::TEXT);
Hubert Denkmair's avatar
cleanup    
Hubert Denkmair committed
36
}