Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web socket error: Connection closed before receiving a handshake response #29

Open
lubeHub opened this issue Nov 3, 2021 · 2 comments

Comments

@lubeHub
Copy link

lubeHub commented Nov 3, 2021

When I run on 127.0.0.1:8000 your test example it works well. But when I am trying to connect to ThingSpeak it give me error:
WebSocket connection to 'ws://mqtt3.thingspeak.com:1883/' failed: Connection closed before receiving a handshake response

Here is ThingSpeaks documentation how to connect: https://www.mathworks.com/help/thingspeak/subscribetoachannelfeed.html
Here is my info:
connection: { host: 'mqtt3.thingspeak.com', port: 1883, endpoint: '/mqtt', clean: true, // Reserved session connectTimeout: 4000, // Time out reconnectPeriod: 4000, // Reconnection interval // Certification Information clientId: 'myId', username: 'myUsername', password: 'myPassword', },

Here is my Connect code:
createConnection() { // 连接字符串, 通过协议指定使用的连接方式 // ws 未加密 WebSocket 连接 // wss 加密 WebSocket 连接 // mqtt 未加密 TCP 连接 // mqtts 加密 TCP 连接 // wxs 微信小程序连接 // alis 支付宝小程序连接 const { host, port, endpoint, ...options } = this.connection const connectUrl =mqtt://${host}:${port}${endpoint} try { this.client = mqtt.connect(connectUrl, options) } catch (error) { console.log('mqtt.connect error', error) } this.client.on('connect', () => { console.log('Connection succeeded!') }) this.client.on('error', error => { console.log('Connection failed', error) }) this.client.on('message', (topic, message) => { this.receiveNews = this.receiveNews.concat(message) console.log(Received message ${message} from topic ${topic}) }) },

I tried changing in connectUrl mqtt and ws and mqtts and ws but does not work. I also tried removing endpointand addingtopicasendpoint` because there is no endpoint in ThingsSpeak documentation but still nothing.

@lubeHub
Copy link
Author

lubeHub commented Nov 3, 2021

I mannaged to connect using websocket and port: 80

@zibuyu1995
Copy link
Member

If you need to connect to a Websocket, then your address should be ws://mqtt3.thingspeak.com:80

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants