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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use js_sys::Uint8Array;
use log::{debug, error};
use rask_wasm_shared::ClientError;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{ErrorEvent, FileReaderSync, MessageEvent, WebSocket};
pub struct WebSocketAdapter {
ws: WebSocket,
}
impl WebSocketAdapter {
pub fn new(url: &str) -> Result<WebSocketAdapter, ClientError> {
debug!("Websocket enry");
let ws = WebSocket::new(url).map_err(ClientError::WebSocketError)?;
let onmessage_callback = Closure::wrap(
Box::new(WebSocketAdapter::message_callback) as Box<dyn FnMut(MessageEvent)>
);
ws.set_onmessage(Some(onmessage_callback.as_ref().unchecked_ref()));
onmessage_callback.forget();
let onerror_callback =
Closure::wrap(Box::new(WebSocketAdapter::error_callback) as Box<dyn FnMut(ErrorEvent)>);
ws.set_onerror(Some(onerror_callback.as_ref().unchecked_ref()));
onerror_callback.forget();
let cloned_ws = ws.clone();
let onopen_callback = Closure::wrap(
Box::new(move |_| WebSocketAdapter::open_callback(&cloned_ws))
as Box<dyn FnMut(JsValue)>,
);
ws.set_onopen(Some(onopen_callback.as_ref().unchecked_ref()));
onopen_callback.forget();
Ok(WebSocketAdapter { ws: ws })
}
pub fn close(&self) -> Result<(), ClientError> {
Ok(self.ws.close()?)
}
pub fn send_str(&self, message: &str) -> Result<(), ClientError> {
match self.ws.ready_state() {
1 => self.ws.send_with_str(message).map_err(|e| e.into()),
_ => Err(ClientError::WebSocketError(JsValue::from(
"Websocket is not ready",
))),
}
}
pub fn send_u8_arr(&self, message: &mut [u8]) -> Result<(), ClientError> {
let view = unsafe { Uint8Array::view(message) };
match self.ws.ready_state() {
1 => self
.ws
.send_with_array_buffer_view(&view.slice(0, message.len() as u32))
.map_err(|e| e.into()),
_ => Err(ClientError::WebSocketError(JsValue::from(
"Websocket is not ready",
))),
}
}
fn message_callback(e: MessageEvent) {
let data = e.data();
if data.is_string() {
let response = data
.as_string()
.expect("Can't convert received data to a string");
debug!("message event, received data: {:?}", response);
} else {
let blob: web_sys::Blob = data.into();
let reader = FileReaderSync::new().unwrap();
let buff = reader.read_as_array_buffer(&blob).unwrap();
let u8_arr: js_sys::Uint8Array = js_sys::Uint8Array::new(&buff);
let size = u8_arr.length();
let mut res = vec![0u8; size as usize];
u8_arr.copy_to(&mut res);
debug!("arr: {:?}", res);
}
}
fn error_callback(e: ErrorEvent) {
error!("error event: {:?}", e);
}
fn open_callback(cloned_ws: &WebSocket) {
debug!("socket opend");
match cloned_ws.send_with_str("hallo") {
Ok(_) => debug!("message delivered"),
Err(err) => error!("error sending message: {:#?}", err),
}
}
}