-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameclient.cpp
36 lines (26 loc) · 931 Bytes
/
gameclient.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "gameclient.h"
#include <QTextCodec>
#include <QThread>
GameClient::GameClient(QObject *parent) : QObject(parent) { }
void GameClient::connectToServer(QHostAddress addr, int port) {
QThread::msleep(50);
sock = new QTcpSocket(this);
sock->bind();
sock->connectToHost(addr, port);
connect(sock, SIGNAL(readyRead()), this, SLOT(onSocketData()));
connect(sock, SIGNAL(connected()), this, SIGNAL(onConnected()));
}
void GameClient::sendData(QString str) {
auto data = QTextCodec::codecForName("Utf-8")->fromUnicode(str + '$');
sock->write(data);
sock->flush();
}
void GameClient::onSocketData() {
auto data = sock->readAll();
QString cont = QTextCodec::codecForName("Utf-8")->toUnicode(data);
auto cmds = cont.split('$');
qDebug() << "[Client]socket recieved raw data: " << cont;
for (QString v : cmds) {
if (v.length() > 0) emit onMessage(v);
}
}