Skip to content

Commit

Permalink
Merge pull request #217 from holochain/0.16-fix-ws-protocol-for-trycp…
Browse files Browse the repository at this point in the history
…-client

Fix ws protocol for trycp client
  • Loading branch information
ThetaSinner committed Jun 14, 2024
2 parents e2718d0 + c2f531d commit c332a7f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Removed
### Changed
### Fixed
- The `trycp_client` now handles ping/pong messages from the TryCP server to keep the connection alive.
- The `trycp_client` now handles the `close` event from the TryCP server to close the connection.

## 2024-06-13: v0.16.0
### Changed
Expand Down
24 changes: 21 additions & 3 deletions crates/trycp_client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,30 @@ impl TrycpClient {

let (recv_send, recv_recv) = tokio::sync::mpsc::channel(32);

let ws = Arc::new(tokio::sync::Mutex::new(sink));
let ws2 = ws.clone();
let pend2 = pend.clone();
let recv_task = tokio::task::spawn(async move {
while let Some(Ok(msg)) = stream.next().await {
let msg = msg.into_data();
let msg = match msg {
Message::Close(close_msg) => {
eprintln!("Received websocket close from TryCP server: {}", close_msg.map(|f| f.reason).unwrap_or("No reason".into()));
break;
}
Message::Ping(p) => {
ws2.lock().await.send(Message::Pong(p)).await.unwrap();
continue;
}
Message::Pong(_) => {
continue;
}
Message::Binary(msg) => {
msg
}
_ => {
panic!("Unexpected message from TryCP server: {:?}", msg);
}
};
let msg: MessageToClient = rmp_serde::from_slice(&msg).unwrap();

match msg {
Expand All @@ -91,8 +111,6 @@ impl TrycpClient {
}
});

let ws = Arc::new(tokio::sync::Mutex::new(sink));

Ok((
Self {
ws,
Expand Down
2 changes: 1 addition & 1 deletion crates/trycp_server/src/app_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub(crate) async fn connect(
port: u16,
response_writer: Arc<futures::lock::Mutex<WsResponseWriter>>,
) -> Result<(), ConnectError> {
let connection_lock = Arc::clone(&CONNECTIONS.lock().await.entry(port).or_default());
let connection_lock = Arc::clone(CONNECTIONS.lock().await.entry(port).or_default());

let mut connection = connection_lock.lock().await;
if connection.is_some() {
Expand Down
2 changes: 1 addition & 1 deletion crates/trycp_server/src/download_dna.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(crate) async fn download_dna(url_str: String) -> Result<String, DownloadDnaE
fn get_downloaded_dna_path(url: &url::Url) -> PathBuf {
Path::new(DNA_DIR_PATH)
.join(url.scheme())
.join(url.path().replace("/", "").replace("%", "_"))
.join(url.path().replace('/', "").replace('%', "_"))
}

/// Tries to create a file, returning Ok(None) if a file already exists at path
Expand Down

0 comments on commit c332a7f

Please sign in to comment.