question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How use socketio decorator within another class

See original GitHub issue

Hi Miguel I tried to use socketio.Client within a class where i can call socketio and its related event frequently.But dnt understand how figure out it. It works fine on normal python file.

i m a part time coder,so plz dnt laugh on my coding. Thanks in advance.

import socketio

class warSock:

    global mwar
    global name
    global ps
    mwar = socketio.Client()

    def __init__(self, name, ps):
        self.name = name
        self.ps = ps

    @mwar.event
    def connect(self):
        print('connected to server')

    @mwar.on('login')
    def on_login(data):
        print data

    def connectme(self):
        mwar.connect('http://localhost')

    def dc(self):
        mwar.disconnect()

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

27reactions
Robokishancommented, Mar 1, 2021

i know this is old but i wanted to have this kind of functionality and i have done this

import socketio
import json

class Wrapper_class():
        sio = socketio.Client()

        def __init__(self):
            pass
        def setup(self):
            self.call_backs()
            self.sio.connect('http://localhost.local:8081')

        def loop(self): 
            self.sio.wait()

        def call_backs(self):
            @self.sio.event
            def connect():
                self.sio.emit('subscribe','room')
                print('connection established')
                
            @self.sio.on("docs")
            def raw_data(data):
                print(f"Data Received {data}")


            @self.sio.event
            def auth(data):
                print(f"Data Received {data}")


            @self.sio.event
            def disconnect():
                print('disconnected from server')
        def run(self):
            self.setup()
            self.loop()

wrapper = Wrapper_class()
wrapper.run()
1reaction
Zartriscommented, Jun 24, 2020

This might be old and you have solved it. If you don’t need the self functionality in connect just make it static like this:

@staticmethod @mwar.event def connect(): print(‘connection established’)

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How to decorate class functions with flask-socketio if ...
Since decorating a function simply calls the decorator and passes the decorated function as the first argument you can write:
Read more >
The Socket.IO Client — python-socketio documentation
To instantiate an Socket.IO client, simply create an instance of the appropriate client class: import socketio # standard Python sio = socketio.
Read more >
python-socketio Documentation - Read the Docs
python-socketio Documentation. 2.10 Class-Based Namespaces. As an alternative to the decorator-based event handlers, the event handlers that belong to a ...
Read more >
python-socketio Documentation - manpages.ubuntu!
This package contains two Socket.IO clients: • The socketio.Client() class creates a client compatible with the standard Python library. • The socketio.
Read more >
How to manage users in socket.io in Node.js ? - GeeksforGeeks
js and express. Create two folders in your main directory name server(backend) and client(frontend). Socket.on will be an event that will be ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found