-2

this code:

SOCKET="wss://stream.binance.com:9443/ws/bitusdt@kline_15m"

def on_open(ws):
  print(" opend connection ")

def on_close(ws):  
  print(" closed connection ")

def on_message(ws,message):
  print(" receive message ")
  json_message=json.loads(message)
  pprint.pprint(json_message)
  

  candle=json_message['k']
  is_closed=candle['x']
  if is_closed:
    close=float(candle['c'])
    high=float(candle['h'])
    low=float(candle['l'])
    vol=float(candle['v'])
    time=pd.to_datetime(candle['t'],unit='ms')
    record={'high':high,'low':low,'volum':vol}    
    print(close)
    print('------')

ws=websocket.WebSocketApp(SOCKET,on_open= on_open,on_close=on_close, on_message= on_message)
ws.run_forever()

the problem is in close function ,it requires other parameters as far as I understand

error is : ERROR:websocket:Handshake status 451 None - goodbye ERROR:websocket:error from callback <function on_close at 0x7f61b5e74f70>: on_close() takes 1 positional argument but 3 were given True

1 Answers1

0

the problem is in close function ,it requires other parameters as far as I understand

True. In such case, you should look for the document of the libraries used here.

https://websocket-client.readthedocs.io/en/latest/app.html

The document says:

on_close (function) – Callback object which is called when connection is closed. on_close has 3 arguments. The 1st argument is this class object. The 2nd argument is close_status_code. The 3rd argument is close_msg.

If this looks confusing, they also provides some examples:

https://websocket-client.readthedocs.io/en/latest/examples.html#receiving-connection-close-status-codes

import websocket

websocket.enableTrace(True)

def on_close(wsapp, close_status_code, close_msg):
    # Because on_close was triggered, we know the opcode = 8
    print("on_close args:")
    if close_status_code or close_msg:
        print("close status code: " + str(close_status_code))
        print("close message: " + str(close_msg))

def on_open(wsapp):
    wsapp.send("Hello")

wsapp = websocket.WebSocketApp("wss://tsock.us1.twilio.com/v3/wsconnect", on_close=on_close, on_open=on_open)
wsapp.run_forever()

For your codes, simply change on_xxx() callbacks' signature to the newest version.

halfelf
  • 9,737
  • 13
  • 54
  • 63