dpallot / simple-websocket-server

A python based websocket server that is simple and easy to use.
951 stars 321 forks source link

adding more local variables to the SimpleChat class #101

Open themindfactory opened 3 years ago

themindfactory commented 3 years ago

for every connection I want to store other variables...

in the class SimpleChat I added

class SimpleChat(WebSocket):
    def __init__(self):
        self.myvariable = "hello"

this does not work, with more research it looked like it should be more:

class SimpleChat(WebSocket):
    def __init__(self):
        super(SimpleChat, self).__init__() #tried also super(WebSocket, self).__init__()
        self.myvariable = "hello" # possibly self['myvariable'] = "hello"

nothing seems to work, not sure where to go....

hoping a python guru comes to my assistance... :-)

Izzy3110 commented 3 years ago

are you really in python or on an other language :D

I'm currently working on a project, where i want to use it, too and i'm facing exactly the same problem

Found the class "WebSocket" in SimpleWebSocketServer.py from the package. I'd recommend doing a "fork" of it or download a copy,

Hope it helps :)

best regards & happy coding

Sascha

pikhovkin commented 3 years ago

Use inherit.

For example:

from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket

class WSRequestHandler(WebSocket):
    def __init__(self, server, sock, address):
        super(WSRequestHandler, self).__init__(server, sock, address)

        ...  # something yet

    ...  # other methods

class SimpleWSServer(SimpleWebSocketServer):
    ...  # some attrs

    def __init__(self, host, port, websocketclass, **kwargs):
        ...  # something yet

        super(SimpleWSServer, self).__init__(host, port, websocketclass)

        ...  # something yet

server = SimpleWSServer(HOST, PORT, WSRequestHandler)
server.serveforever()