Coder Social home page Coder Social logo

Comments (7)

Mixser avatar Mixser commented on July 23, 2024 4

Hi, @fangchih. At this moment ws transport is experimital feature in gmqtt. You may help us to debug and test it by using gmqtt from support-ws-transport branch.

As I say, this is experimital feature and in future we can to change methods and ways to use it;

Here is a small exmaple how to init client with ws as transport:

mqtt = Client("test")
mqtt.set_auth_credentials('TOKEN')
# init your handlers on_connect, on_message and etc

await mqtt.connect('ws://mqtt.flespi.io', keepalive=60)
mqtt.subscribe('#')

data_to_send = '0xDEADBEAF' * (512 * 102)

mqtt.publish('topic/test', data_to_send)

from gmqtt.

honglei avatar honglei commented on July 23, 2024 4

Any news about this branch? When will it been merged to master?

from gmqtt.

Lenka42 avatar Lenka42 commented on July 23, 2024 3

@jeffreykira This happens because you didn't set valid credentials for the client, uncomment this line:

# mqtt.set_auth_credentials(token, None)

from gmqtt.

jeffreykira avatar jeffreykira commented on July 23, 2024 2

@Lenka42 Thanks for your replay. It works for me.

from gmqtt.

jeffreykira avatar jeffreykira commented on July 23, 2024

Hi, @Mixser. I get the wrong result using the above example.

Traceback (most recent call last):
  File "gmqtt_websocket.py", line 60, in <module>
    loop.run_until_complete(main(host, token))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "gmqtt_websocket.py", line 43, in main
    await mqtt.connect(broker_host, keepalive=60)
  File "/Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/client.py", line 143, in connect
    self._connection = await self._create_connection(
  File "/Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/client.py", line 160, in _create_connection
    connection = await MQTTConnection.create_connection(host, port, ssl, clean_session, keepalive)
  File "/Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/mqtt/connection.py", line 26, in create_connection
    transport, protocol = await loop.create_connection(MQTTProtocol, host, port, ssl=ssl)
  File "uvloop/loop.pyx", line 1914, in create_connection
socket.gaierror: [Errno 8] nodename nor servname provided, or not known
Task was destroyed but it is pending!
task: <Task pending name='Task-2' coro=<Client._resend_qos_messages() running at /Users/jing/virtualenv/mqtt_test/lib/python3.8/site-packages/gmqtt/client.py:96> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x10d5c2070>()]>>

Below is my code. How can I solve this problem? Thank you.

import asyncio
import os
import signal
import time

from gmqtt import Client

# gmqtt also compatibility with uvloop  
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

STOP = asyncio.Event()


def on_connect(client, flags, rc, properties):
    print('Connected')

def on_message(client, topic, payload, qos, properties):
    print('RECV TOPIC: ', topic)

def on_disconnect(client, packet, exc=None):
    print('Disconnected')

def on_subscribe(client, mid, qos):
    print('SUBSCRIBED, mid: {}'.format(mid))

def ask_exit(*args):
    STOP.set()

async def main(broker_host, token):
    mqtt = Client("test")

    mqtt.on_connect = on_connect
    mqtt.on_message = on_message
    mqtt.on_disconnect = on_disconnect
    mqtt.on_subscribe = on_subscribe

    # mqtt.set_auth_credentials(token, None)
    await mqtt.connect(broker_host, keepalive=60)
    mqtt.subscribe('#')

    data_to_send = '0xDEADBEAF' * (512 * 102)
    mqtt.publish('topic/test', data_to_send)

    await STOP.wait()
    await mqtt.disconnect()


if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.add_signal_handler(signal.SIGINT, ask_exit)
    loop.add_signal_handler(signal.SIGTERM, ask_exit)

    host = 'ws://mqtt.flespi.io'
    token = os.environ.get('FLESPI_TOKEN')
    loop.run_until_complete(main(host, token))

from gmqtt.

Lenka42 avatar Lenka42 commented on July 23, 2024

@jeffreykira WS support feature is not master yet, so in order to test it you should clone the repo and setup gmqtt from support-ws-transport branch

from gmqtt.

jeffreykira avatar jeffreykira commented on July 23, 2024

@Lenka42 Thank you for your reminder. After adjustment I got the following error.

Traceback (most recent call last):
  File "gmqtt_websocket.py", line 56, in <module>
    loop.run_until_complete(main(host, token))
  File "uvloop/loop.pyx", line 1456, in uvloop.loop.Loop.run_until_complete
  File "gmqtt_websocket.py", line 39, in main
    await mqtt.connect(broker_host, keepalive=60)
  File "/Users/jing/workspace/mqtt/gmqtt/gmqtt/client.py", line 152, in connect
    raise self._error
gmqtt.gmqtt.mqtt.handler.MQTTConnectError: code 134 (Connection Refused: Bad User Name or Password)

from gmqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.