Skip to content
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ docker build --build-arg TARGET_ARCH=x86_64-unknown-linux-musl -t mavlink/mavlin

### Websocket

It's also possible to connect multiple websockets with the following path `/v1/ws/mavlink`, the endpoint also accepts the query parameter `filter`, the filter value should be a regex that matches MAVLink message names, E.g: `/v1/ws/mavlink?filter=.*` for all messages, `/v1/ws/mavlink?filter=RC_.*` will match **RC_CHANNELS_RAW** and **RC_CHANNELS**.
It's also possible to connect multiple websockets with the following path `/v1/ws/mavlink`, the endpoint also accepts the query parameter `filter`, the filter value should be a regex that matches MAVLink message names, E.g: `/v1/ws/mavlink?filter=.*` for all messages, `/v1/ws/mavlink?filter=RC_.*` will match **RC_CHANNELS_RAW** and **RC_CHANNELS**. The `system_id` and `component_id` query parameters restrict the messages to a single sender, E.g: `/v1/ws/mavlink?system_id=1&component_id=1`.

You can test websockets using [websocat](https://github.com/vi/websocat):
```sh
Expand Down
10 changes: 8 additions & 2 deletions src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ pub struct Info {
pub struct WebsocketQuery {
/// Regex filter to selected the desired MAVLink messages by name
filter: Option<String>,
/// Only messages from this system id
system_id: Option<u8>,
/// Only messages from this component id
component_id: Option<u8>,
}

#[derive(Apiv2Schema, Deserialize)]
Expand Down Expand Up @@ -198,14 +202,16 @@ pub async fn websocket(
query: web::Query<WebsocketQuery>,
stream: web::Payload,
) -> Result<HttpResponse, actix_web::Error> {
let filter = match query.into_inner().filter {
let query = query.into_inner();
let filter = match query.filter {
Some(filter) => filter,
_ => ".*".to_owned(),
};

debug!("New websocket with filter {:#?}", &filter);

ws::start(WebsocketActor::new(filter), &req, stream)
let actor = WebsocketActor::new(filter, query.system_id, query.component_id);
ws::start(actor, &req, stream)
}

async fn not_found_response(message: String) -> actix_web::Result<HttpResponse> {
Expand Down
20 changes: 16 additions & 4 deletions src/websocket_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub struct WebsocketError {
pub struct WebsocketActorContent {
pub actor: Addr<WebsocketActor>,
pub re: Option<Regex>,
pub system_id: Option<u8>,
pub component_id: Option<u8>,
}

#[derive(Derivative, Default)]
Expand All @@ -36,14 +38,18 @@ pub struct WebsocketManager {
}

impl WebsocketManager {
pub fn send(&self, value: &serde_json::Value, name: &str) {
pub fn send(&self, value: &serde_json::Value, header: &mavlink::MavHeader, name: &str) {
if self.clients.is_empty() {
return;
}

let string = serde_json::to_string_pretty(value).unwrap();
for client in &self.clients {
let is_match = client.re.as_ref().map_or(false, |regx| regx.is_match(name));
let is_match = client.re.as_ref().map_or(false, |regx| regx.is_match(name))
&& client.system_id.map_or(true, |id| id == header.system_id)
&& client
.component_id
.map_or(true, |id| id == header.component_id);
if is_match {
client.actor.do_send(StringMessage(string.clone()));
}
Expand All @@ -63,20 +69,24 @@ pub fn manager() -> Arc<Mutex<WebsocketManager>> {
pub fn send(message: &MAVLinkMessage<mavlink::dialects::ardupilotmega::MavMessage>) {
let name = message.message.message_name();
let value = serde_json::to_value(message).unwrap();
MANAGER.lock().unwrap().send(&value, name);
MANAGER.lock().unwrap().send(&value, &message.header, name);
}

#[derive(Debug)]
pub struct WebsocketActor {
server: Arc<Mutex<WebsocketManager>>,
pub filter: String,
pub system_id: Option<u8>,
pub component_id: Option<u8>,
}

impl WebsocketActor {
pub fn new(message_filter: String) -> Self {
pub fn new(message_filter: String, system_id: Option<u8>, component_id: Option<u8>) -> Self {
Self {
server: MANAGER.clone(),
filter: message_filter,
system_id,
component_id,
}
}
}
Expand All @@ -103,6 +113,8 @@ impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebsocketActor {
.push(WebsocketActorContent {
actor: ctx.address(),
re: Regex::new(&self.filter).ok(),
system_id: self.system_id,
component_id: self.component_id,
});
}

Expand Down
Loading