I\'m trying access some data using websockets, but I cannot really get around the examples given in the websockets documentation.
I have this code (https://pypi.org/proj
Just updating the code written by other authors on this page, that worked for me. The problem is that in the event callback functions definition like on_message we should not use ws as parameter. self takes care of it and in the body of these event handler functions we should use self.ws
class MySocket(object):
def __init__(self):
websocket.enableTrace(True)
self.ws = websocket.WebSocketApp("ws://echo.websocket.org:12300/foo",
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
def on_message(self, message):
# if you want to send something use like this
# self.ws.send()
print message
def on_error(self, error):
print error
def on_close(self):
print "### closed ###"
def on_open(self):
self.ws.send("Hello Message")
Package the call inside an anonymous lambda
function to achieve a proper call with the correct self
:
class Client:
def __init__(self, db, symbols):
self.ws = websocket.WebSocketApp("wss://the.server.com/api",
on_message = lambda ws,msg: self.on_message(ws, msg),
on_error = lambda ws,msg: self.on_error(ws, msg),
on_close = lambda ws: self.on_close(ws),
on_open = lambda ws: self.on_open(ws))
def on_message(self, ws, message):
msg = json.loads(message)
print(msg)
...
You need to add "self" to you class methods:
class MySocket(object):
def __init__(self):
websocket.enableTrace(True)
self.ws = websocket.WebSocketApp("ws://echo.websocket.org:12300/foo",
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
def on_message(self, ws, message):
print message
def on_error(self, ws, error):
print error
def on_close(self, ws):
print "### closed ###"
def on_open(self, ws):
ws.send("Hello %d" % i)
The Self makes those methods as Class methods , Got this one working as the on_error/message/close methods signature will get satisfied if called by self as will refer to the class itself .
class MySocket(object):
def __init__(self,x):
websocket.enableTrace(True)
## Only Keep the object Initialisation here
self.x=x
self.ws=None
# call This method from a Object and it will create and run the websocket
def ws_comm(self):
self.ws = websocket.WebSocketApp(self.WS_URL,on_message =
self.on_message,on_error =self.on_error,on_close = self.on_close)
self.ws.on_open = self.on_open
self.ws.run_forever()
def on_error(self,ws, error):
print "onError", error
def on_close(self,ws):
print "onClosed"
#Send some message on open
def on_open(self,ws):
self.ws.send(json.dumps(register_msg))
def on_message(self,ws, msg):
self.ws.send(json.dumps(msg))
user1=Userapp('x')
user1.ws_comm()
This is working:
class MySocket(object):
def __init__(self):
websocket.enableTrace(True)
self.ws = websocket.WebSocketApp("ws://echo.websocket.org:12300/foo",
on_message = self.on_message,
on_error = self.on_error,
on_close = self.on_close)
@staticmethod
def on_message(ws, message):
print message
@staticmethod
def on_error(ws, error):
print error
@staticmethod
def on_close(ws):
print "### closed ###"
@staticmethod
def on_open(ws):
ws.send("Hello %d" % i)
But you don't have access to self
I would like try this way:
class FooClient(object):
def __init__(self):
def on_message(ws, message):
print message
# use 'self' variable to access other resource
# handle message from websocket server like this
self.handler.handle(message)
def on_error(ws, error):
print error
def on_close(ws):
print "### closed ###"
def on_open(ws):
ws.send("Hello %d" % i)
# assign to 'self.handler'
self.handler = FooHandler()
# maybe there are another module should be initiated
# ...
websocket.enableTrace(True)
self.ws = websocket.WebSocketApp("ws://echo.websocket.org:12300/foo",
on_message = on_message,
on_error = on_error,
on_close = on_close)
def run_forever(self):
self.ws.run_forever()
def close(self):
"""clean other resources"""
pass
Using inner function in method __init__(self)
could avoid the problem that the arguments number of on_message(self, ws, message)
method not match with the number of WebSocketApp
provides to its argument on_message
(class method has one more argument self
).
I have a handler
above to handle the message, method close(self)
to clean some resources if I have, run_forever(self)
to run websocket.