pub async fn connect_to_websocket<H: WebsocketHandler + 'static>(
        &mut self,
        handler: H,
    ) -> Result<(), ApiError> {
        let url = format!("{}/api/v4/websocket", self.instance_url).replace("https", "wss");
        let (mut stream, _response) = async_tungstenite::tokio::connect_async(url).await?;
        stream
            .send(Message::Text(serde_json::to_string(&json!({
              "seq": 1,
              "action": "authentication_challenge",
              "data": {
                "token": self.auth_token.as_ref().unwrap()
              }
            }))?))
            .await?;
        loop {
            if let Some(event) = stream.next().await {
                match event {
                    Ok(message) => match message {
                        Message::Text(text) => {
                            // for now, replies are not sent to the handler
                            if !text.contains("seq_reply") {
                                match serde_json::from_str(&text) {
                                    Ok(as_struct) => handler.callback(as_struct).await,
                                    Err(e) => error!("Could not parse websocket event JSON: {}", e),
                                }
                            }
                        }
                        Message::Binary(_) => debug!("Websocket binary message"),
                        Message::Ping(_) => debug!("Websocket ping message"),
                        Message::Pong(_) => debug!("Websocket pong message"),
                        Message::Close(_) => break,
                    },
                    Err(e) => {
                        error!("Error getting data from websocket: {}", e);
                    }
                }
            };
        }
        Ok(())
    }
/// Connect to the websocket API on the instance.
///
/// This method loops, sending messages received from
/// the websocket connection to the passed handler. The
/// authentication handshake is handled with the
/// connection is made, but otherwise no handling of
/// messages is currently implemented.
///
/// This function is likely to experience a great
/// deal of change soon.
///
/// # Example
///
/// ```rust,no_run
/// use async_trait::async_trait;
/// use mattermost_api::prelude::*;
///
/// struct Handler {}
///
/// #[async_trait]
/// impl WebsocketHandler for Handler {
///     async fn callback(&self, message: WebsocketEvent) {
///         println!("{:?}", message);
///     }
/// }
///
/// # async fn run() {
/// let auth = AuthenticationData::from_password("you@example.com", "password");
/// let mut api = Mattermost::new("https://your-mattermost-instance.com", auth);
/// api.store_session_token().await.unwrap();
/// api.connect_to_websocket(Handler {}).await.unwrap();
/// # }
/// ```
Connect to the websocket API on the instance.
This method loops, sending messages received from
the websocket connection to the passed handler. The
authentication handshake is handled with the
connection is made, but otherwise no handling of
messages is currently implemented.

This function is likely to experience a great
deal of change soon.
Connect to the websocket API on the instance . This method loops sending messages received from the websocket connection to the passed handler . The authentication handshake is handled with the connection is made but otherwise no handling of messages is currently implemented . This function is likely to experience a great deal of change soon .
pub async fn connect_to_websocket<H: WebsocketHandler + 'static>(
        &mut self,
        handler: H,
    ) -> Result<(), ApiError> {
        let url = format!("{}/api/v4/websocket", self.instance_url).replace("https", "wss");
        let (mut stream, _response) = async_tungstenite::tokio::connect_async(url).await?;
        stream
            .send(Message::Text(serde_json::to_string(&json!({
              "seq": 1,
              "action": "authentication_challenge",
              "data": {
                "token": self.auth_token.as_ref().unwrap()
              }
            }))?))
            .await?;
        loop {
            if let Some(event) = stream.next().await {
                match event {
                    Ok(message) => match message {
                        Message::Text(text) => {
                            // for now, replies are not sent to the handler
                            if !text.contains("seq_reply") {
                                match serde_json::from_str(&text) {
                                    Ok(as_struct) => handler.callback(as_struct).await,
                                    Err(e) => error!("Could not parse websocket event JSON: {}", e),
                                }
                            }
                        }
                        Message::Binary(_) => debug!("Websocket binary message"),
                        Message::Ping(_) => debug!("Websocket ping message"),
                        Message::Pong(_) => debug!("Websocket pong message"),
                        Message::Close(_) => break,
                    },
                    Err(e) => {
                        error!("Error getting data from websocket: {}", e);
                    }
                }
            };
        }
        Ok(())
    }
pub async fn connect_to_websocket < H : WebsocketHandler + ' static > ( & mut self , handler : H , ) -> Result < ( ) , ApiError > { let url = format ! ( " " self instance_url ) . replace ( " " , " " ) ; let ( mut stream , _response ) = async_tungstenite :: tokio :: connect_async ( url ) . await ? ; stream . send ( Message :: Text ( serde_json :: to_string ( & json ! ( { " " 1 " " " " " " { " " self auth_token as_ref ( ) unwrap ( ) } } ) ) ? ) ) . await ? ; loop { if let Some ( event ) = stream . next ( ) . await { match event { Ok ( message ) => match message { Message :: Text ( text ) => { // for now, replies are not sent to the handler if ! text . contains ( " " ) { match serde_json :: from_str ( & text ) { Ok ( as_struct ) => handler . callback ( as_struct ) . await , Err ( e ) => error ! ( " " e ) , } } } Message :: Binary ( _ ) => debug ! ( " " ) , Message :: Ping ( _ ) => debug ! ( " " ) , Message :: Pong ( _ ) => debug ! ( " " ) , Message :: Close ( _ ) => break , } , Err ( e ) => { error ! ( " " e ) ; } } } ; } Ok ( ( ) ) }
Connect to the websocket API on the instance.
Connect to the websocket API on the instance .
&mut self
handler
H
&mut self
handler
H
example
```rust,no_run
use async_trait::async_trait;
use mattermost_api::prelude::*.

struct Handler {}
` ` ` rust no_run use async_trait :: async_trait ; use mattermost_api :: prelude :: * . struct Handler {}
[async_trait]
impl WebsocketHandler for Handler {
async fn callback(&self, message: WebsocketEvent) {
println!("{:?}", message);
}
}
impl WebsocketHandler for Handler { async fn callback ( &self message : WebsocketEvent ) { println! ( " { : ? } " message ) ; } }
async fn run() {
None
}
None