// src/lib.rs
use wasm_bindgen::prelude::*;
use web_sys::{window, Document, Element};
use worker::*;
use js_sys::JSON;
use serde_json::*;
use serde::Deserialize;
pub struct User{
name: String,
}
pub struct Message{
msg: String,
}
#[durable_object]
pub struct Chatroom {
users: Vec<User>,
messages: Vec<Message>,
state: State,
env: Env,
}
#[durable_object]
impl DurableObject for Chatroom {
fn new(state: State, env: Env) -> Self {
Self {
users: vec![],
messages: vec![],
state: state,
env,
}
}
async fn fetch(&mut self, _req: Request) -> Result<Response> {
// do some work when a worker makes a request to this DO
Response::ok(&format!("{} active users.", self.users.len()))
}
async fn websocket_message(&mut self, ws: WebSocket, message: WebSocketIncomingMessage) -> Result<()> {
match message {
WebSocketIncomingMessage::Binary(_) => {
ws.send_with_str("Durable Object received binary message")?;
}
WebSocketIncomingMessage::String(text) => {
if let Ok(send_message) = serde_json::from_str::<SendMessage>(&text) {
let result = self.handle(&send_message).await;
let response = serde_json::to_string(&result).unwrap_or_else(|_| "{}".to_string());
_ = ws.send_with_str(&response);
} else {
ws.send_with_str(&format!("Durable Object received text message: {text}"))?;
}
}
}
Ok(())
}
async fn websocket_close(&mut self, ws: web_sys::WebSocket, code: usize, reason: String, was_clean: bool) -> Result<()> {
ws.close_with_code_and_reason(code as u16, &format!("Durable Object is closing WebSocket {reason:?} clean: {was_clean:?}"))?;
Ok(())
}
// src/lib.rs
use wasm_bindgen::prelude::*;
use web_sys::{window, Document, Element};
use worker::*;
use js_sys::JSON;
use serde_json::*;
use serde::Deserialize;
pub struct User{
name: String,
}
pub struct Message{
msg: String,
}
#[durable_object]
pub struct Chatroom {
users: Vec<User>,
messages: Vec<Message>,
state: State,
env: Env,
}
#[durable_object]
impl DurableObject for Chatroom {
fn new(state: State, env: Env) -> Self {
Self {
users: vec![],
messages: vec![],
state: state,
env,
}
}
async fn fetch(&mut self, _req: Request) -> Result<Response> {
// do some work when a worker makes a request to this DO
Response::ok(&format!("{} active users.", self.users.len()))
}
async fn websocket_message(&mut self, ws: WebSocket, message: WebSocketIncomingMessage) -> Result<()> {
match message {
WebSocketIncomingMessage::Binary(_) => {
ws.send_with_str("Durable Object received binary message")?;
}
WebSocketIncomingMessage::String(text) => {
if let Ok(send_message) = serde_json::from_str::<SendMessage>(&text) {
let result = self.handle(&send_message).await;
let response = serde_json::to_string(&result).unwrap_or_else(|_| "{}".to_string());
_ = ws.send_with_str(&response);
} else {
ws.send_with_str(&format!("Durable Object received text message: {text}"))?;
}
}
}
Ok(())
}
async fn websocket_close(&mut self, ws: web_sys::WebSocket, code: usize, reason: String, was_clean: bool) -> Result<()> {
ws.close_with_code_and_reason(code as u16, &format!("Durable Object is closing WebSocket {reason:?} clean: {was_clean:?}"))?;
Ok(())
}