flowlang/flowlang/http/
websocket_write.rs

1use ndata::dataobject::*;
2use crate::flowlang::http::listen::*;
3use std::io::Write;
4
5pub fn execute(o: DataObject) -> DataObject {
6let a0 = o.get_int("stream_id");
7let a1 = o.get_string("msg");
8let ax = websocket_write(a0, a1);
9let mut o = DataObject::new();
10o.put_int("a", ax);
11o
12}
13
14pub fn websocket_write(stream_id:i64, msg:String) -> i64 {
15let msg = msg.as_bytes();
16
17let n = msg.len() as i64;
18let mut reply: Vec<u8> = Vec::new();
19
20reply.push(129); // Text = 129 / Binary = 130;
21
22if n < 126 {
23  reply.push((n & 0xFF) as u8);
24}
25else if n < 65536 {
26  reply.push(126);
27  reply.push(((n >> 8) & 0xFF) as u8);
28  reply.push((n & 0xFF) as u8);
29}
30else {
31  reply.push(127);
32  reply.push(((n >> 56) & 0xFF) as u8);
33  reply.push(((n >> 48) & 0xFF) as u8);
34  reply.push(((n >> 40) & 0xFF) as u8);
35  reply.push(((n >> 32) & 0xFF) as u8);
36  reply.push(((n >> 24) & 0xFF) as u8);
37  reply.push(((n >> 16) & 0xFF) as u8);
38  reply.push(((n >> 8) & 0xFF) as u8);
39  reply.push((n & 0xFF) as u8);
40}
41
42reply.extend_from_slice(msg);
43
44let heap = &mut WEBSOCKS.write().unwrap();
45let heap = heap.as_mut().unwrap();
46let sock = &mut heap.get(stream_id as usize);
47let _ = sock.0.write(&reply).unwrap();
48
49n as i64
50
51}
52