|
50 | 50 | //! |
51 | 51 | //! ```no_run |
52 | 52 | //! # async_std::task::block_on(async { |
53 | | -//! use hypercore_protocol::{ProtocolBuilder, Event, Message}; |
54 | | -//! use hypercore_protocol::schema::*; |
55 | 53 | //! use async_std::prelude::*; |
| 54 | +//! use hypercore_protocol::{schema::*, Event, Message, ProtocolBuilder}; |
56 | 55 | //! // Start a tcp server. |
57 | 56 | //! let listener = async_std::net::TcpListener::bind("localhost:8000").await.unwrap(); |
58 | 57 | //! async_std::task::spawn(async move { |
59 | 58 | //! let mut incoming = listener.incoming(); |
60 | 59 | //! while let Some(Ok(stream)) = incoming.next().await { |
61 | | -//! async_std::task::spawn(async move { |
62 | | -//! onconnection(stream, false).await |
63 | | -//! }); |
| 60 | +//! async_std::task::spawn(async move { onconnection(stream, false).await }); |
64 | 61 | //! } |
65 | 62 | //! }); |
66 | 63 | //! |
|
69 | 66 | //! onconnection(stream, true).await; |
70 | 67 | //! |
71 | 68 | //! /// Start Hypercore protocol on a TcpStream. |
72 | | -//! async fn onconnection (stream: async_std::net::TcpStream, is_initiator: bool) { |
| 69 | +//! async fn onconnection(stream: async_std::net::TcpStream, is_initiator: bool) { |
73 | 70 | //! // A peer either is the initiator or a connection or is being connected to. |
74 | 71 | //! let name = if is_initiator { "dialer" } else { "listener" }; |
75 | 72 | //! // A key for the channel we want to open. Usually, this is a pre-shared key that both peers |
|
86 | 83 | //! // The handshake event is emitted after the protocol is fully established. |
87 | 84 | //! Event::Handshake(_remote_key) => { |
88 | 85 | //! protocol.open(key.clone()).await; |
89 | | -//! }, |
| 86 | +//! } |
90 | 87 | //! // A Channel event is emitted for each established channel. |
91 | 88 | //! Event::Channel(mut channel) => { |
92 | 89 | //! // A Channel can be sent to other tasks. |
|
97 | 94 | //! eprintln!("{} received message: {:?}", name, message); |
98 | 95 | //! } |
99 | 96 | //! }); |
100 | | -//! }, |
| 97 | +//! } |
101 | 98 | //! _ => {} |
102 | 99 | //! } |
103 | 100 | //! } |
|
0 commit comments