Code Monkey home page Code Monkey logo

websockets's People

Contributors

imranmaj avatar secana avatar thsioutas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

websockets's Issues

Are the methods implemented on Websocket cancellation-safe?

Is cancellation of futures yielded by methods of this library (e.g. Websocket::receive) safe?

Imagine a scenario like this

async fn foo() {
  // ...
  tokio::select!{
    _ = done_soon() => {
      do_stuff()
    },
    _ = websocket.receive() => {
      // ...
    }
  }
}

If done_soon finishes while Websocket::receive processes an incoming Frame, will the frame be lost?

Stream + Sink

I need a way to both send and receive at the same time by fetching a stream and sink. Something like this:

let mut conn = WebSocket::connect(&url).await.unwrap();
let (sink, stream) = conn.duplex().split();

let conn_stream = stream! {
    while let Some(frame) = stream.next().await {
        // Return a frame for certain responses
        yield frame;
    }
};

// Combine frame responses from frames received through a channel
futures::stream::select(conn_stream, frame_receiver.into_stream())
    .forward(sink)

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: InvalidHandshakeError'

Not sure what's happening here, I am connecting to a SSL secured web socket, and getting this error for connection. (The URL provided is not the valid URL).

main.rs

#[tokio::main]
async fn main() {
    let mut binding = WebSocket::builder();
    let options = binding
        .add_header("Sec-WebSocket-Protocol", "wamp.json");

    let mut client = WampClient::connect("wss://chat.some.co/chat", options).await.unwrap();

    client.send(Hello { 
        realm: "co.fun.chat".to_string(), 
        details: json::object! {
           ...
        }
    }).await.unwrap();

    client.loop_messages().await;
}

client.rs

pub struct WampClient {
    websocket: WebSocket,
    ...
}

impl WampClient {
    pub async fn connect(url: &str, mut options: &mut WebSocketBuilder) -> Result<Self, WebSocketError> {
        let websocket = options.connect(url).await?;
        
        Ok(Self { 
            websocket, 
            ...
        })
    }
    
   pub async fn send<M: WampMessageTrait>(&mut self, message: M) -> Result<(), Error> {
        Ok(self.websocket.send_text(
          message
                  .to_json()
                  .map_err(|e| Error::JsonError(e))?
                  .to_string()
              )
              .await
              .map_err(|e| Error::WsError(e))?
       )
    }
}

Error

thread 'main' panicked at 'called Result::unwrap() on an Err value: InvalidHandshakeError', src/main.rs:12:86

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.