Merge branch 'main' into ShipSpawn
This commit is contained in:
commit
70ef730e86
13 files changed
+292
-152
No files matched your search
@@ -418,11 +418,13 @@ namespace ZL {
|
||||
pinfo.position = pr.pos;
|
||||
pinfo.rotation = dir.toRotationMatrix();
|
||||
pinfo.velocity = velocity;
|
||||
pendingProjectiles.push_back(pinfo);
|
||||
|
||||
std::cout << "LocalClient: Created projectile at pos (" << shotPos.x() << ", "
|
||||
if (pinfo.shooterId != GetClientId()) {
|
||||
pendingProjectiles.push_back(pinfo);
|
||||
}
|
||||
std::cout << "LocalClient: Created server projectile at pos (" << shotPos.x() << ", "
|
||||
<< shotPos.y() << ", " << shotPos.z() << ") vel (" << pr.vel.x() << ", "
|
||||
<< pr.vel.y() << ", " << pr.vel.z() << ")" << std::endl;
|
||||
<< pr.vel.y() << ", " << pr.vel.z() << ") shooter=" << pr.shooterId << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
namespace ZL {
|
||||
void WebSocketClientEmscripten::Connect(const std::string& host, uint16_t port) {
|
||||
// Формируем URL. Обратите внимание, что в Web часто лучше использовать ws://localhost
|
||||
std::string url = "ws://" + host + ":" + std::to_string(port);
|
||||
//std::string url = "wss://api.spacegame.fishrungames.com";
|
||||
//std::string url = "ws://" + host + ":" + std::to_string(port);
|
||||
std::string url = "wss://api.spacegame.fishrungames.com";
|
||||
|
||||
EmscriptenWebSocketCreateAttributes attr = {
|
||||
url.c_str(),
|
||||
@@ -22,13 +22,32 @@ namespace ZL {
|
||||
emscripten_websocket_set_onmessage_callback(socket_, this, onMessage);
|
||||
emscripten_websocket_set_onerror_callback(socket_, this, onError);
|
||||
emscripten_websocket_set_onclose_callback(socket_, this, onClose);
|
||||
|
||||
connected = false;
|
||||
}
|
||||
|
||||
void WebSocketClientEmscripten::flushOutgoingQueue() {
|
||||
std::lock_guard<std::mutex> lock(outgoingMutex);
|
||||
if (!socket_) return;
|
||||
while (!outgoingQueue.empty()) {
|
||||
const std::string &m = outgoingQueue.front();
|
||||
emscripten_websocket_send_utf8_text(socket_, m.c_str());
|
||||
outgoingQueue.pop();
|
||||
}
|
||||
}
|
||||
|
||||
void WebSocketClientEmscripten::Send(const std::string& message) {
|
||||
if (connected && socket_ > 0) {
|
||||
auto signedMsg = SignMessage(message);
|
||||
std::cout << "[WebWS] Sending message: " << signedMsg << std::endl;
|
||||
emscripten_websocket_send_utf8_text(socket_, signedMsg.c_str());
|
||||
std::string signedMsg = SignMessage(message);
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(outgoingMutex);
|
||||
if (connected && socket_ > 0) {
|
||||
std::cout << "[WebWS] Sending message (immediate): " << signedMsg << std::endl;
|
||||
emscripten_websocket_send_utf8_text(socket_, signedMsg.c_str());
|
||||
return;
|
||||
}
|
||||
outgoingQueue.push(signedMsg);
|
||||
std::cout << "[WebWS] Queued outgoing message (waiting for open): " << signedMsg << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,19 +55,16 @@ namespace ZL {
|
||||
// Локальная очередь для минимизации времени блокировки мьютекса
|
||||
std::queue<std::string> localQueue;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(queueMutex);
|
||||
if (messageQueue.empty()) return;
|
||||
std::swap(localQueue, messageQueue);
|
||||
}
|
||||
if (messageQueue.empty()) return;
|
||||
std::swap(localQueue, messageQueue);
|
||||
|
||||
while (!localQueue.empty()) {
|
||||
const std::string& msg = localQueue.front();
|
||||
std::cout << "[WebWS] Processing message: " << msg << std::endl;
|
||||
std::cout << "[WebWS] Processing message: " << msg << std::endl;
|
||||
|
||||
// Передаем в базовый класс для парсинга игровых событий (BOXES, UPD, и т.д.)
|
||||
HandlePollMessage(msg);
|
||||
|
||||
|
||||
localQueue.pop();
|
||||
}
|
||||
}
|
||||
@@ -59,6 +75,9 @@ namespace ZL {
|
||||
auto* self = static_cast<WebSocketClientEmscripten*>(userData);
|
||||
self->connected = true;
|
||||
std::cout << "[WebWS] Connection opened" << std::endl;
|
||||
|
||||
self->flushOutgoingQueue();
|
||||
|
||||
return EM_TRUE;
|
||||
}
|
||||
|
||||
@@ -67,7 +86,6 @@ namespace ZL {
|
||||
auto* self = static_cast<WebSocketClientEmscripten*>(userData);
|
||||
if (e->isText && e->data) {
|
||||
std::string msg(reinterpret_cast<const char*>(e->data), e->numBytes);
|
||||
std::lock_guard<std::mutex> lock(self->queueMutex);
|
||||
self->messageQueue.push(msg);
|
||||
}
|
||||
return EM_TRUE;
|
||||
|
||||
@@ -18,7 +18,10 @@ namespace ZL {
|
||||
|
||||
// Очередь для хранения сырых строк от браузера
|
||||
std::queue<std::string> messageQueue;
|
||||
std::mutex queueMutex;
|
||||
|
||||
std::queue<std::string> outgoingQueue;
|
||||
std::mutex outgoingMutex;
|
||||
void flushOutgoingQueue();
|
||||
|
||||
public:
|
||||
WebSocketClientEmscripten() = default;
|
||||
|
||||
Reference in new issue
Block a user