坚持QtQML,坚持移动互联网

http://www.qtcn.org/bbs/u/121778  [收藏] [复制]

toby520

将QtCoding进行到底,做Qt的宠儿

  • 89

    关注

  • 164

    粉丝

  • 3579

    访客

  • 等级:精灵王
  • 身份:论坛版主
  • 总积分:1262
  • 男,1986-11-17

最后登录:2024-05-03

更多资料

日志

基于Qt框架的socket.io客户端与服务器nodejs通讯方案

2014-04-21 23:20

       Node.Js现在很火?是的,它现在属于后台服务器比较火的方案之一,废话不多说,想了解nodejs自己google去查查,基于javascript的后台服务器框架
       最近要做PC客户端的IM工作,而恰好后端使用的nodejs的,做即时通讯等功能,对于nodejs来说应该是绰绰有余,单线程下的高并发,适合创业公司学习。
       今天上午本来打算使用Qt下面的QTcpSocket来与后端socket.io建立链接,不曾想,简单的socket链接 是无法满足于现状,于是乎,网上找,群内问,终于找到了基于Qt下面的websocket的例子,可惜我现在Qt版本是5.2.1,但是Qt5.3已经将websocket模块集成到啦Qt框架里面啦,真是蛋疼的不得不用最新的,本来想使用QWebsocket源码进行编译,然后与QSocketIO源码进行编译成可用的动态库,但是不曾想,里面的坑太多,只得安装新的Qt5.3Beta包,才得以用之。
       现在主要说下QSocketIO的开源代码,代码地址:QSocketIO,老外写的开源的东东,分享精神不错,还是有QWebsocket这个本来是他开源的,后来直接被Qt收录囊中,直接集成到模块,Qt真是个大杂烩,集开源代码于一身。
       要使用socket.io QSocketIO已经有例子告诉你如何写。
       简单说下Qt部分
       #include "echoclient.h"#include <QtSocketIo/QSocketIOClient>#include <QtCore/QJsonObject>#include <QtCore/QJsonArray>#include <QtCore/QDebug>#define function(args...) [=](args)EchoClient::EchoClient(QObject *parent) :QObject(parent),m_client(){QObject::connect(&m_client, SIGNAL(connected(QString)), this, SLOT(connected(QString)));QObject::connect(&m_client, SIGNAL(disconnected(QString)), this, SLOT(disconnected(QString)));QObject::connect(&m_client, SIGNAL(errorReceived(QString,QString)), this, SLOT(errorReceived(QString,QString)));QObject::connect(&m_client, SIGNAL(heartbeatReceived()), this, SLOT(heartbeatReceived()));QObject::connect(&m_client, SIGNAL(messageReceived(QString)), this, SLOT(messageReceived(QString)));}EchoClient::~EchoClient(){}void EchoClient::open(QUrl url){m_client.open(url);}void EchoClient::messageReceived(QString message){qDebug() << "Message Received:" << message;}void EchoClient::errorReceived(QString reason, QString advice){qDebug() << "Error received:" << reason << "(advice" << advice << ")";}void EchoClient::ackReceived(int messageId, QJsonArray arguments){qDebug() << "Ack received for message with id" << messageId << "arguments:" << arguments;}void EchoClient::connected(QString endpoint){qDebug() << "Connected to endpoint" << endpoint;m_client.emitMessage("event with 2 arguments",QVariantList() << 1 << QStringLiteral("Hello socket.io,I am Qter"),function(QJsonArray returnValue) {qDebug() << "Got reply from event with 2 arguments:" << returnValue;});m_client.emitMessage("event with a json object",QVariantMap({ {"number", 1}, { QStringLiteral("message"), QStringLiteral("Hello socket.io")}}),function(QJsonArray returnValue) {qDebug() << "Got reply from event with a json object:" << returnValue;});m_client.on("event from server", function(QJsonArray data) {qDebug() << "Got event from server with data" << data;});}void EchoClient::disconnected(QString endpoint){qDebug() << "Disconnected from endpoint" << endpoint;}void EchoClient::heartbeatReceived(){qDebug() << "Received heartbeat";m_client.emitMessage("event with a json object",QVariantMap({ {"number", 1}, { QStringLiteral("message"), QStringLiteral("Hello socket.io")}}),function(QJsonArray returnValue) {qDebug() << "Got reply from event with a json object:" << returnValue;});}创建了一个wssocket连接,connect到服务器,同时也向服务器发送了一些数据,connect(QString str);里面还使用了回调函数,可以拿到服务器给出的应答

再者我们看下nodejs部分
首先需要安装nodejs模块
mac osx下面可以使用brew这个包管理工具 类似linux下面的apt工具 windows需要安装nodejs安装包
mac 下面 可以直接brew install nodejs,直接可以给你解决各种依赖关系
但是nodejs下面需要使用npm install socket.io 来安装socket.io模块
var io = require('socket.io').listen(8001, {"log": false,"close timeout": 30});io.sockets.on('connection', function(socket) {socket.emit('news', 1, 'world', function(data) {console.log("Message was delivered.");});socket.on('event with 2 arguments', function(data1, data2,callback) {console.log('Received event with arguments:' + data1 + " and " + data2);//setTwoarg();function setTwoarg(){if (callback) {callback("Okay received your event with 2 arguments");}}setTimeout(setTwoarg,2000);});socket.on('event with a json object', function(object, callback) {console.log('Received event with object:' + JSON.stringify(object));if (callback) {callback("Okay received your json object.");}});socket.emit("event from server", {"attribute1" : "hello", "attribute2": "world!"});});这里是nodejs代码

代码就创建了一个socket.io的服务,socket.on是监听客户端的链接,而emit是发送数据出去
socket.io接口如何使用可以参考socket.io网站
Qt部分写好后,在终端运行node server.js 然后客户端启动就可以链接服务器,做各种操作啦。
各位可以畅游服务器与客户端之间
晚安

分类:默认分类|回复:1|浏览:2727|全站可见|转载
 
 
删除

自强不吸

2014-05-30 09:45 -

Powered by phpwind v8.7 Certificate Copyright Time now is:05-04 17:48
©2005-2016 QTCN开发网 版权所有 Gzip disabled