Skip to content

Commit

Permalink
fix: no auto connect (#66)
Browse files Browse the repository at this point in the history
* fix: no auto connect

update README and make http client work the same as websocket  (no auto connect)

* fix: no auto connect

update README and make http client work the same as websocket  (no auto connect)

* fix: no auto connect

update README and make http client work the same as websocket  (no auto connect)
  • Loading branch information
JarbasAl authored Jan 8, 2025
1 parent e4aab16 commit ef960d0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish_stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Setup Python
uses: actions/setup-python@v1
with:
python-version: 3.8
python-version: "3.11"
- name: Install Build Tools
run: |
python -m pip install build wheel
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ from hivemind_bus_client.http_client import HiveMindHTTPClient

# not passing key etc so it uses identity file
client = HiveMindHTTPClient(host="http://localhost", port=5679)
client.connect() # establish a secure end-to-end encrypted connection
```

via [hivemind-websocket-protocol](https://github.com/JarbasHiveMind/hivemind-websocket-protocol)
Expand All @@ -25,6 +26,7 @@ from hivemind_bus_client.client import HiveMessageBusClient

# not passing key etc so it uses identity file
client = HiveMessageBusClient(host="ws://localhost", port=5678)
client.connect() # establish a secure end-to-end encrypted connection
```

### Example: Simple Chat
Expand Down
10 changes: 6 additions & 4 deletions hivemind_bus_client/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ def __init__(self, key: Optional[str] = None,
self._handlers: Dict[str, List[Callable[[HiveMessage], None]]] = {}
self._agent_handlers: Dict[str, List[Callable[[MycroftMessage], None]]] = {}
self.start()
self.wait_for_handshake()


def wait_for_handshake(self, timeout=5):
self.connected.wait(timeout=timeout)
self.handshake_event.wait(timeout=timeout)
if not self.handshake_event.is_set():
self.protocol.start_handshake()
Expand Down Expand Up @@ -239,8 +238,9 @@ def _handle_hive_protocol(self, message: HiveMessage):
# main loop
def run(self):
self.stopped.clear()

# Connect to the server
self.connect()
self.connected.wait()

# Retrieve messages until stop
while not self.stopped.is_set():
Expand Down Expand Up @@ -358,7 +358,8 @@ def connect(self, bus=FakeBus(), protocol=None, site_id=None):
self.protocol.bind(bus)
url = f"{self.base_url}/connect"
response = requests.post(url, params={"authorization": self.auth})
self.connected.set() # TODO validate no error in post request
self.connected.set()
self.wait_for_handshake()
return response.json()

def disconnect(self) -> dict:
Expand Down Expand Up @@ -419,6 +420,7 @@ def handle_receive_tts(self, bin_data: bytes,
# not passing key etc so it uses identity file
client = HiveMindHTTPClient(host="http://localhost", port=5679,
bin_callbacks=BinaryDataHandler())
client.connect()

# send HiveMessages as usual
client.emit(HiveMessage(HiveMessageType.BUS,
Expand Down

0 comments on commit ef960d0

Please sign in to comment.