Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/executors/web_socket_executors.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
use std::borrow::Cow;

use actix::prelude::*;
use actix::AsyncContext;
use actix_web_actors::ws;
use pyo3::prelude::*;
use pyo3::types::PyString;
use pyo3_asyncio::TaskLocals;

use crate::types::function_info::FunctionInfo;
use crate::websockets::WebSocketConnector;

pub enum WsMsgIn<'a> {
String(String),
Bytes(Cow<'a, [u8]>),
}

impl <'a>Default for WsMsgIn<'a> {
fn default() -> Self {
WsMsgIn::String(Default::default())
}
}


impl <'a>IntoPy<PyObject> for WsMsgIn<'a> {
fn into_py(self, py: Python<'_>) -> PyObject {
match self {
WsMsgIn::String(val) => val.into_py(py),
WsMsgIn::Bytes(val) => val.into_py(py),
}
}
}

fn get_function_output<'a>(
function: &'a FunctionInfo,
fn_msg: Option<String>,
fn_msg: Option<WsMsgIn>,
py: Python<'a>,
ws: &WebSocketConnector,
) -> Result<&'a PyAny, PyErr> {
Expand Down Expand Up @@ -57,9 +81,10 @@ fn get_function_output<'a>(
}
}


pub fn execute_ws_function(
function: &FunctionInfo,
text: Option<String>,
text: Option<WsMsgIn>,
task_locals: &TaskLocals,
ctx: &mut ws::WebsocketContext<WebSocketConnector>,
ws: &WebSocketConnector,
Expand Down
17 changes: 14 additions & 3 deletions src/websockets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod registry;

use crate::executors::web_socket_executors::execute_ws_function;
use crate::executors::web_socket_executors::{execute_ws_function, WsMsgIn};
use crate::types::function_info::FunctionInfo;
use crate::types::multimap::QueryParams;
use registry::{Close, SendMessageToAll, SendText};
Expand All @@ -17,6 +17,7 @@ use pyo3_asyncio::TaskLocals;
use uuid::Uuid;

use registry::{Register, WebSocketRegistry};
use std::borrow::Cow;
use std::collections::HashMap;

/// Define HTTP actor
Expand Down Expand Up @@ -90,13 +91,23 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebSocketConnecto
let function = self.router.get("message").unwrap();
execute_ws_function(
function,
Some(text.to_string()),
Some(WsMsgIn::String(text.to_string())),
&self.task_locals,
ctx,
self,
);
}
Ok(ws::Message::Binary(bin)) => {
debug!("Bin data received");
let function = self.router.get("message").unwrap();
execute_ws_function(
function,
Some(WsMsgIn::Bytes(Cow::from(bin.to_vec()))),
&self.task_locals,
ctx,
self,
);
}
Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
Ok(ws::Message::Close(_close_reason)) => {
debug!("Socket was closed");
let function = self.router.get("close").unwrap();
Expand Down
Loading