File: Server_ws.cpp

package info (click to toggle)
cgal 6.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,952 kB
  • sloc: cpp: 811,597; ansic: 208,576; sh: 493; python: 411; makefile: 286; javascript: 174
file content (100 lines) | stat: -rw-r--r-- 3,286 bytes parent folder | download | duplicates (3)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include <QNetworkInterface>
#include <QApplication>
#include <QMessageBox>
#include "Server_ws.h"

#include <iostream>


EchoServer::EchoServer(quint16 port) :
  QObject(),
  m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"),
                                          QWebSocketServer::NonSecureMode, this))
{
  if (m_pWebSocketServer->listen(QHostAddress::Any, port)) {
    connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
            this, &EchoServer::onNewConnection);
    connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &EchoServer::closed);
  }
  QHostAddress local_host("0.0.0.0");

  //to avoid printing 127.0.0.1. Not really sure it won't ever print the external ipv4 though.
  const QHostAddress &localhost = QHostAddress(QHostAddress::LocalHost);
  for (const QHostAddress &address: QNetworkInterface::allAddresses()) {
    if (address.protocol() == QAbstractSocket::IPv4Protocol && address != localhost)
    {
      local_host= address;
      break;
    }
  }
  QMessageBox mb(QMessageBox::NoIcon, "WS Server",
                 tr("WebSockets Server started.\nEnter the following address in\nyour Network Preferences to be able to join it :\n"
                    "ws://%1:%2").arg(local_host.toString()).arg(port), QMessageBox::Ok);
  mb.setTextInteractionFlags(Qt::TextSelectableByMouse);
  mb.exec();
}

EchoServer::~EchoServer()
{
  m_pWebSocketServer->close();
  qDeleteAll(m_clients.begin(), m_clients.end());
}

void EchoServer::onNewConnection()
{
  QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();

  connect(pSocket, &QWebSocket::textMessageReceived, this, &EchoServer::processTextMessage);
  connect(pSocket, &QWebSocket::binaryMessageReceived, this, &EchoServer::processBinaryMessage);
  connect(pSocket, &QWebSocket::disconnected, this, &EchoServer::socketDisconnected);

  m_clients << pSocket;
}

void EchoServer::processTextMessage(QString message)
{
  QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
  for(auto *client : m_clients) {
    if(client != pClient)
      client->sendTextMessage(message);
  }
}

void EchoServer::processBinaryMessage(QByteArray message)
{
  QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
  if (pClient) {
    pClient->sendBinaryMessage(message);
  }
}

void EchoServer::socketDisconnected()
{
  QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
  if (pClient) {
    m_clients.removeAll(pClient);
    pClient->deleteLater();
  }
}

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  QCommandLineParser parser;
  parser.setApplicationDescription("WS Server");
  parser.addHelpOption();
  QCommandLineOption portOption(QStringList() << "p" << "port",
                                QCoreApplication::translate("main", "Port for echoserver [default: 1234]."),
                                QCoreApplication::translate("main", "port"), QLatin1String("1234"));
  parser.addOption(portOption);
  parser.process(a);
  int port = parser.value(portOption).toInt();
  EchoServer *server = new EchoServer(port);
  QObject::connect(server, &EchoServer::closed, &a, &QCoreApplication::quit);

  return a.exec();
}