Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void ChatServer::readSocket()
- {
- qDebug() << Q_FUNC_INFO;
- QBluetoothSocket *socket = qobject_cast<QBluetoothSocket*>(sender());
- if (socket == NULL)
- return;
- while (socket->canReadLine()) {
- QByteArray array = socket->readLine();
- QMap<QString,QVariant> map;
- {
- QDataStream data(&array, QIODevice::ReadOnly);
- data >> map;
- }
- qDebug() << array;
- qDebug() << map;
- QString messageType = map.value(BT_MESSENGER_MESSAGE_TYPE).toString();
- QString message = map.value(BT_MESSENGER_MESSAGE).toString();
- QString sender = map.value(BT_MESSENGER_SENDER).toString();
- if (messageType == MESSAGE_IS_TYPING) {
- bool isTyping = false;
- if (message == USER_IS_TYPING)
- isTyping = true;
- emit clientTyping(sender, isTyping);
- } else if (messageType == CLIENT_REQUEST_LIST) {
- QStringList clientNames;
- foreach(QBluetoothSocket *client, clientSockets)
- clientNames << client->peerName();
- QMap<QString,QVariant> map;
- map.insert(BT_MESSENGER_MESSAGE_TYPE, MESSAGE_IS_CLIENTLIST);
- map.insert(BT_MESSENGER_MESSAGE, clientNames);
- QByteArray array;
- {
- QDataStream data(&array, QIODevice::ReadOnly);
- data << map;
- }
- socket->write(array);
- return;
- } else if (messageType == MESSAGE_IS_MESSAGE) {
- emit messageReceived(sender, message);
- }
- // In order to have clients connect to one server, we need to re-send
- // the messages received by the server to all clients, we also need to
- // include the name of the client that originally sent the message,
- // this eliminates the need to have clients connect to each other, and
- // thus saves on the 8-device limit
- foreach (QBluetoothSocket *clientSocket, clientSockets)
- clientSocket->write(array);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement