usimp websocket things
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use crate::error::*;
|
||||
use crate::http;
|
||||
|
||||
use crate::usimp;
|
||||
use crate::websocket::*;
|
||||
use base64;
|
||||
use crypto;
|
||||
@ -12,6 +13,7 @@ pub fn recv_message(client: &mut http::HttpStream) -> Result<Message, Error> {
|
||||
loop {
|
||||
let header = FrameHeader::from(&mut client.stream)?;
|
||||
|
||||
// FIXME control frames may show up in a fragmented stream
|
||||
if msg_type != 0 && header.opcode != 0 {
|
||||
return Err(Error::new(Kind::WebSocketError, Class::ClientError)
|
||||
.set_desc("continuation frame expected".to_string()));
|
||||
@ -157,6 +159,10 @@ pub fn handshake(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn error_handler(error: Error) {
|
||||
// TODO send error response frame
|
||||
}
|
||||
|
||||
pub fn connection_handler(
|
||||
client: &mut http::HttpStream,
|
||||
req: &http::Request,
|
||||
@ -167,13 +173,17 @@ pub fn connection_handler(
|
||||
}
|
||||
|
||||
loop {
|
||||
let msg = recv_message(client).unwrap();
|
||||
match recv_message(client) {
|
||||
Ok(msg) => {
|
||||
match msg {
|
||||
Message::TextMessage(msg) => {
|
||||
println!("Data: {}", msg.data);
|
||||
// TODO threads?
|
||||
let req: RequestEnvelope = serde_json::from_str(msg.data.as_str()).unwrap();
|
||||
println!("Endpoint: {}, ReqNo: {}, Data: {}", req.endpoint, req.request_nr, req.data);
|
||||
let a = usimp::endpoint(req.endpoint.as_str(), req.data).unwrap();
|
||||
},
|
||||
Message::CloseMessage(msg) => {
|
||||
|
||||
println!("Received close frame: {}: {}", msg.code.unwrap_or(0), msg.reason.unwrap_or("-".to_string()));
|
||||
return
|
||||
}
|
||||
Message::PingMessage(msg) => {
|
||||
@ -183,5 +193,10 @@ pub fn connection_handler(
|
||||
// TODO something
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
error_handler(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
mod handler;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::error::Error;
|
||||
use crate::http;
|
||||
pub use handler::*;
|
||||
@ -45,6 +46,21 @@ pub struct TextMessage {
|
||||
data: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct RequestEnvelope {
|
||||
endpoint: String,
|
||||
request_nr: u64,
|
||||
data: serde_json::Value,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct ResponseEnvelope {
|
||||
to_request_nr: u64,
|
||||
status: String,
|
||||
message: Option<String>,
|
||||
data: serde_json::Value,
|
||||
}
|
||||
|
||||
impl FrameHeader {
|
||||
pub fn from(socket: &mut http::Stream) -> Result<Self, Error> {
|
||||
let mut data = [0u8; 2];
|
||||
|
Reference in New Issue
Block a user