Hi,
I am using QWebChannel to expose easy communication interface to connected web browser clients.
Also I have overridden QWebChannelAbstractTransport interface which sends and receives data from/to QWebSocket.
When my client connects and all of the code runs in event loop of main thread all works great,
but when specific action occurs in the application I need to create new thread and move all of the client resources to this thread.
I have properly implemented parent->child hierarchy for moveToThread() operation.
When moveToThread() operation completes successfully and all object runs in new event loop (<- I think) clients can easily send me a message and I can easily process it in new thread,
but when my application trying to send message to client I am receiving console message error: "QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread".
I tried to debug it by printing threads in which objects or called methods works and it seems that everything is fine, but doesn't work.
TESTCASE
Part of my QWebChannelAbstractTransport implementation:
Output from qDebug line 11 looks like this: "Client in msg from thread >>> QThread(0x6aa270)"
Output from qDebug line 3 looks like this: "Client out msg from thread <<< QThread(0x6aa270) (socket: QThread(0x6aa270) )"
So it looks like all of object runs in the same thread but in line 6 I have a message: "QSocketNotifier: Socket notifiers cannot be enabled or disabled from another thread"
Of course socket is QWebSocket object.
void ClientTransportLayer::sendMessage(const QJsonObject &message)
{
qDebug() <<"Client out msg from thread <<< " <<QThread::currentThread() <<" (socket: " <<socket->thread() <<")";
QJsonDocument doc(message);
socket
->sendTextMessage
(QString::fromUtf8(doc.
toJson(QJsonDocument
::Compact)));
}
void ClientTransportLayer
::textMessageReceived(const QString &messageData
) {
qDebug() <<"Client in msg from thread >>> " <<QThread::currentThread();
QJsonParseError error;
QJsonDocument message = QJsonDocument::fromJson(messageData.toUtf8(), &error);
if(!error.error && message.isObject())
emit messageReceived(message.object(), this);
}
void ClientTransportLayer::sendMessage(const QJsonObject &message)
{
qDebug() <<"Client out msg from thread <<< " <<QThread::currentThread() <<" (socket: " <<socket->thread() <<")";
QJsonDocument doc(message);
socket->sendTextMessage(QString::fromUtf8(doc.toJson(QJsonDocument::Compact)));
}
void ClientTransportLayer::textMessageReceived(const QString &messageData)
{
qDebug() <<"Client in msg from thread >>> " <<QThread::currentThread();
QJsonParseError error;
QJsonDocument message = QJsonDocument::fromJson(messageData.toUtf8(), &error);
if(!error.error && message.isObject())
emit messageReceived(message.object(), this);
}
To copy to clipboard, switch view to plain text mode
Bookmarks