Sha256: 6add13d99cf9600618d1d85e8c3c7a2c3683255e7c9166d9a49ac329aa46be15

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

import asyncio
import struct
from websockets.sync.client import connect

from javonet.utils.connectionData.WsConnectionData import WsConnectionData


class WebSocketClient:
    _instance = None
    clients = dict()

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(WebSocketClient, cls).__new__(cls)
        return cls._instance

    def add_or_get_client(self, connection_data: WsConnectionData):
        if connection_data.hostname in self.clients:
            return self.clients[connection_data.hostname]
        else:
            self.clients[connection_data.hostname] = connect(connection_data.hostname)
            return self.clients[connection_data.hostname]

    def send_message(self, connection_data: WsConnectionData, serialized_command):
        byte_array = struct.pack("B" * len(serialized_command), *serialized_command)
        return self.send(connection_data, byte_array)

    def send(self, connection_data: WsConnectionData, byte_array):
        websocket = self.add_or_get_client(connection_data)
        websocket.send(byte_array)
        response = websocket.recv()
        return response

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
javonet-ruby-sdk-2.4.10 lib/javonet-ruby-sdk/Binaries/Python/Windows/X64/javonet/core/webSocketClient/WebSocketClient.py
javonet-ruby-sdk-2.4.9 lib/javonet-ruby-sdk/Binaries/Python/Windows/X64/javonet/core/webSocketClient/WebSocketClient.py