1

I created a simple chat application using QML's WebSockets component. It's just the client:

Window {
    id: root
    visible: true
    width: 1024
    height: 768

    property variant messages: []

    WebSocket {
        id: sock
        url: "http://localhost:3700"
        onTextMessageReceived: {
            var data = message;
            var messages = root.messages;
            if(data.message) {
                messages.push(data);
                var html = '';
                for(var i = 0; i < messages.length; i++) {
                    html += '<b>' + (messages[i].username ? messages[i].username : 'Server') + ': </b>';
                    html += messages[i].message + '<br />';
                }
                messageBox.append(html);
            } else {
                messageBox.append("There is a problem:", data);
            }
        }
        onStatusChanged: {
            if (sock.status == WebSocket.Error) {
                messageBox.append("Error: " + sock.errorString);
            }
            else if (sock.status == WebSocket.Open) {
                messageBox.append("Socket open");
            }
            else if (sock.status == WebSocket.Closed) {
                messageBox.append("Socket closed");
            }
        }
        active: false
    }

The server is implemented on Node.js and Socket.io using this article. The problem is, when I try to connect to the server the app throws this:

Error: Unsupported WebSocket scheme: http

If I change the protocol to ws, then the server closes the connection. What can I do?

The server code:

var express = require("express");
var app = express();
var port = 3700;

app.set('views', __dirname + '/tpl');
app.set('view engine', "jade");
app.engine('jade', require('jade').__express);
app.get("/", function(req, res){
    res.render("page");
});

app.use(express.static(__dirname + '/public'));

var io = require('socket.io').listen(app.listen(port));

io.sockets.on('connection', function (socket) {
    socket.emit('message', { message: 'welcome to the chat' });
    socket.on('send', function (data) {
        io.sockets.emit('message', data);
    });
});

console.log("Listening on port " + port);
3
  • Socket.io !== websockets. Socket.io first connects using xhr and then upgrades to websockets if they are supported. Commented Nov 6, 2014 at 11:02
  • Socket.io is on the server, there is no xhr there. Do you mean I need to create a server without Socket.io? Commented Nov 6, 2014 at 11:05
  • The initial connection to socket.io expects xhr... Socket.IO never assumes that WebSocket will just work, because in practice there’s a good chance that it won’t. Instead, it establishes a connection with XHR or JSONP right away, and then attempts to upgrade the connection to WebSocket.. Commented Nov 6, 2014 at 11:06

2 Answers 2

1

From Qt documentation: The url must have one of 2 schemes: ws:// or wss://. When not supplied, then ws:// is used. So you have to specify the url as

url: "ws://localhost:3700"

or wss:// if you use secured connection.

Also take into account that QML WebSocket supports only version 13 of the WebSocket protocol. See RFC documentation for more info.

Sign up to request clarification or add additional context in comments.

Comments

1

I found a much simplier solution for this problem.

It isn't as easy as setting url to "ws://localhost:3000" when using websockets with socket.io You also have to set the transport-type when talking to socket.io, for example:

url: "ws://127.0.0.1:3000/socket.io/?EIO=3&transport=websocket"

This can be used with the QML WebSockets example provided by qt here http://doc.qt.io/qt-5/qtwebsockets-qmlwebsocketclient-qml-qmlwebsocketclient-main-qml.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.