2015-07-01 24 views
0

到MQTT經紀人我正在錯誤無法連接使用javascript客戶

SCRIPT12152: WebSocket Error: Network Error 12152, The server returned an invalid or unrecognized response

在IE

,和

WebSocket connection to 'ws://192.168.1.100:1883/' failed: Connection closed before receiving a handshake response

在鉻

..下面是我已經使用

的代碼段
<!DOCTYPE html> 
<html lang="en"> 

<head></head> 

<body> 
<script src="../bower_components/jquery/dist/jquery.min.js"></script> 
<script src="../js/mqttws31.js"></script> 
<script src="../js/mqttws31-min.js"></script> 
<script src="../js/reconnecting-websocket.js"></script> 
<script src="../js/reconnecting-websocket.min.js"></script> 
<script> 

// Create a client instance 
client = new Paho.MQTT.Client("192.168.1.100", 1883, "100"); 
var s = new ReconnectingWebSocket("ws://192.168.1.100:1883"); 
// set callback handlers 
client.onConnectionLost = onConnectionLost; 
client.onMessageArrived = onMessageArrived; 

// connect the client 
client.connect({onSuccess:onConnect}); 

// called when the client connects 
function onConnect() { 
    alert("connected"); 
    // Once a connection has been made, make a subscription and send  
    console.log("onConnect"); 
    client.subscribe("/World"); 
    message = new Paho.MQTT.Message("Hello"); 
    message.destinationName = "/World"; 
    client.send(message); 
} 

// called when the client loses its connection 
function onConnectionLost(responseObject) { 
    if (responseObject.errorCode !== 0) { 
     console.log("onConnectionLost:"+responseObject.errorMessage); 
    } 
} 

// called when a message arrives 
function onMessageArrived(message) { 
    console.log("onMessageArrived:"+message.payloadString); 
} 
</script> 

</body> 

</html> 
+0

?它支持websockets嗎? – knolleary

+0

我嘗試使用Mosquitto經紀人,然後我跑後面的問題,然後我換成HiveMQ經紀人,與我只是啓用websocket,並得到它固定 但事情是我讀蚊子1.4以上支持websocket我做了相同的變化,蚊子經紀人以及,但仍然是下降 –

+0

您仍然連接到錯誤的端口。在另一個問題,我們爲蚊子設置的websocket端口是1884而不是1883 – hardillb

回答

2

如果您使用的是由蚊子提供的Windows的二進制文件,您應該知道它們不帶有libwebsockets sup端口啓用。如果你想在Windows上使用mosquitto支持websockets,你需要自己編譯libwebsockets,然後在啓用websockets支持後編譯mosquitto。

這也是值得注意的是,目前libwebsockets在Windows上支持不是很大,尤其是連接的客戶端的數量,你使用的是什麼經紀人不能超過64

+0

是否有任何其他替代方案爲我使用蚊子經紀人的JavaScript客戶端? –

+0

我不確定你在問什麼。蚊子經紀人不會充當javascript客戶端。您可以使用任何帶有mosquitto的JavaScript MQTT客戶端,但您必須在Windows上自己編譯才能獲得WebSockets支持。 – ralight

+0

在java中,我使用tcp for mqtt ..這裏用javascript應該只依賴於websocket或者是使用tcp連接的任何方式?對不起Iam新來的網絡東西像tcs/websocket等:( –

相關問題