QT------WebSocket

前言:关于websocket,前几天尝试着在qt里面用原生QTcpServer和QTcpSocket进行解析。最终是连接成功了,但是数据发送全乱码了,至今不知道这回事儿。

简单说下就是建立它们的握手:我们用html做客户端,qt做服务端

该部分摘自网友的,有兴趣的同学可以参考这里点击打开链接

handshake(握手)
client请求:
     GET /chat HTTP/1.1
        Host: server.example.com
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
        Sec-WebSocket-Version: 13
server回复:
     HTTP/1.1 101 Switching Protocols
        Upgrade: websocket
        Connection: Upgrade
        Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=

"dGhlIHNhbXBsZSBub25jZQ=="(Sec-WebSocket-Key)+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
先SHA-1哈希,再用base64编码,得到"s3pPLMBiTxaQ9kYGzzhZRbK+xOo="
其中"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"为固定字符串,至于为什么就没有深究了。上面的哈希、bas64在qt里面都很好实现:
QString clent_str="dGhlIHNhbXBsZSBub25jZQ==";
QString sever_str=QCryptographicHash::hash(clent_str.toLocal8Bit(),
                                    QCryptographicHash::Sha1).toBase64();

接下来说说QT自带的QWebSocketServer、QWebSocket,这里有QT已经封装过的,会自动握手,减少了我们的工作量。我们在头文件里面声明:

public:
    QWebSocketServer* websocketsever2=NULL;
    QWebSocket* websocket2=NULL;
public slots:
   void new_sockeconnection2();//新连接到来
   void read_websocket_massage2(QString message);//接收websocket消息

在cpp面定义

//构造函数里面new出sever,并监听
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
  // websocketsever=new QTcpServer(this);
  // websocketsever->listen(QHostAddress::Any,9547);
  // connect(websocketsever,SIGNAL(newConnection()),this,SLOT(new_sockeconnection()));
   websocketsever2=new QWebSocketServer(QStringLiteral("Echo Server"),
                                       QWebSocketServer::NonSecureMode, this);
   websocketsever2->listen(QHostAddress::Any,9547);//监听端口9547
   connect(websocketsever2,SIGNAL(newConnection()),this,SLOT(new_sockeconnection2()));//当有新连接到来,执行new_sockeconnection2()函数
}
//新连接到来
void MainWindow::new_sockeconnection2()
{
    if(websocketsever2!=NULL)
    {
      ui->textEdit->append(QString::fromLocal8Bit("新的连接!"));
      websocket2=websocketsever2->nextPendingConnection();
    }
    if(websocket2!=NULL)
    {
        connect(websocket2,SIGNAL(textMessageReceived(QString)),this, SLOT(read_websocket_massage2(QString)));//当有消息到来,执行read_websocket_massage2(QString)函数接收消息
    }
}
//接收到消息
void MainWindow::read_websocket_massage2(QString message)
{
    QWebSocket *pClient = qobject_cast(sender());
    ui->textEdit->append(message);
    qDebug() << "Message received:" << message;
    if (pClient)
    {
       // pClient->sendTextMessage(message);
    }
}
//发送消息到网页
void MainWindow::send_websocket_massage2()
{
    QString adasd="sadadadada";
    adasd=ui->textEdit_2->toPlainText();
    websocket2->sendTextMessage(adasd);
}

HTML文件:


    
        WebSocket Echo Client
    
    
    
        

WebSocket Echo Client

     

                                   

     

               

     

                         

       

QT------WebSocket_第1张图片

你可能感兴趣的:(QT)