Guest User

Untitled

a guest
Oct 3rd, 2011
39
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void ChatServer::readSocket()
  2. {
  3.     qDebug() << Q_FUNC_INFO;
  4.     QBluetoothSocket *socket = qobject_cast<QBluetoothSocket*>(sender());
  5.  
  6.     if (socket == NULL)
  7.         return;
  8.  
  9.     while (socket->canReadLine()) {
  10.         QByteArray array = socket->readLine();
  11.         QMap<QString,QVariant> map;
  12.         {
  13.             QDataStream data(&array, QIODevice::ReadOnly);
  14.             data >> map;
  15.         }
  16.         qDebug() << array;
  17.         qDebug() << map;
  18.  
  19.         QString messageType = map.value(BT_MESSENGER_MESSAGE_TYPE).toString();
  20.         QString message = map.value(BT_MESSENGER_MESSAGE).toString();
  21.         QString sender = map.value(BT_MESSENGER_SENDER).toString();
  22.  
  23.         if (messageType == MESSAGE_IS_TYPING) {
  24.             bool isTyping = false;
  25.  
  26.             if (message == USER_IS_TYPING)
  27.                 isTyping = true;
  28.  
  29.             emit clientTyping(sender, isTyping);
  30.         } else if (messageType == CLIENT_REQUEST_LIST) {
  31.             QStringList clientNames;
  32.             foreach(QBluetoothSocket *client, clientSockets)
  33.                 clientNames << client->peerName();
  34.  
  35.             QMap<QString,QVariant> map;
  36.             map.insert(BT_MESSENGER_MESSAGE_TYPE, MESSAGE_IS_CLIENTLIST);
  37.             map.insert(BT_MESSENGER_MESSAGE, clientNames);
  38.             QByteArray array;
  39.             {
  40.                 QDataStream data(&array, QIODevice::ReadOnly);
  41.                 data << map;
  42.             }
  43.  
  44.             socket->write(array);
  45.             return;
  46.         } else if (messageType == MESSAGE_IS_MESSAGE) {
  47.             emit messageReceived(sender, message);
  48.         }
  49.  
  50.         // In order to have clients connect to one server, we need to re-send
  51.         // the messages received by the server to all clients, we also need to
  52.         // include the name of the client that originally sent the message,
  53.         // this eliminates the need to have clients connect to each other, and
  54.         // thus saves on the 8-device limit
  55.         foreach (QBluetoothSocket *clientSocket, clientSockets)
  56.             clientSocket->write(array);
  57.     }
  58. }
  59.  
RAW Paste Data