收藏
回答

WebSocket net::ERR_CONNECTION_TIMED_OUT

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 需求 WebSocket 工具 6.6.7 2.2.2

appservice?t=1533630357896:1032 WebSocket connection to 'wss://36.41.188.53/mqtt' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

我在微信小程序中是用mqtt协议,通过nginx代理监听443端口,在本地测试通过,但是在连接服务器是报上述异常,可以ping通ip,telnet也可以连接服务器也正常,MQ使用的是emqttd,恳请各位大佬指点。

// pages/connect/connect.js

var app = getApp();

var MQTT = require("../../utils/paho-mqtt.js");


Page({

/**

    * 页面的初始数据

    */

data: {

server_addr: '36.41.188.53',

user_name: "test",

user_psw: ['1','2','3','4','5','6','7','8','9'],

error_message: '',

switch_checked: false,

btn_loading: false

},


server_addr_input: function(e) {

//console.log(e);

this.setData({

server_addr: e.detail.value

});

},

user_name_input: function(e) {

//console.log(e);

this.setData({

user_name: e.detail.value

});

},

user_psw_input: function(e) {

//console.log(e);

this.setData({

user_psw: e.detail.value

});

},

switch_change: function(e) {

//console.log(e);

this.setData({

switch_checked: e.detail.value

});

},


btn_connect: function() {


//查看输入是否为空,设置错误信息

if (this.data.server_addr == '' || this.data.server_addr == null) {

this.setData({

error_message: "server address can not be empty!"

});

return;

}

if (this.data.switch_checked) {

if (this.data.user_name == null || this.data.user_name == '') {

this.setData({

error_message: "user name can not be empty!"

});

return;

}

if (this.data.user_psw == null || this.data.user_psw == '') {

this.setData({

error_message: "user password can not be empty!"

});

return;

}

}


//reset error message

if (this.data.error_message) {

this.setData({

error_message: ''

});

}

//在按钮上显示加载标志

this.setData({

btn_loading: true

});


var client = new MQTT.Client("ws://" + this.data.server_addr + "/mqtt", "mqttjs" + Math.random().toString(36).substr(2));

var that = this;

//connect to  MQTT broker

var connectOptions = {

timeout: 10,

useSSL: true,

cleanSession: true,

keepAliveInterval: 30,

reconnect: true,

userName: "test",

password: "123456789",


onSuccess: function() {

console.log('connected');


app.globalData.mqtt_client = client;


client.onMessageArrived = function(msg) {

if (app.globalData.messages != null) {

app.globalData.messages = [{

topic: msg.topic,

humidity: msg.payloadString.toString().substr(6, 2),

temperature: msg.payloadString.toString().substr(15, 2),

doorLock: msg.payloadString.toString().substr(24, 1),

socket: msg.payloadString.toString().substr(32, 1),

}].concat(app.globalData.messages);

}

}


client.onConnectionLost = function(responseObject) {

if (typeof app.globalData.onConnectionLost === 'function') {

return app.globalData.onConnectionLost(responseObject);

}

if (responseObject.errorCode !== 0) {

console.log("onConnectionLost:" + responseObject.errorMessage);

}

}

//去除按钮上的加载标志

that.setData({

btn_loading: false

});


wx.switchTab({

url: '../subscribe/subscribe',

});

},

onFailure: function(option) {

console.log(option);

//去除按钮上的加载标志

that.setData({

btn_loading: false

});

wx.showModal({

//title: msg.destinationName,

content: option.errorMessage

});

}

};

if (this.data.switch_checked) {

connectOptions.userName = this.data.user_name;

connectOptions.password = this.data.user_psw;

}


client.connect( );


},


/**

    * 生命周期函数--监听页面加载

    */

onLoad: function(options) {


},


/**

    * 生命周期函数--监听页面初次渲染完成

    */

onReady: function() {


},


/**

    * 生命周期函数--监听页面显示

    */

onShow: function() {

if (app.globalData.mqtt_client != null) {

wx.reLaunch({

url: '../subscribe/subscribe',

});

return;

}

}

})


回答关注问题邀请回答
收藏

2 个回答

  • 焦志杰(老焦)
    焦志杰(老焦)
    2018-11-09

    腾讯没有人了吗?这么多的问题都没有解答

    2018-11-09
    赞同
    回复
  • ༺罗程༻
    ༺罗程༻
    2018-09-03

    楼主,请问问题解决了吗,我也是同样的问题。

    我也是用的emqtt,nginx代理。不过小程序端我用的是mpvue,mqtt也是paho.mqtt,出现同样的问题。

    2018-09-03
    赞同
    回复
登录 后发表内容