Jason,

Thanks for your reply.

I have used ws module directly without Primus and I’ve been able to establish 
communication between Qt and Node.

I still don’t know what is causing the problem with Primus, but for now I don’t 
care anymore! ;)


Thanks,

Best,

Nuno

> On 25 Jun 2018, at 14:26, Jason H <[email protected]> wrote:
> 
> Did you ever call liten on the server side? Literally server.listen(); I used 
> a different module, express-ws.
>  
> You need to have something listening on the port, then the server can map the 
> incomming request to the correct handler. I use the following (incomplete 
> code) to broadcast activity to all connected clients.
> "use strict";
>  
> //my modules
> const db = require('./database');
> const path = require('path');
> const config = require('./config');
>  
> // for multithreading
> const cluster = require('cluster');
>  
> if (cluster.isMaster) {
>   console.log(process.cwd())
>  
>   const con = db.connect();
>   db.init(con);
>   cluster.fork();
>  
> } else {
>  
>   const express = require('express');
>   const app = express();
>   const expressWs = require('express-ws')(app);
>   app.ewss = expressWs;
>  
>   const compression = require('compression');
>   app.use(compression());
>  
>   const bodyParser = require('body-parser');
>  
>   app.use(bodyParser.urlencoded({ extended: true }));
>   app.use(bodyParser.json());
>  
>   const multer  = require('multer');
>   const upload = multer({ dest: config.uploadDir });
>   app.use(upload.any());
>  
>   app.get('/', function (req, res) {
>     res.send('Hello World!');
>   });
>  
>  
> //I have my own module/endpoint registration API, this is that.
>   var modules = [];
>   for(var class_name of ['a', 'b', 'c']) {
>     var clazz = require('./'+class_name);
>     modules.push(new clazz(app));
>   }
>  
>   app.listen(config.port, function () {
>     console.log('Example app listening on port '+ config.port);
>   });
>  
> Meanwhile in my 'c' module:
> class c {
> ...
>       '/c/activity': {
>         'ws': this.activity // 'ws' is the method like GET, POST, etc.
>       }
> ...
>   async activity(ws ,resp) {
>     ws.on('open', () => {
>       console.log('ws client opened');
>     })
>     ws.on('close', () => {
>       console.log('ws client closed');
>     })
>  
>   }
>  
>   broadcast (wss, data) {
>     wss.clients.forEach(function each(client) {
>       if (client.readyState === WebSocket.OPEN) {
>         client.send(data);
>       }
>     });
>   }
>  
> Then, to use broadcast in a function in my c class
> this.broadcast(this.app.ewss.getWss(), JSON.stringify(req.body)); 
> Sent: Sunday, June 24, 2018 at 1:02 PM
> From: "Nuno Santos" <[email protected]>
> To: "Qt Project MailingList" <[email protected]>
> Subject: [Interest] QtWebsocket and Node.js
> Hi,
>  
> I’m trying to establish a connection between a QtWebsocket client and a 
> Node.js websockets server. I’m using the bare minimum. On the Node.js side 
> I’m. Using Primus to be able to easily switch to other web sockets 
> implementations. S far I have only tried with websockets implementation and 
> the source code resumes to the following lines:
>  
> 'use strict';
>  
> var Primus = require('primus');
> var http = require('http');
>  
> var server = http.createServer();
> var primus = new Primus(server, { transformer: 'websockets' });
>  
> primus.on('connection', function(socket) {
>   socket.on('data', function ping(message) {
>     console.log('received a new message', message);
>     socket.write({ ping: 'pong' });
>   });
> });
>  
> server.listen(8080);
>  
> And on the Qt side:
>  
> connect(&_webSocket, SIGNAL(connected()), this, SLOT(onConnected()));
> connect(&_webSocket, SIGNAL(disconnected()), this, SLOT(onDisconnected()));
> connect(&_webSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, 
> SLOT(onError(QAbstractSocket::SocketError)));
>  
> _webSocket.open(QUrl("ws://localhost:8080 <>"));
>  
> Where:
>  
> void Controller::onConnected()
> {
>     qDebug() << "on connected";
> }
>  
> void Controller::onDisconnected()
> {
>     qDebug() << "on disconnected";
> }
>  
> void Controller::onError(QAbstractSocket::SocketError error)
> {
>     qDebug() << "error" << error;
> }
>  
>  
> When I start the Qt app with the server already running nothing gets printed 
> out to console.
>  
> However… if I stop the server it displayed the following lines on the Qt app 
> side:
>  
> error QAbstractSocket::RemoteHostClosedError
> on disconnected
>  
> Has anyone stumbled on a similar problem before?
>  
> Thanks,
>  
> Regards,
>  
> Nuno
>  
> _______________________________________________ Interest mailing list 
> [email protected] http://lists.qt-project.org/mailman/listinfo/interest 
> <http://lists.qt-project.org/mailman/listinfo/interest>
_______________________________________________
Interest mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to