import websocket
import threading
import math
import json
import time
moon_addr = "127.0.0.1"
def on_open(wsapp):
    print("打开新的连接")
    input("回车以启动同步: ")
    message = {
        "id": "pulsar",
        "sendto": "comet",
        "content": "-3"
    }
    wsapp.send(json.dumps(message))
    x = round(time.time()*1000)
    while 1:
        x = round(time.time()*1000)
        x = x % 628
        message["content"] = str(math.sin(x*0.01))
        wsapp.send(json.dumps(message))
        time.sleep(0.01)

def on_message(wsapp, message):
    print("on_message:", message)

def on_close(wsapp):
    print("on_close")

wsapp = websocket.WebSocketApp(f"ws://{moon_addr}:8765",
on_open=on_open,
on_message=on_message,
on_close=on_close)
wsapp.run_forever()