import websocket
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("WebSocket连接已关闭")
def on_open(ws):
print("WebSocket连接已打开")
# 在连接建立后可以发送消息
ws.send("Hello, WebSocket!")
if __name__ == "__main__":
websocket.enableTrace(True) # 开启调试信息
ws = websocket.WebSocketApp("ws://echo.websocket.org/",
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.on_open = on_open
ws.run_forever()
发表评论 取消回复