WebSocket握手错误净:: ERR_CONNECTION_RESET

问题描述:

我想连接Paho JS库捕捉!以允许通过MQTT协议与服务器进行通信,但不知何故,WebSockets不断崩溃。WebSocket握手错误净:: ERR_CONNECTION_RESET

这是我使用的代码:

var wsbroker = "127.0.0.1"; 
var wsport = 9001; 

console.log("Connecting to: ", wsbroker); 
console.log("Connecting to port: ", Number(wsport)); 

client = new Paho.MQTT.Client(wsbroker, Number(wsport),"Snap"); 

// set callback handlers 
client.onConnectionLost = onConnectionLost; 
client.onMessageArrived = onMessageArrived; 

// connect the client 
var client_options = { 
    onSuccess:onConnect, 
    onFailure:doFail 
} 

client.connect(client_options); 

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

function doFail(e){ 
    console.log("I haz failed"); 
    console.log(e); 
} 

// 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); 
} 

mqttws31.min.js之前(使用mqttws31.js但相同的结果)来实现。

我有以下的配置安装mosquitto 1.4.8:

# Place your local configuration in /etc/mosquitto/conf.d/ 
    # 
    # A full description of the configuration file is at 
    # /usr/share/doc/mosquitto/examples/mosquitto.conf.example 

    pid_file /var/run/mosquitto.pid 

    listener 1883 127.0.0.1 protocol mqtt 
    listener 9001 127.0.0.1 protocol websockets 

    persistence true 
    persistence_location /var/lib/mosquitto/ 

    log_dest file /var/log/mosquitto/mosquitto.log 

    include_dir /etc/mosquitto/conf.d 

的JS代码的HTML文件运行在蟒蛇SimpleHTTPServer始终运行后打印此控制台:

WebSocket connection to 'ws://127.0.0.1:9001/mqtt' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET 

与此作为onFail函数上的错误代码:

Object {invocationContext: undefined, errorCode: 7, errorMessage: "AMQJS0007E Socket error:undefined."} 

在服务器端(mosqui TTO)我得到以下日志:

1496269205: mosquitto version 1.4.8 (build date Fri, 19 Feb 2016 12:03:16 +0100) starting 
1496269205: Config loaded from myconf.conf. 
1496269205: Opening ipv4 listen socket on port 1883. 
1496269205: Opening ipv4 listen socket on port 9001. 
1496269215: New connection from 127.0.0.1 on port 9001. 
1496269215: Socket error on client <unknown>, disconnecting. 
1496269215: New connection from 127.0.0.1 on port 9001. 
1496269215: Socket error on client <unknown>, disconnecting. 
1496269224: New connection from 127.0.0.1 on port 9001. 
1496269224: Socket error on client <unknown>, disconnecting. 
1496269224: New connection from 127.0.0.1 on port 9001. 
1496269224: Socket error on client <unknown>, disconnecting. 

所以我想那一定是错的地方在代码中的一部分,但我完全失去了这里。

定义应与配置文件中的listener分开一行。目前他们被忽略,所以这两个监听器都被解释为原生MQTT。

pid_file /var/run/mosquitto.pid 

listener 1883 127.0.0.1 
protocol mqtt 

listener 9001 127.0.0.1 
protocol websockets 

persistence true 
persistence_location /var/lib/mosquitto/ 

log_dest file /var/log/mosquitto/mosquitto.log 

include_dir /etc/mosquitto/conf.d 
+0

我有同样的错误,我的配置监听器和协议是相同的,你描述(没有127.0.0.1)。还有什么可能会出错?我只能在使用另一个蚊子实例时才能发布和订阅邮件 – TheUnreal

+0

@TheUnreal所有的新问题(引用这个需要的)并包括你自己的日志和配置文件,所以我们可以设置它是完全相同的问题 – hardillb