zoukankan      html  css  js  c++  java
  • python实现的WebSocket客户端

    code

    #coding=utf-8
    import json
    import time
    from websocket import create_connection
    
    ws = create_connection("ws://x.x.x.x:8090/haiyou/device")
    print("Sending 'Hello, World'...")
    t=str(time.time()).split(".")[0]
    params={
        "version": 1,
        "msgNo": "1",
        "machNo": "U040119110001",
        "cmd": 1,
        "time": 1574064604418
    }
    
    ws.send(json.dumps(params))
    print("Sent")
    print("Reeiving...")
    result = ws.recv()
    print("Received '{}'".format(result))
    
    
    params={
        "version": 1,
        "msgNo": t,
        "machNo": "U040119110001",
        "cmd": 7,
        "time": t,
        "data": {
            "userId": 8,
            "companyType": 3,
            "before": 0,
            "after": 100,
            "openTime": t,
            "closeTime": t
        }
    }
    
    ws.send(json.dumps(params))
    print("Sent")
    print("Reeiving...")
    result = ws.recv()
    print("Received '{}'".format(result))
    
    
    ws.close()

     整理后

    #coding=utf-8
    import json
    import time
    from websocket import create_connection
    
    class websocket:
        def __init__(self,address):
            self.ws = create_connection(address)
        
        def send(self,params):
            print("Sending ...")
            self.ws.send(json.dumps(params))
            print("Reeiving...")
            result = self.ws.recv()
            print("Received '{}'".format(result))
    
        def quit(self):
            self.ws.close()
            
    t=str(time.time()*1000).split(".")[0]
    address="ws://39.106.85.158:8090/haiyou/device"
    
    params1={
        "version": 1,
        "msgNo":t,
        "machNo": "U040119110001",
        "cmd": 1,
        "time":t 
    }
    
    params2={
        "version": 1,
        "msgNo": t,
        "machNo": "U040119110001",
        "cmd": 7,
        "time": t,
        "data": {
            "userId": 8,
            "companyType": 3,
            "before": 0,
            "after": 100,
            "openTime": t,
            "closeTime": t
        }
    }
    
    #初始化
    webso=websocket(address)
    
    #发送数据
    webso.send(params1)
    webso.send(params2)
    
    #断开连接
    webso.quit()

    参考:

    https://blog.csdn.net/qq562029186/article/details/81203893

  • 相关阅读:
    最近的几个坑
    最近对Allegro的几个总结
    sub drawing
    Allegro的几点小结
    产品量产的几个问题
    电源板问题的定位
    PCB学习
    servlet(6) 链接数据库
    servlet(5) HttpSession
    servlet(4)异常处理
  • 原文地址:https://www.cnblogs.com/sea-stream/p/11990209.html
Copyright © 2011-2022 走看看