Add examples/hello_world_await.rs
This commit is contained in:
		
							parent
							
								
									02bbfd8664
								
							
						
					
					
						commit
						71605f0113
					
				| @ -30,6 +30,7 @@ optional = true | |||||||
| version = "0.1.4" | version = "0.1.4" | ||||||
| 
 | 
 | ||||||
| [dev-dependencies] | [dev-dependencies] | ||||||
|  | futures-await = { git = 'https://github.com/alexcrichton/futures-await' } | ||||||
| ruma-events = "0.9.0" | ruma-events = "0.9.0" | ||||||
| 
 | 
 | ||||||
| [features] | [features] | ||||||
|  | |||||||
							
								
								
									
										73
									
								
								examples/hello_world_await.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								examples/hello_world_await.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,73 @@ | |||||||
|  | #![feature(conservative_impl_trait)] | ||||||
|  | #![feature(generators)] | ||||||
|  | #![feature(proc_macro)] | ||||||
|  | #![feature(try_from)] | ||||||
|  | 
 | ||||||
|  | extern crate futures_await as futures; | ||||||
|  | extern crate ruma_client; | ||||||
|  | extern crate ruma_events; | ||||||
|  | extern crate ruma_identifiers; | ||||||
|  | extern crate tokio_core; | ||||||
|  | extern crate url; | ||||||
|  | 
 | ||||||
|  | use std::convert::TryFrom; | ||||||
|  | 
 | ||||||
|  | use futures::prelude::*; | ||||||
|  | use ruma_client::Client; | ||||||
|  | use ruma_client::api::r0; | ||||||
|  | use ruma_events::EventType; | ||||||
|  | use ruma_events::room::message::{MessageEventContent, MessageType, TextMessageEventContent}; | ||||||
|  | use ruma_identifiers::RoomAliasId; | ||||||
|  | use tokio_core::reactor::{Core as TokioCore, Handle as TokioHandle}; | ||||||
|  | use url::Url; | ||||||
|  | 
 | ||||||
|  | fn hello_world( | ||||||
|  |     tokio_handle: &TokioHandle, | ||||||
|  |     homeserver_url: Url, | ||||||
|  | ) -> impl Future<Item = (), Error = ruma_client::Error> + 'static { | ||||||
|  |     let client = Client::https(tokio_handle, homeserver_url, None).unwrap(); | ||||||
|  | 
 | ||||||
|  |     async_block! { | ||||||
|  |         await!(client.register_guest())?; | ||||||
|  | 
 | ||||||
|  |         let response = await!(r0::alias::get_alias::call( | ||||||
|  |             client.clone(), | ||||||
|  |             r0::alias::get_alias::Request { | ||||||
|  |                 room_alias: RoomAliasId::try_from("#ruma-client-test:matrix.org").unwrap(), | ||||||
|  |             } | ||||||
|  |         ))?; | ||||||
|  | 
 | ||||||
|  |         let room_id = response.room_id; | ||||||
|  | 
 | ||||||
|  |         await!(r0::membership::join_room_by_id::call( | ||||||
|  |             client.clone(), | ||||||
|  |             r0::membership::join_room_by_id::Request { | ||||||
|  |                 room_id: room_id.clone(), | ||||||
|  |                 third_party_signed: None, | ||||||
|  |             } | ||||||
|  |         ))?; | ||||||
|  | 
 | ||||||
|  |         await!(r0::send::send_message_event::call( | ||||||
|  |             client.clone(), | ||||||
|  |             r0::send::send_message_event::Request { | ||||||
|  |                 room_id: room_id, | ||||||
|  |                 event_type: EventType::RoomMessage, | ||||||
|  |                 txn_id: "1".to_owned(), | ||||||
|  |                 data: MessageEventContent::Text(TextMessageEventContent { | ||||||
|  |                     body: "Hello World!".to_owned(), | ||||||
|  |                     msgtype: MessageType::Text, | ||||||
|  |                 }), | ||||||
|  |             } | ||||||
|  |         ))?; | ||||||
|  | 
 | ||||||
|  |         Ok(()) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn main() { | ||||||
|  |     let mut core = TokioCore::new().unwrap(); | ||||||
|  |     let handle = core.handle(); | ||||||
|  |     let server = Url::parse("https://matrix.org/").unwrap(); | ||||||
|  | 
 | ||||||
|  |     core.run(hello_world(&handle, server)).unwrap(); | ||||||
|  | } | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user