Problem with QTcpSocket and javascript client
-
Hi boys. I cant connect from javascript websocket to qtcpserver.
If i use a client like Hercules the conection its ok, but when i use my qt app dont work.
I did it a telnet to my app ip and port and the conection its ok.Any Idea?
Server::Server(QObject *parent) : QTcpServer(parent) { mSocket=nullptr; connect(this,SIGNAL(newConnection()),this,SLOT (proximocliente())); } void Server::proximocliente() { mSocket= nextPendingConnection(); qDebug() << "Nuevo cliente conectado"; connect(mSocket,SIGNAL(readyRead()),this,SLOT(ListoParaLeer())); mSocket->waitForReadyRead(); // qDebug() << mSocket->bytesAvailable(); } void Server::ListoParaLeer() { const int MaxLength = 1024; char buffer[MaxLength + 1]; qint64 byteCount = mSocket->read(buffer, MaxLength); buffer[byteCount] = 0; QByteArray ContentType = "Conexion OK"; qDebug() << mSocket->write(ContentType); mSocket->waitForBytesWritten(); mSocket->close(); delete mSocket; }
in my java script I use:
var wSocket = new WebSocket('ws://192.168.1.5:1045'); wSocket.onopen = function (evt){ alert('Conectado...'); }; wSocket.onclose = function (evt){ alert('Cerrado....'); }; wSocket.onmessage = function (evt){ alert("Mensaje "+evt.value); }; wSocket.onerror = function (evt){ alert('Error: ' +evt.value); }; function enviar () { var txt = document.getElementById("txt").value; wSocket.send(txt); }
The New conecction is
thrown out. -
Use QWebSocket: https://doc.qt.io/qt-5/echoclient.html
-
@eyllanesc said in Problem with QTcpSocket and javascript client:
Use QWebSocket: https://doc.qt.io/qt-5/echoclient.
I guess the OP needs to use WebSockets on the server side. Please see this example.
-
Previous replies are not correct. OP is trying to use a QTcpServer with javascript client, so he's actually working on the server side. The correct Qt class for that is QWebSocketServer. The page also has examples, so should be straightforward.
-
@Mhamon said in Problem with QTcpSocket and javascript client:
As i said I put in .pro qwebsocket
It's
QT += websockets
as shown here https://doc.qt.io/qt-5/qwebsocket.html
-
@Taytoo said in Problem with QTcpSocket and javascript client:
Previous replies are not correct.
With due respect, I do consider my reply correct since I pointed the OP to the Websokects echo server example, which according to its description:
The Echo Server Example shows how to create a simple server application that sends back the messages it receives, using the WebSocket API.
and looking at its source code, you can see clearly that it uses a QWebSocketServer
EchoServer::EchoServer(quint16 port, bool debug, QObject *parent) : QObject(parent), m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"), QWebSocketServer::NonSecureMode, this)),
and the .pro clearly shows how to reference the Qt Websockets module:
QT = websockets TARGET = echoserver CONFIG += console ...
-
@Pablo-J-Rogina yes as I said in .pro added websockets BUT can't include in project, always show error QT= websockets file not foud. Here the problem id not the use it the example, I think its the configuration.
Here show:
"Project ERROR: Unknown module(s) in QT: websockets"