Skip to content

Commit

Permalink
Fix ws connect panic (#727)
Browse files Browse the repository at this point in the history
* fix websocket connect panic on malformed url

* fix build failure

* fix review

* fix cargo fmt
  • Loading branch information
lizhaoxian authored and jstarry committed Nov 10, 2019
1 parent c178d52 commit e4b6512
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/dashboard/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl Component for Model {
});
let task =
self.ws_service
.connect("ws://localhost:9001/", callback, notification);
.connect("ws://localhost:9001/", callback, notification).unwrap();
self.ws = Some(task);
}
WsAction::SendData(binary) => {
Expand Down
11 changes: 8 additions & 3 deletions src/services/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,16 @@ impl WebSocketService {
url: &str,
callback: Callback<OUT>,
notification: Callback<WebSocketStatus>,
) -> WebSocketTask
) -> Result<WebSocketTask, &str>
where
OUT: From<Text> + From<Binary>,
{
let ws = WebSocket::new(url).unwrap();
let ws = WebSocket::new(url);
if ws.is_err() {
return Err("Failed to created websocket with given URL");
}

let ws = ws.unwrap();
ws.set_binary_type(SocketBinaryType::ArrayBuffer);
let notify = notification.clone();
ws.add_event_listener(move |_: SocketOpenEvent| {
Expand All @@ -80,7 +85,7 @@ impl WebSocketService {
callback.emit(out);
}
});
WebSocketTask { ws, notification }
Ok(WebSocketTask { ws, notification })
}
}

Expand Down

0 comments on commit e4b6512

Please sign in to comment.