examples: Modify the wasm example to be similar to the command bot.

master
Damir Jelić 2020-05-12 14:30:33 +02:00 committed by stoically
parent 2a411bf5df
commit 0bb1be94e2
2 changed files with 50 additions and 12 deletions

View File

@ -14,6 +14,7 @@ matrix-sdk = { path = "../..", default-features = false, features = ["encryption
url = "2.1.1" url = "2.1.1"
wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] } wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.12" wasm-bindgen-futures = "0.4.12"
web-sys = { version = "0.3.39", features = ["console"] }
[workspace] [workspace]

View File

@ -1,11 +1,50 @@
use matrix_sdk::{ use matrix_sdk::{
events::room::message::{MessageEventContent, TextMessageEventContent}, api::r0::sync::sync_events::Response as SyncResponse,
events::collections::all::RoomEvent,
events::room::message::{MessageEvent, MessageEventContent, TextMessageEventContent},
identifiers::RoomId, identifiers::RoomId,
Client, ClientConfig, Client, ClientConfig, SyncSettings,
}; };
use std::convert::TryFrom;
use url::Url; use url::Url;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use web_sys::console;
struct WasmBot(Client);
impl WasmBot {
async fn on_room_message(&self, room_id: &RoomId, event: RoomEvent) {
let msg_body = if let RoomEvent::RoomMessage(MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
..
}) = event
{
msg_body.clone()
} else {
return;
};
console::log_1(&format!("Received message event {:?}", &msg_body).into());
if msg_body.starts_with("!party") {
let content = MessageEventContent::Text(TextMessageEventContent::new_plain(
"🎉🎊🥳 let's PARTY with wasm!! 🥳🎊🎉".to_string(),
));
self.0.room_send(&room_id, content, None).await.unwrap();
}
}
async fn on_sync_response(&self, response: SyncResponse) {
console::log_1(&format!("Synced").into());
for (room_id, room) in response.rooms.join {
for event in room.timeline.events {
if let Ok(event) = event.deserialize() {
self.on_room_message(&room_id, event).await
}
}
}
}
}
#[wasm_bindgen] #[wasm_bindgen]
pub async fn run() -> Result<JsValue, JsValue> { pub async fn run() -> Result<JsValue, JsValue> {
@ -18,20 +57,18 @@ pub async fn run() -> Result<JsValue, JsValue> {
let client = Client::new_with_config(homeserver_url, None, client_config).unwrap(); let client = Client::new_with_config(homeserver_url, None, client_config).unwrap();
client client
.login(username, password, None, Some("rust-sdk")) .login(username, password, None, Some("rust-sdk-wasm"))
.await .await
.unwrap(); .unwrap();
let room_id = RoomId::try_from("!KpLWMcXcHKDMfEYNqA:localhost").unwrap(); let bot = WasmBot(client.clone());
let content = MessageEventContent::Text(TextMessageEventContent { client.sync(SyncSettings::default()).await.unwrap();
body: "hello from wasm".to_string(),
format: None,
formatted_body: None,
relates_to: None,
});
client.room_send(&room_id, content, None).await.unwrap(); let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
client
.sync_forever(settings, |response| bot.on_sync_response(response))
.await;
Ok(JsValue::NULL) Ok(JsValue::NULL)
} }