Skip to content
Snippets Groups Projects
websocketlinux.cpp 3.91 KiB
/********************************************************************************************
 *                                                                                          *
 * Copyright (C) 2017 Armin Felder, Dennis Beier                                            *
 * This file is part of RocketChatMobileEngine <https://git.fairkom.net/chat/fairchat>.     *
 *                                                                                          *
 * RocketChatMobileEngine is free software: you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by                     *
 * the Free Software Foundation, either version 3 of the License, or                        *
 * (at your option) any later version.                                                      *
 *                                                                                          *
 * RocketChatMobileEngine is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of                           *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                            *
 * GNU General Public License for more details.                                             *
 *                                                                                          *
 * You should have received a copy of the GNU General Public License                        *
 * along with RocketChatMobileEngine. If not, see <http://www.gnu.org/licenses/>.           *
 *                                                                                          *
 ********************************************************************************************/



#include <QWebSocket>

#include "websocketlinux.h"
#include "websocketabstract.h"

websocketLinux::websocketLinux( QObject *parent ): WebsocketAbstract( parent ), mWebsocket( "rocket.chat.mobile", QWebSocketProtocol::VersionLatest, this )
{
    connect( &mWebsocket, &QWebSocket::connected, this, &websocketLinux::connected, Qt::UniqueConnection );
    connect( &mWebsocket, &QWebSocket::textMessageReceived, this, &websocketLinux::received, Qt::UniqueConnection );
    connect( &mWebsocket, &QWebSocket::disconnected, this, &websocketLinux::closed, Qt::UniqueConnection );
    //connect(&websocket,&QWebSocket::error,this,&websocketLinux::error);
    connect( &mWebsocket, &QWebSocket::stateChanged, this, &websocketLinux::onStatusChanged, Qt::UniqueConnection );
}

void websocketLinux::open( QUrl url )
{
    qDebug() << "open url";
    mWebsocket.open( url );
}

void websocketLinux::close()
{
    mWebsocket.close();
}

void websocketLinux::sendTextMessage( QString pMessage )
{
    if ( !mWebsocket.sendTextMessage( pMessage ) ) {
        qWarning() << "ddp did not send message: " << pMessage;
    }
}

bool websocketLinux::isValid()
{
    qDebug() << "state: " << mWebsocket.state();
    return mWebsocket.isValid();
}

bool websocketLinux::isDisconnected()
{
    bool returnValue;

    if ( mWebsocket.state() == QAbstractSocket::UnconnectedState ) {
        returnValue = true;
    } else {
        returnValue = false;
    }
    return returnValue;
}

bool websocketLinux::isConnecting()
{
    bool returnValue;

    if ( mWebsocket.state() == QAbstractSocket::ConnectingState ) {
        returnValue = true;
    } else {
        returnValue = false;
    }

    return returnValue;
}

void websocketLinux::received( QString pMsg )
{
    emit( textMessageReceived( pMsg ) );
}

void websocketLinux::onStatusChanged()
{
    //    if(mWebsocket.state() == QAbstractSocket::UnconnectedState){
    //        emit closed();
    //    }else if(mWebsocket.state() == QAbstractSocket::ConnectedState){
    //        emit connected();
    //    }
    qDebug() << "state linux websocket: " << mWebsocket.state();
}