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的更多相关文章

  1. Qt websocket协议的实现

      handshake(握手) client请求:      GET /chat HTTP/1.1         Host: server.example.com         Upgrade: ...

  2. QT使用websocket进行长连接

    一般我们用的最多的就是http请求,但是频繁的请求可能对服务造成的压力很大,所以今天谈谈websocket长连接,一句话:简单 1.什么是长连接? A:一次请求连接,终身使用,就可以长久的保持信息的交 ...

  3. QT实现HTTP JSON高效多线程处理服务器

    QT实现HTTP JSON高效多线程处理服务器 Legahero QQ:1395449850 现在一个平台级的系统光靠web打天下是不太现实的了,至少包含APP和web两部分,在早期APP直接访问we ...

  4. 两个基于C++/Qt的开源WEB框架

    1.tufao 项目地址: https://github.com/vinipsmaker/tufao 主页: http://vinipsmaker.github.io/tufao/ 介绍: Tufão ...

  5. WebSocket学习总结

    一 .websocket 已解决      但是websocket延伸出来的网络编程还有好多知识点没有清理.主要的流程和实现方式已经大概了解清楚,下面从学习的进度思路来一点点复习.        网络 ...

  6. QT:用QWebSocket实现webchannel,实现C++与HTML通信

    基本原理是通过channel将C++对象暴露给HTML,在HTML中调用qwebchannel.js.前提是建立transport,QT只提供了一个抽象基类QWebChannelAbstractTra ...

  7. 使用nginx作为websocket的proxy server

    blog.csdn.net/zhx6044/article/details/50278765 WebSocket WebSocket协议为创建客户端和服务器端需要实时双向通讯的webapp提供了一个选 ...

  8. Qt写websocketpp服务端

    1.下载websocketpp,地址为https://github.com/zaphoyd/websocketpp,版本为0.7. 2.下载boost,地址为https://www.boost.org ...

  9. QT各模块

    基本模块: QT core QT gui QT widgets QT multimedia QT webkit 浏览器引擎 QT network QT sql QT test 单元测试 QT webv ...

随机推荐

  1. CF1120D Power Tree

    沙发~~ 题意简述 给你一棵有根树,定义叶子为度数为1的点. 你可以以$ w_x \(的代价控制\)x\(点.选择控制之后可以给它的子树里的叶子加 上\)t (t \in Z )$. 你要以最小的总代 ...

  2. React 合并行 RowSpan

    十年河东,十年河西,莫欺少年穷 学无止境,精益求精 今儿分享一篇关于React Table 组件合并单元行的方法! 实例效果如下: 原则就是遇到相同的供方名称,就要做行合并! 思路如下:后端计算合并的 ...

  3. 【学习总结】GirlsInAI ML-diary day-14-function函数

    [学习总结]GirlsInAI ML-diary 总 原博github链接-day14 认识函数function 函数相当于一个固定的公式,一个映射.有输入,有输出. 1-python内置函数 max ...

  4. springboot 静态注入 单例

    package com.b2q.web_push.util; import io.goeasy.GoEasy; import org.springframework.beans.factory.ann ...

  5. Python——Window启动服务

    一.新建启动服务程序 # ZPF # encoding=utf-8 import win32timezone from logging.handlers import TimedRotatingFil ...

  6. 4.6 并发编程/IO模型

    并发编程/IO模型 背景概念 IO模型概念 IO模型分类 阻塞IO  (blocking IO) 特点: 两个阶段(等待数据和拷贝数据两个阶段)都被block 设置 server.setsockopt ...

  7. Django 路由系统

    Django 路由系统 基本格式 from django.conf.urls import url urlpatterns = [ url(正则表达式, views视图函数,参数,别名), ] 参数说 ...

  8. 20165223 《信息安全系统设计基础》 实现mypwd

    一.学习pwd命令 1. pwd命令简介 英文原名:Print Working Directory 指令功能:打印出当前工作目录 执行权限:All User 指令所在路径:/usr/bin/pwd 或 ...

  9. tf.reducemean()到底是什么意思?

    https://blog.csdn.net/he_min/article/details/78694383 在tensorflow中经常见到reducemean这个api,到底有什么用,到底是对谁求均 ...

  10. Window下Eclipse+Tomcat远程调试

    需求:       项目在开发环境跑得好好的,但是当发布到服务器上时,却出现了一些意外的问题.服务器上不可能给你装IDE调试工具啊,又没有很好的日志帮助下,这时候就用到了JVM的Java Platfo ...