Qt websocket
1.pro 添加
QT += websockets
#ifndef MYWEBSOCKETSERVER_H
#define MYWEBSOCKETSERVER_H #include <QObject>
#include <QtWebSockets/QWebSocket>
#include <QtWebSockets/QWebSocketServer>
#include <QList>
#include <QByteArray> class MyWebSocketServer : public QObject
{
Q_OBJECT
public:
explicit MyWebSocketServer(quint16 port, bool debug = false, QObject *parent = nullptr);
~MyWebSocketServer();
signals:
void closed();
public slots:
void onNewConnection();
void processTextMessage(QString message);
void processBinaryMessage(QByteArray message);
void socketDisconnected();
private:
QWebSocketServer *m_pWebSocketServer;
QList<QWebSocket *> m_clients;
bool m_debug;
}; #endif // MYWEBSOCKETSERVER_H
#include "mywebsocketserver.h"
//! [constructor]
MyWebSocketServer::MyWebSocketServer(quint16 port, bool debug, QObject *parent) :
QObject(parent),
m_pWebSocketServer(new QWebSocketServer(QStringLiteral("Echo Server"),
QWebSocketServer::NonSecureMode, this)),
m_debug(debug)
{
if (m_pWebSocketServer->listen(QHostAddress::Any, port)) {
if (m_debug)
qDebug() << "websocket server listening on port" << port;
connect(m_pWebSocketServer, &QWebSocketServer::newConnection,
this, &MyWebSocketServer::onNewConnection);
connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &MyWebSocketServer::closed);
}
}
//! [constructor] MyWebSocketServer::~MyWebSocketServer()
{
m_pWebSocketServer->close();
qDeleteAll(m_clients.begin(), m_clients.end());
} //! [onNewConnection]
void MyWebSocketServer::onNewConnection()
{ QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
qDebug() << "onNewConnection:"<<pSocket->peerAddress(); connect(pSocket, &QWebSocket::textMessageReceived, this, &MyWebSocketServer::processTextMessage);
connect(pSocket, &QWebSocket::binaryMessageReceived, this, &MyWebSocketServer::processBinaryMessage);
connect(pSocket, &QWebSocket::disconnected, this, &MyWebSocketServer::socketDisconnected); m_clients << pSocket;
}
//! [onNewConnection] //! [processTextMessage]
void MyWebSocketServer::processTextMessage(QString message)
{
// QWebSocket *pClient = qobject_cast<QWebSocket *>(sender()); // qDebug() <<pClient;
// if (m_debug)
// qDebug() << "Message received:" << message;
// if (pClient) {
// qDebug() << "sendTextMessage:" << message;
// pClient->sendTextMessage(message);
// } if(m_clients.count ()>){
qDebug() << "sendTextMessage:" << message;
m_clients.at(m_clients.count()-)->sendTextMessage (message);
} }
//! [processTextMessage] //! [processBinaryMessage]
void MyWebSocketServer::processBinaryMessage(QByteArray message)
{
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
if (m_debug)
qDebug() << "Binary Message received:" << message;
if (pClient) {
pClient->sendBinaryMessage(message);
}
}
//! [processBinaryMessage] //! [socketDisconnected]
void MyWebSocketServer::socketDisconnected()
{
qDebug() << "socketDisconnected:";
QWebSocket *pClient = qobject_cast<QWebSocket *>(sender());
if (m_debug)
qDebug() << "socketDisconnected:" << pClient;
if (pClient) {
m_clients.removeAll(pClient);
pClient->deleteLater();
}
}
//! [socketDisconnected]
Qt websocket的更多相关文章
- Qt websocket协议的实现
handshake(握手) client请求: GET /chat HTTP/1.1 Host: server.example.com Upgrade: ...
- QT使用websocket进行长连接
一般我们用的最多的就是http请求,但是频繁的请求可能对服务造成的压力很大,所以今天谈谈websocket长连接,一句话:简单 1.什么是长连接? A:一次请求连接,终身使用,就可以长久的保持信息的交 ...
- QT实现HTTP JSON高效多线程处理服务器
QT实现HTTP JSON高效多线程处理服务器 Legahero QQ:1395449850 现在一个平台级的系统光靠web打天下是不太现实的了,至少包含APP和web两部分,在早期APP直接访问we ...
- 两个基于C++/Qt的开源WEB框架
1.tufao 项目地址: https://github.com/vinipsmaker/tufao 主页: http://vinipsmaker.github.io/tufao/ 介绍: Tufão ...
- WebSocket学习总结
一 .websocket 已解决 但是websocket延伸出来的网络编程还有好多知识点没有清理.主要的流程和实现方式已经大概了解清楚,下面从学习的进度思路来一点点复习. 网络 ...
- QT:用QWebSocket实现webchannel,实现C++与HTML通信
基本原理是通过channel将C++对象暴露给HTML,在HTML中调用qwebchannel.js.前提是建立transport,QT只提供了一个抽象基类QWebChannelAbstractTra ...
- 使用nginx作为websocket的proxy server
blog.csdn.net/zhx6044/article/details/50278765 WebSocket WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选 ...
- Qt写websocketpp服务端
1.下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7. 2.下载boost,地址为https://www.boost.org ...
- QT各模块
基本模块: QT core QT gui QT widgets QT multimedia QT webkit 浏览器引擎 QT network QT sql QT test 单元测试 QT webv ...
随机推荐
- hdu 3037——Saving Beans
Saving Beans Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 清北学堂part1
睡眠质量相当高的一天(滑稽) 整一整都学了啥 1:高精度(相当水,毕竟学过) 2:模运算(?! 这还要讲?) 3:快速幂(还要谢一位学习高数时间为我们讲解的同学...不得不说真的有效,快速幂已经是随手 ...
- [模板] dp套dp && bzoj5336: [TJOI2018]party
Description Problem 5336. -- [TJOI2018]party Solution 神奇的dp套dp... 考虑lcs的转移方程: \[ lcs[i][j]=\begin{ca ...
- 【数学建模】day14-建立GM(1,1)预测评估模型应用
学习建立GM(1,1)灰色预测评估模型,解决实际问题: SARS疫情对某些经济指标的影响问题 一.问题的提出 2003 年的 SARS 疫情对中国部分行业的经济发展产生了一定影响,特别是对部分 疫情较 ...
- 数据分析---《Python for Data Analysis》学习笔记【01】
<Python for Data Analysis>一书由Wes Mckinney所著,中文译名是<利用Python进行数据分析>.这里记录一下学习过程,其中有些方法和书中不同 ...
- tomcat在windows及linux环境下安装
下载tomcat 下载地址: https://tomcat.apache.org/download-90.cgi 7,8,9的版本都可以下,这里下载最新版本 注意:Binary是编译好的,可以直接使用 ...
- 柳叶刀重磅出击!全外显子测序在胎儿结构异常的评估Whole-exome sequencing in the evaluation of fetal structural anomalies: a prospective cohort study
柳叶刀发表的文献解读:Whole-exome sequencing in the evaluation of fetal structural anomalies: a prospective coh ...
- c语言中堆栈和静态空间
什么是堆空间.栈空间与静态空间 堆空间:由程序员自己分配空间,如malloc需要指定分配多少个多大的字节空间,不用的时候需要自己释放 栈空间:栈空间是由系统自动分配与释放,如int,char等大小都已 ...
- 20165232 实现pwd
20165232 实现mypwd 题目要求 学习pwd命令 研究pwd实现需要的系统调用(man -k; grep),写出伪代码 实现mypwd 测试mypwd 学习pwd命令 用man pwd 查看 ...
- 当使用makemigrations时报错No changes detected
在修改了models.py后,有些用户会喜欢用python manage.py makemigrations生成对应的py代码. 但有时执行python manage.py makemigration ...