【问题标题】:python/ websocket implementation of sending messages to the client that keeps the socket openpython / websocket实现向客户端发送消息以保持套接字打开
【发布时间】:2021-12-07 00:22:21
【问题描述】:

帮我弄清楚如何正确实现它。 底线:客户端是连接、打开套接字并向服务器发送消息并接收响应的充电站。 服务器 - 侦听端口,查看连接的站点,接收消息并向它们发送响应。 问题:当客户端连接并发送标头时,我可以向客户端发送消息。但是我需要定期向保持套接字打开的客户端发送消息,我不明白如何实现这一点。谁能告诉我?

示例发送代码:

charge_point_id = path.strip('/')
    cp = client_main(charge_point_id, websocket)
    logging.info(charge_point_id)
    print(charge_point_id)
    print(path)
    await websocket.send(json.dumps([2,"222", "GetLocalListVersion", {}]))
    await cp.start()

从客户端接收消息的示例:

class client_main(cp):

        errors = False
            
        if not errors:
            
                        
            @on('BootNotification')
            def on_boot_notitication(self,    charge_point_vendor, charge_point_model,charge_point_serial_number,firmware_version, 
                                     meter_type, **kwargs):
               return call_result.BootNotificationPayload(
                    status="Accepted",
                    current_time=date_str.replace('+00:00','Z'),
                    interval=60
                )

在这种情况下,充电站根据ocpp协议打开连接并保持打开状态,应该可以以某种方式给他写信


如何向客户端发送消息?我的例子:

  @on('Heartbeat')
    def on_getlocallistversion(self):
    await self.route_message(json.dumps([2,"222","GetLocalListVersion",{}]))
    def on_hearbeat(self):
        return call_result.HeartbeatPayload(
                    current_time=datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S')+"Z"
       )

我收到一个错误:

等待 self.route_message(json.dumps([2,"222", "GetLocalListVersion",{}]))

【问题讨论】:

    标签: python websocket


    【解决方案1】:

    这是不可能的。只有在客户端连接时才能发送消息。

    【讨论】:

      猜你喜欢
      • 2017-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      相关资源
      最近更新 更多