Python-socketio: How use socketio decorator within another class

Created on 29 Nov 2019  路  5Comments  路  Source: miguelgrinberg/python-socketio

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()
question

Most helpful comment

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()

All 5 comments

What part of this does not work? The only thing I noticed is that the on_login method is missing self.

As a side note, you may want to look at class-based namespaces in the documentation, as they already provide a way to implement events as methods in a class.

Thanks for your reply.
Problem was from here

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

sio wants connect(), but here class wants connect(self)
thats why sio call argument typeError (something like this)

And i think i sort out this by putting all sio related events inner function(def) of class def. And it works still.
And So much thanks for your great creation.I tried it 9 month earlier,bt i failed to connect single one.
may be i failed to understand the concept of decorator.

I have a request for connection establishment. It got hang on ide as like httplib connection.
I used socketio js and java client.Those are faster.

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')

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()
Was this page helpful?
0 / 5 - 0 ratings