Qt笔记(三十五)之QWebSocket简单使用(1)-实现服务端

104 篇文章 125 订阅

一.前言

楼主在最近的工作中接触到socket方面的知识,而又是接触Qt的多,于是就找了下Qt这方面封装的类,以此来练手熟悉一下

二.关于QwbSocket

Implements a TCP socket that talks the WebSocket protocol.
实现一个与WebSocket协议对话的TCP套接字

WebSockets is a web technology providing full-duplex communications channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011. QWebSocket can both be used in a client application and server application.
WebSockets是一种通过单个TCP连接提供全双工通信通道的Web技术。WebSocket协议在2011年被IETF标准化为RFC 6455。QWebSocket既可用于客户端应用程序,也可用于服务器应用程序。

This class was modeled after QAbstractSocket.
这个类是继承了QAbstractSocket

QWebSocket currently does not support WebSocket Extensions and WebSocket Subprotocols.
QWebSocket当前不支持WebSocket扩展和WebSocket子工具。

QWebSocket only supports version 13 of the WebSocket protocol, as outlined in RFC 6455.
QWebSocket仅支持WebSocket协议的版本13,如RFC6455所述。

三.服务端的实现举例

1.pro文件添加websockets

QT       += core gui websockets

2.简单的布局界面
在这里插入图片描述
3.逻辑编写(主要用到了QWebSocket和QWebSocketServer),核心代码

#include "ServerWidget.h"
#include "ui_ServerWidget.h"

ServerWidget::ServerWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ServerWidget)
{
    ui->setupUi(this);
    setWindowTitle("服务端");

    webScoketServer= new QWebSocketServer("Server",QWebSocketServer::NonSecureMode);
    connect(webScoketServer, SIGNAL(newConnection()), this, SLOT(onNewConnection()));
}

ServerWidget::~ServerWidget()
{
    delete ui;
}

void ServerWidget::on_pushButton_1_clicked()
{
    if(ui->lineEdit->text().isEmpty())
        return;

    int port = ui->lineEdit->text().toInt();
    webScoketServer->listen(QHostAddress::Any,port);
    ui->textEdit_2->append(QDateTime::currentDateTime().toString()+":开始监听");
}

void ServerWidget::on_pushButton_2_clicked()
{
    ui->textEdit_2->append(QDateTime::currentDateTime().toString()+":停止监听");
    if(webScoketServer->isListening())
        webScoketServer->close();
}

void ServerWidget::on_pushButton_3_clicked()
{
    QString msg = ui->textEdit_1->toPlainText();
    webSocket->sendTextMessage(msg);
    ui->textEdit_2->append(QDateTime::currentDateTime().toString()+":服务端给客户端发送消息:"+msg);
}

void ServerWidget::onNewConnection()
{
    webSocket= webScoketServer->nextPendingConnection();
    connect(webSocket, SIGNAL(textMessageReceived(QString)), this, SLOT(onTextMessageReived(QString)));
    connect(webSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
    ui->textEdit_2->append(QDateTime::currentDateTime().toString()+":有新链接进来了:"+webSocket->localAddress().toString());
}

void ServerWidget::onTextMessageReived(QString msg)
{
    ui->textEdit_2->append(QDateTime::currentDateTime().toString()+":客户端发来消息:"+msg);
}

void ServerWidget::onDisconnected()
{
    ui->textEdit_2->append(QDateTime::currentDateTime().toString()+":客户端断开了连接");
}
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值