1

I am using a simpleWebSocket server class and have a 1 second interval timer that I would like to call methods in a couple of different classes.

the wsscb() class is the handler for the SimpleWebSocketServer(), how can I call a method from the wss() object from another object such as the udt() timer ?

Calling wss.wsscb().myfunc() results in an error: "AttributeError: 'SimpleWebSocketServer' object has no attribute 'wsscb'"

calling wsscb.myfunc() results in: TypeError: unbound method myfunc() must be called with wsscb instance as first argument (got nothing instead)

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

  def myfunc(self):
    self.send('some data')

  def handleMessage(self):
    pass

  def handleConnected(self):
    pass

class udt(Thread):
  def __init__(self, event):
    Thread.__init__(self)
    self.stopped = event

  def run(self):
    while not self.stopped.wait(1.00):
      wss.wsscb().myfunc()
      xxx.yyy()().anotherfunc()


## Main
wss = SimpleWebSocketServer('', 4545,wsscb)

## Start Timer
stopFlag = Event()
self.udt = udt(stopFlag)
self.udt.start()    

wss.serveforever()
crankshaft
  • 2,607
  • 4
  • 45
  • 77
  • Why do you do `wss.wsscb`? The error says `wsscb` is not an attribute of `wss`. – Gohn67 Oct 22 '14 at 02:11
  • WHy not just wsscb.myfunc()? Also, what library is this, did you create the SimpleWebSocketServer class, or what's it from? – Parker Oct 22 '14 at 02:12
  • Thanks, I am trying to call myfunc() from the object wss – crankshaft Oct 22 '14 at 02:13
  • Why do you need to call `myfunc` on run? I'm not sure I understand. I thought `wsscb` would be initialized by your socket server whenever a request hit your socket server. – Gohn67 Oct 22 '14 at 02:16
  • 1
    I never used this library, but I took a look at their simple example: http://opiate.github.io/SimpleWebSocketServer/ (I think that's the library you're using) – Gohn67 Oct 22 '14 at 02:17
  • @crankshaft, why do you need to do `wss.wsscb().myfunc()`? Why not just do `wsscb.myfunc()` – Parker Oct 22 '14 at 02:18
  • udt is a seperate class that is basically an interval time that runs once a second, from this interval timer I would like to call the myfunc() method of wsscb() which is a subclass of simpleSocketServer() – crankshaft Oct 22 '14 at 02:18
  • wsscb.myfunc() results in: TypeError: unbound method myfunc() must be called with wsscb instance as first argument (got nothing instead) – crankshaft Oct 22 '14 at 02:19

1 Answers1

0

There are a couple problems.

wss.wsscb() isn't valid. Typing that means you're trying to call a function in wss called wsscb(). wss is a SimpleWebSocketServer, and there is no function called wsscb(). A function is not the same as calling an object.

wsscb() won't work either, because in your class, you're saying it's takes a WebSocket object, which I assume takes some parameters, so you need to pass it those.

I think it would be best to make a subclass of SimpleWebSocketServer (instead of WebSocket), and put your custom function in there. Your comment says "wsscb() is a subclass of SimpleSocketServer", but it is not. It's a subclass of WebSocket.

You also never created an object of type wsscb.

If you can explain what you're specifically trying to achieve, and what myfunc() is, we may be able to help more

Also, you really shouldn't subclass Thread. Scrap the udt class you made and instead

def myfunc(wsscb_object):
     while True:
         time.sleep(1)
         wsscb_object.myfunc()
         #whatever else you want

 wsscb_object = wsscb(#pass the parameters)
 thread = Thread(target=myfunc, args=(some_socket))
 thread.start()

You may also want to read up more on inheritance:

Community
  • 1
  • 1
Parker
  • 8,539
  • 10
  • 69
  • 98
  • Thanks for helping, this is only a code snippet, I am running the websocket server forever, and every second I want to send a message to connected clients from the udt class, myfunc will call the self.send() method to transmit some data to the connected client – crankshaft Oct 22 '14 at 02:31
  • See http://stackoverflow.com/a/660974/380451 for a better way to handle threading. There's really no reason to subclass `Thread` – Parker Oct 22 '14 at 02:37