Blazor WebAssembly基于websocket的MQTT不工作



我正试图使用Paho在Blazor中实现一个基于websocket客户端订阅者的mqtt。问题是它坚持使用ws而不是ws,并在连接时抛出ERR_SSL_PROTOCOL_ERROR错误。

这里有一个简化的代码块:

var mqtt;
var host = "api.mydomainexample.com";
var port = 1884;
function onConnect(){
console.log("connected ! Now listening for messages ..");
mqtt.subscribe("someTopic");
}
function onFailure(message){
console.log("connection to host failed: " + message);
}
function onMessageArrived(msg){
var message = "Message received on topic '"+ msg.destinationName +"': "+ msg.payloadString;
console.log(message);
}
function mqttConnect() {
console.log("connecting to " + host + " ..");
mqtt = new Paho.MQTT.Client(host, port, clientid);
var options = {
timeout: 3,
onSuccess: onConnect,
onFailure: onFailure,
useSSL: false
};
mqtt.onMessageArrived = onMessageArrived;
mqtt.connect(options);
}

我将这些代码复制到记事本中创建的html页面中,从html主体调用该函数,并在浏览器中运行该文件。它运行良好,订阅良好。此外,我在连接选项中添加了useSSL: false,尽管我以前没有,但仍然无法工作。

这是我从控制台收到的错误:

WebSocket connection to 'wss://api.mydomainexample:1884/mqtt' failed: Error in connection establishment: net::ERR_SSL_PROTOCOL_ERROR

我还更改了我的项目启动设置,使其以http而不是https启动,因为根据这个答案,我无法使用通过https加载的页面中的ws。

有什么想法吗?我不能直接连接到blazor中没有证书的websocket吗?

好的,事实证明,在创建blazor应用程序时,有一个到'configure on https'的选项,该选项会导致请求从http重定向到https,从而请求安全的ws而不是ws。

希望这能帮助到别人!

最新更新