This commit is contained in:
azw 2023-08-19 11:23:33 +08:00
parent 8be63ace09
commit 0d795ff0dc
2 changed files with 25 additions and 7 deletions

View File

@ -10,6 +10,8 @@
#include "player.h" #include "player.h"
#include "cs_proto.pb.h"
void Player::Update() void Player::Update()
{ {
@ -57,18 +59,30 @@ void Player::CoLogin(f8::Coroutine* co)
{ {
}; };
web_socket_->Open();
{ {
web_socket_->Open();
while (!net_connected_) { while (!net_connected_) {
co->CoYield(); co->CoYield();
} }
f8::UdpLog::Instance()->Info
("WebSocketClient on_connect ok", {});
}
{
cs::CMJoin msg;
SendMsg(msg);
} }
f8::UdpLog::Instance()->Info
("WebSocketClient on_connect ok", {});
} }
void Player::CoGame(f8::Coroutine* co) void Player::InternalSendMsg(int msgid, ::google::protobuf::Message& msg)
{ {
int packlen = msg.ByteSize();
char* buff = (char*)malloc(sizeof(f8::PackHead) + packlen);
f8::PackHead* head = (f8::PackHead*)buff;
head->packlen = packlen;
head->msgid = msgid;
head->magic_code = f8::MAGIC_CODE;
msg.SerializeToArray(buff + sizeof(f8::PackHead), packlen);
web_socket_->SendBuff(buff, sizeof(f8::PackHead) + packlen);
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <f8/protoutils.h>
namespace a8 namespace a8
{ {
class WebSocketClient; class WebSocketClient;
@ -28,8 +30,10 @@ public:
const std::string& session_id, const std::string& session_id,
std::shared_ptr<a8::WebSocketClient> socket); std::shared_ptr<a8::WebSocketClient> socket);
template <typename T> template <typename T>
void SendNotifyMsg(T& msg) void SendMsg(T& msg)
{ {
static int msgid = f8::Net_GetMessageId(msg);
InternalSendMsg(msgid, msg);
} }
int GetSocketId() { return socket_id_; } int GetSocketId() { return socket_id_; }
bool NetConnected() { return net_connected_; } bool NetConnected() { return net_connected_; }
@ -38,7 +42,7 @@ public:
private: private:
void CoLogin(f8::Coroutine* co); void CoLogin(f8::Coroutine* co);
void CoGame(f8::Coroutine* co); void InternalSendMsg(int msgid, ::google::protobuf::Message& msg);
private: private:
int socket_id_ = 0; int socket_id_ = 0;