imranmaj / websockets Goto Github PK
View Code? Open in Web Editor NEWA WebSocket client implementation
License: MIT License
A WebSocket client implementation
License: MIT License
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?
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)
It seems that using the following instruction is not valid
export https_proxy=http://127.0.0.1:7890; export http_proxy=http://127.0.0.1:7890; export all_proxy=socks5://127.0.0.1:7890
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))?
)
}
}
thread 'main' panicked at 'called Result::unwrap()
on an Err
value: InvalidHandshakeError', src/main.rs:12:86
I think it would be a good idea to be able to use non-default SSL configuration as a parameter to the connect function similar to: https://docs.rs/websocket/0.26.2/websocket/client/builder/struct.ClientBuilder.html#method.connect
What do you think? I could open a PR for this.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.