Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send binary data with websocket client python use WebSocketApp (not create_connection)

I selected WebSocketApp because it can remain connected forever. Unfortunately WebSocketApp does not offer ws.send_binary() just like create_connection. I want send binary data message and decode incoming message, Please help me, here is the original example:

import websocket
def on_message(ws, message):
    print(message)
def on_error(ws, error):
    print(error)
def on_close(ws):
    print('Websocket: closed')
def on_open(ws):
    print('Websocket: open')
ws = websocket.WebSocketApp('ws://echo.websocket.org/',on_message = on_message,on_error = on_error,on_close = on_close,on_open = on_open)
ws.run_forever()
like image 888
SolaticSama John Avatar asked Sep 04 '25 02:09

SolaticSama John


2 Answers

Try this one:

ws.send(data, websocket.ABNF.OPCODE_BINARY)
like image 88
dimitris mastro Avatar answered Sep 07 '25 05:09

dimitris mastro


You could use websockets or ws4py(ws.send(buf, binary=True)), both can send binary data directly.

like image 29
Harrison Avatar answered Sep 07 '25 07:09

Harrison