00001 /**************************************************************************** 00002 ** 00003 ** Copyright (C) 2016 Kurt Pattyn <pattyn.kurt@gmail.com>. 00004 ** Contact: https://www.qt.io/licensing/ 00005 ** 00006 ** This file is part of the QtWebSockets module of the Qt Toolkit. 00007 ** 00008 ** $QT_BEGIN_LICENSE:BSD$ 00009 ** Commercial License Usage 00010 ** Licensees holding valid commercial Qt licenses may use this file in 00011 ** accordance with the commercial license agreement provided with the 00012 ** Software or, alternatively, in accordance with the terms contained in 00013 ** a written agreement between you and The Qt Company. For licensing terms 00014 ** and conditions see https://www.qt.io/terms-conditions. For further 00015 ** information use the contact form at https://www.qt.io/contact-us. 00016 ** 00017 ** BSD License Usage 00018 ** Alternatively, you may use this file under the terms of the BSD license 00019 ** as follows: 00020 ** 00021 ** "Redistribution and use in source and binary forms, with or without 00022 ** modification, are permitted provided that the following conditions are 00023 ** met: 00024 ** * Redistributions of source code must retain the above copyright 00025 ** notice, this list of conditions and the following disclaimer. 00026 ** * Redistributions in binary form must reproduce the above copyright 00027 ** notice, this list of conditions and the following disclaimer in 00028 ** the documentation and/or other materials provided with the 00029 ** distribution. 00030 ** * Neither the name of The Qt Company Ltd nor the names of its 00031 ** contributors may be used to endorse or promote products derived 00032 ** from this software without specific prior written permission. 00033 ** 00034 ** 00035 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00036 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00037 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00038 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00039 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00040 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00041 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00042 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00043 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00044 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00045 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." 00046 ** 00047 ** $QT_END_LICENSE$ 00048 ** 00049 ****************************************************************************/ 00050 #include "client.h" 00051 #include <QtCore/QDebug> 00052 #include <QtMath> 00053 #include <QThread> 00054 #include <chrono> 00055 #include <QStringBuilder> 00056 #include <thread> 00057 00058 #ifdef WIN32 00059 #include <Windows.h> 00060 #endif 00061 00062 QT_USE_NAMESPACE 00063 00065 Client::Client(const QUrl &url, bool debug, QObject *parent) : 00066 QObject(parent), 00067 m_url(url), 00068 m_debug(debug),timer_(this),timer2_(this) 00069 { 00070 if (m_debug) 00071 qDebug() << "WebSocket server:" << url; 00072 connect(&m_webSocket, &QWebSocket::connected, this, &Client::onConnected); 00073 connect(&m_webSocket, &QWebSocket::disconnected, this, &Client::closed); 00074 m_webSocket.open(QUrl(url)); 00075 } 00077 00079 void Client::onConnected() 00080 { 00081 if (m_debug) 00082 qDebug() << "WebSocket connected"; 00083 m_webSocket.sendTextMessage("start"); 00084 00085 i_ = 0.0; 00086 00087 timer_.setInterval(50); 00088 timer2_.setInterval(10); 00089 double i=0.0; 00090 connect(&timer_, &QTimer::timeout, [&](){ 00091 sendMsg("sin"); 00092 }); 00093 connect(&timer2_, &QTimer::timeout, [&](){ 00094 sendMsg("cos"); 00095 }); 00096 00097 timer_.start(); 00098 timer2_.start(); 00099 } 00100 00101 void Client::sendMsg(const QString &key){ 00102 using namespace std::chrono; 00103 static std::chrono::high_resolution_clock::time_point initial_time = high_resolution_clock::now(); 00104 00105 double steps = 1000.0; 00106 i_ +=M_PI/steps; 00107 00108 double value = qSin(i_); 00109 if(key=="cos") 00110 value = qCos(i_); 00111 auto now = high_resolution_clock::now(); 00112 const double t = duration_cast< duration<double>>( now - initial_time ).count() ; 00113 QString str = key+QString(":") 00114 + QString::number(t)+":" 00115 + QString::number(value); 00116 qDebug() << str; 00117 m_webSocket.sendTextMessage(str); 00118 } 00120 00122 void Client::onTextMessageReceived(QString message) 00123 { 00124 if (m_debug) 00125 qDebug() << "Message received:" << message; 00126 m_webSocket.close(); 00127 }