MCPcopy Create free account
hub / github.com/MegEngine/MegFlow / connected

Function connected

flow-rs/src/debug/server.rs:27–82  ·  view source on GitHub ↗
(ws: WebSocket, graph: String)

Source from the content-addressed store, hash-verified

25static CONNECTED: AtomicBool = AtomicBool::new(false);
26
27async fn connected(ws: WebSocket, graph: String) -> Result<()> {
28 let (mut tx, mut rx) = ws.split();
29
30 while PORT.1.try_recv().is_ok() {}
31 let init_event = serde_json::to_string(&serde_json::json!({
32 "ty": TYPE_EVENT,
33 "event": EVENT_INITIALIZED,
34 "features": FeatureCommand::registry_global().keys(),
35 "graph": graph
36 }))?;
37 tx.send(Message::text(init_event)).await?;
38
39 flow_rs::rt::task::spawn(async move {
40 while let Ok(msg) = PORT.1.recv().await {
41 if msg.ty == "__CLOSED__" {
42 tx.send(Message::text(
43 serde_json::to_string(
44 &serde_json::json!({ "ty": TYPE_EVENT, "event": EVENT_TERMINATED }),
45 )
46 .unwrap(),
47 ))
48 .await
49 .ok();
50 break;
51 }
52 if tx
53 .send(Message::text(serde_json::to_string(&msg).unwrap()))
54 .await
55 .is_err()
56 {
57 break;
58 }
59 }
60 });
61
62 while let Some(Ok(msg)) = rx.next().await {
63 if let Ok(s) = msg.to_str() {
64 let protocol: ProtocolMessage = serde_json::from_str(s)?;
65 match protocol.ty.as_str() {
66 TYPE_REQUEST => {
67 let req: RequestMessage =
68 serde_json::from_value(serde_json::Value::Object(protocol.others))?;
69 dowith_request(req).await?;
70 }
71 TYPE_EVENT => {
72 let event: EventMessage =
73 serde_json::from_value(serde_json::Value::Object(protocol.others))?;
74 dowith_event(event).await?;
75 }
76 _ => unreachable!(),
77 }
78 }
79 }
80
81 Ok(())
82}
83
84async fn reset() {

Callers 1

listenMethod · 0.85

Calls 5

dowith_requestFunction · 0.85
dowith_eventFunction · 0.85
try_recvMethod · 0.80
sendMethod · 0.45
recvMethod · 0.45

Tested by

no test coverage detected