I am attempting to connect to the Twitch chat server via SSL and I basically copied and pasted some code from the Secure SSL Connection example in QT. When I can connectToHostEncrypted
it all crashed. Any help is greatly appreciated
Relative code:
void MainWindow::secureConnect()
{
if (!socket) {
socket = new QSslSocket(this);
connect(socket, SIGNAL(stateChanged(QAbstractSocket::SocketState)),
this, SLOT(socketStateChanged(QAbstractSocket::SocketState)));
connect(socket, SIGNAL(encrypted()),
this, SLOT(socketEncrypted()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
this, SLOT(socketError(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(sslErrors(QList<QSslError>)),
this, SLOT(sslErrors(QList<QSslError>)));
connect(socket, SIGNAL(readyRead()),
this, SLOT(socketReadyRead()));
}
qDebug() << 2;
socket->connectToHostEncrypted("irc.chat.twitch.tv", 443);
QEventLoop loop;
connect(socket, SIGNAL(connected()), &loop, SLOT(quit()));
loop.exec();
qDebug() << 5;
sendData("PASS oauth:" + TwitchAccessToken);
sendData("NICK ThatRedstoneGT");
sendData("JOIN smartguy316");
sendData("PRIVMSG Hello!");
}
Declaration:
Public:
QSslSocket *socket:
Putting this as an answer as I'm not sure I've managed to get my point across in the comments.
You have a class MainWindow
with a member socket
. Keeping things to a minimum let's say it's...
class MainWindow {
public:
MainWindow();
void secureConnect();
void sendData(QString text);
QSslSocket *socket:
};
Now, I would expect the constructor implementation to look something like...
MainWindow::MainWindow ()
: socket(nullptr)
{}
If you don't initialize socket
to nullptr
before MainWindow::secureConnect
is called then it will have some random value. In which case...
if (!socket) {
will fail meaning that the block containing...
socket = new QSslSocket(this);
will never be executed.
By way of a test, can you run your code under a debugger, set a breakpoint on the line...
if (!socket) {
and check the value of socket
. The first time that line is hit socket
should be null. Is it?