websockets-rs / websockets-rs/rust-websocket

Should server example using coroutines be added?

Open
#144 7 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.6k
Forks
225
PR merge metrics
No merged PRs in 30d

Description

I've created a port of "async-autobahn-server.rs" (sans error handling) using the futures-await / experimental coroutines.

#![feature(proc_macro, conservative_impl_trait, generators)]

extern crate websocket;
//extern crate futures;
extern crate futures_await as futures;
extern crate tokio_core;

use futures::prelude::*;

use websocket::message::OwnedMessage;
use websocket::async::Server;

use tokio_core::reactor::Core;
use futures::{Future, Sink, Stream};

type UsualClient = websocket::client::async::Framed<tokio_core::net::TcpStream, websocket::async::MessageCodec<websocket::OwnedMessage>>;

#[async]
fn serve_client(client: UsualClient) -> std::result::Result<(),()> {
    let (mut sink, stream) = client.split();
    #[async]
    for m in stream.map_err(|_|()) {
        if m.is_close() { break; }
        let fwd = match m {
            OwnedMessage::Ping(p) => OwnedMessage::Pong(p),
            OwnedMessage::Pong(_) => continue,
            _ => m,
        };
        sink = await!(sink.send(fwd).map_err(|_|()))?;
    }
    await!(sink.send(OwnedMessage::Close(None)).map_err(|_|()))?;
    Ok(())
}

fn main() {
    let mut core = Core::new().unwrap();
    let handle = core.handle();
    let server = Server::bind("127.0.0.1:9002", &handle).unwrap();

    let str = server.incoming().map_err(|_|());
	
    let f = async_block! {
        #[async]
        for (upgrade, addr) in str {
            // accept the request to be a ws connection
            println!("Got a connection from: {}", addr);
            let (client, _headers) = await!(upgrade.accept().map_err(|_|()))?;
            let f = serve_client(client);
            handle.spawn(f.map_err(|_|()).map(|_|()));
        }
        Ok::<(),()>(())
    };

    core.run(f).unwrap();
}

Should it be added to the examples or better to wait for stabilization of respective Rust features?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Review the existing async-autobahn-server.rs example and the Rust futures-await and experimental coroutines references in the issue. Check the comment discussion and current project examples before deciding whether this belongs in the repository. Done means reaching a maintainer-backed decision about adding or deferring the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.