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);
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..