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. iOS开发基础篇-Button基础

    一.简单介绍  UIButton 的功能:响应用户操作.显示文字.显示图片.调整内部图片和文字的位置. 二. UIButton 的状态  UIControlStateNormal :普通状态,为默认情 ...

  2. 微信小程序中用户登录和登录态维护

    提供用户登录以及维护用户的登录状态,是一个拥有用户系统的软件应用普遍需要做的事情.像微信这样的一个社交平台,如果做一个小程序应用,我们可能很少会去做一个完全脱离和舍弃连接用户信息的纯工具软件. 让用户 ...

  3. codeforces#1136 C. Nastya Is Transposing Matrices(找规律)

    题意:给出两个n*m的矩阵,每次操作可以让一个正方形矩阵行列交换.问,在无限次操作下,第一个矩阵能否变成第二个矩阵 分析:先把操作限定在2*2的矩阵中.这样对角线上的元素就可以随意交换.也就是说,如果 ...

  4. iview table行render渲染不同的组件

    table不同的行,相同的列渲染不同的组件,如图1:第一行渲染selece,第二行渲染input render:(h,params)=>{ if(params.index === 0){ //以 ...

  5. Qt中的QWebView

    一.Webkit了解   Webkit是一个开源的浏览器引擎,chrome也使用了作为核心.Qt中对Webkit做了封装,主要有以下几个类: QWebView :最常用的类,作为一个窗体控件 QWeb ...

  6. Java Mail 实现第三方邮件发送功能

    1 创建一个用于发送邮件的类 package com.latiny.service; import java.io.IOException; import java.io.InputStream; i ...

  7. webpack+vue 组件间传参(单一事件中心管理组件通信--$root),如果有路由的话会失效

    先给一个例子: <body> <div id="box"> <com-a></com-a> <com-b></co ...

  8. springboot2.0整合shiro出现ShiroDialect报错 找不到org/thymeleaf/processor/attr/AbstractTextChildModifierAttrPr

    包版本过低,找最新包 https://mvnrepository.com/ <dependency> <groupId>com.github.theborakompanioni ...

  9. 微软发布Xamarin Live Player:Win10可开发iOS

    微软在Build2017大会上发布一款名为Xamarin Live Player的全新工具,可以让PC用户在不使用Mac的前提下测试和调试iOS应用.但是向App Store提交应用时仍然需要Xcod ...

  10. [模板] 二分图博弈 && BZOJ2463:[中山市选2009]谁能赢呢?

    二分图博弈 from BZOJ 1443 游戏(二分图博弈) - free-loop - 博客园 定义 1.博弈者人数为两人,双方轮流进行决策. 2.博弈状态(对应点)可分为两类(状态空间可分为两个集 ...