matrix-sdk: Fix the WASM example

master
Damir Jelić 2021-03-22 20:24:30 +01:00
parent bbe812f1d9
commit 12bf0f53a8
4 changed files with 31 additions and 19 deletions

View File

@ -10,15 +10,15 @@ edition = "2018"
crate-type = ["cdylib"] crate-type = ["cdylib"]
[dependencies] [dependencies]
url = "2.1.1" url = "2.2.1"
wasm-bindgen = { version = "0.2.62", features = ["serde-serialize"] } wasm-bindgen = { version = "0.2.72", features = ["serde-serialize"] }
wasm-bindgen-futures = "0.4.12" wasm-bindgen-futures = "0.4.22"
console_error_panic_hook = "*" console_error_panic_hook = "0.1.6"
web-sys = { version = "0.3.39", features = ["console"] } web-sys = { version = "0.3.49", features = ["console"] }
[dependencies.matrix-sdk] [dependencies.matrix-sdk]
path = "../.." path = "../.."
default-features = false default-features = false
features = ["native-tls"] features = ["native-tls", "encryption"]
[workspace] [workspace]

View File

@ -7,6 +7,4 @@ You can build the example locally with:
and then visiting http://localhost:8080 in a browser should run the example! and then visiting http://localhost:8080 in a browser should run the example!
Note: Encryption isn't supported yet This example is loosely based off of [this example](https://github.com/seanmonstar/reqwest/tree/master/examples/wasm_github_fetch), an example usage of `fetch` from `wasm-bindgen`.
This example is loosely based off of [this example](https://github.com/seanmonstar/reqwest/tree/master/examples/wasm_github_fetch), an example usage of `fetch` from `wasm-bindgen`.

View File

@ -5,8 +5,8 @@
}, },
"devDependencies": { "devDependencies": {
"@wasm-tool/wasm-pack-plugin": "1.0.1", "@wasm-tool/wasm-pack-plugin": "1.0.1",
"text-encoding": "^0.7.0",
"html-webpack-plugin": "^3.2.0", "html-webpack-plugin": "^3.2.0",
"text-encoding": "^0.7.0",
"webpack": "^4.29.4", "webpack": "^4.29.4",
"webpack-cli": "^3.1.1", "webpack-cli": "^3.1.1",
"webpack-dev-server": "^3.1.0" "webpack-dev-server": "^3.1.0"

View File

@ -1,7 +1,7 @@
use matrix_sdk::{ use matrix_sdk::{
deserialized_responses::SyncResponse, deserialized_responses::SyncResponse,
events::{ events::{
room::message::{MessageEventContent, TextMessageEventContent}, room::message::{MessageEventContent, MessageType, TextMessageEventContent},
AnyMessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, SyncMessageEvent, AnyMessageEventContent, AnySyncMessageEvent, AnySyncRoomEvent, SyncMessageEvent,
}, },
identifiers::RoomId, identifiers::RoomId,
@ -17,35 +17,49 @@ impl WasmBot {
async fn on_room_message( async fn on_room_message(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
event: SyncMessageEvent<MessageEventContent>, event: &SyncMessageEvent<MessageEventContent>,
) { ) {
let msg_body = if let SyncMessageEvent { let msg_body = if let SyncMessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), content:
MessageEventContent {
msgtype: MessageType::Text(TextMessageEventContent { body: msg_body, .. }),
..
},
.. ..
} = event } = event
{ {
msg_body.clone() msg_body
} else { } else {
return; return;
}; };
console::log_1(&format!("Received message event {:?}", &msg_body).into()); console::log_1(&format!("Received message event {:?}", &msg_body).into());
if msg_body.starts_with("!party") { if msg_body.contains("!party") {
let content = AnyMessageEventContent::RoomMessage(MessageEventContent::Text( let content = AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(
TextMessageEventContent::plain("🎉🎊🥳 let's PARTY with wasm!! 🥳🎊🎉".to_string()), "🎉🎊🥳 let's PARTY!! 🥳🎊🎉",
)); ));
self.0.room_send(&room_id, content, None).await.unwrap(); println!("sending");
self.0
// send our message to the room we found the "!party" command in
// the last parameter is an optional Uuid which we don't care about.
.room_send(room_id, content, None)
.await
.unwrap();
println!("message sent");
} }
} }
async fn on_sync_response(&self, response: SyncResponse) -> LoopCtrl { async fn on_sync_response(&self, response: SyncResponse) -> LoopCtrl {
console::log_1(&"Synced".to_string().into()); console::log_1(&"Synced".to_string().into());
for (room_id, room) in response.rooms.join { for (room_id, room) in response.rooms.join {
for event in room.timeline.events { for event in room.timeline.events {
if let AnySyncRoomEvent::Message(AnySyncMessageEvent::RoomMessage(ev)) = event { if let AnySyncRoomEvent::Message(AnySyncMessageEvent::RoomMessage(ev)) = event {
self.on_room_message(&room_id, ev).await self.on_room_message(&room_id, &ev).await
} }
} }
} }