~starkingdoms/starkingdoms

ref: 494c0b1ca0e58be47c24cf6fd937b57dea1cd1f5 starkingdoms/crates/client/src/networking/ws_wasm.rs -rw-r--r-- 2.8 KiB
494c0b1c — ghostly_zsh player and parts rendering, but jk it doesnt work 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use bevy_ecs::system::Resource;
//use crossbeam::channel::{unbounded, Receiver, Sender};
use futures::{channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender}, SinkExt};
use wasm_bindgen::{prelude::Closure, JsCast, JsValue};
use wasm_bindgen_futures::spawn_local;
use web_sys::{MessageEvent, WebSocket};
use starkingdoms_common::packet::Packet;

use crate::ecs::RecvPacket;

const PORT: u16 = 3000;

#[derive(Debug)]
pub struct Socket(WebSocket);
unsafe impl Send for Socket {}
unsafe impl Sync for Socket {}

#[derive(Resource, Debug)]
pub struct Ws {
    socket: Socket,
    pub sender: UnboundedSender<Packet>,
    pub receiver: UnboundedReceiver<Packet>,
    packet_receiver: UnboundedReceiver<Packet>,
}

impl Ws {
    pub fn new() -> Self {
        let window = web_sys::window().unwrap();
        let ws = WebSocket::new(&format!("ws://{}:{}",
                window.location().hostname().unwrap(), PORT)).expect("Couldn't connect to server");
        let (packet_sender, receiver) = unbounded();
        //let packet_sender = Rc::new(RwLock::new(packet_sender));
        let (sender, packet_receiver) = unbounded();

        /*let onopen_callback = Closure::<dyn FnMut()>::new(move || {

        });
        ws.set_onopen(Some(onopen_callback.as_ref().unchecked_ref()));
        onopen_callback.forget();*/
        let onmessage_callback = Closure::<dyn FnMut(_)>::new(move |e: MessageEvent| {
            //tracing::error!("{}", ws.ready_state());
            let data = e.data().as_string().expect("Expected string, found some other type");
            let data: Packet = serde_json::from_str(&data).expect("Received invalid json from server");
            let mut sender_clone = packet_sender.clone();
            spawn_local(async move {
                sender_clone.send(data).await.expect("Couldn't transmit packet to client");
            });
        });
        ws.set_onmessage(Some(onmessage_callback.as_ref().unchecked_ref()));
        onmessage_callback.forget();
        Ws {
            socket: Socket(ws),
            sender,
            receiver,
            packet_receiver,
        }
    }
    pub fn send_all_packets_from_channel(&mut self) {
        //for packet in self.packet_receiver.iter() {
        while let Ok(Some(packet)) = self.packet_receiver.try_next() {
            self.socket.0.send_with_str(&serde_json::to_string(&packet).expect("Couldn't convert packet to json")).expect("Couldn't send packet to server");
        }
    }
    pub fn send_packet(&mut self, packet: Packet) {
        let socket = self.socket.0.clone();
        spawn_local(async move {
            while socket.ready_state() != 1 {}
            socket.send_with_str(&serde_json::to_string(&packet).expect("Couldn't convert packet to json")).expect("Couldn't send packet to server");
        });
    }
}