Expect multiple SignalR messages per WebSocket message
This commit is contained in:
parent
c0e6009a00
commit
a259a685ee
@ -31,9 +31,6 @@ pub enum RecvError {
|
|||||||
#[error("Bad message type")]
|
#[error("Bad message type")]
|
||||||
BadMessageType,
|
BadMessageType,
|
||||||
|
|
||||||
#[error("Not a SignalR message: {0:?}")]
|
|
||||||
NotSignalRMessage(String),
|
|
||||||
|
|
||||||
#[error("Invalid json: {0}")]
|
#[error("Invalid json: {0}")]
|
||||||
InvalidJson(#[from] serde_json::Error),
|
InvalidJson(#[from] serde_json::Error),
|
||||||
|
|
||||||
@ -41,7 +38,9 @@ pub enum RecvError {
|
|||||||
TungsteniteError(#[from] tungstenite::Error),
|
TungsteniteError(#[from] tungstenite::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Stream { sock: WebSocket<MaybeTlsStream<TcpStream>> }
|
pub struct Stream {
|
||||||
|
sock: WebSocket<MaybeTlsStream<TcpStream>>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Stream {
|
impl Stream {
|
||||||
pub fn open(ctx: &mut Context) -> Result<Stream, NegotiateError> {
|
pub fn open(ctx: &mut Context) -> Result<Stream, NegotiateError> {
|
||||||
@ -88,15 +87,15 @@ impl Stream {
|
|||||||
self.sock.send(Message::Text(msg))
|
self.sock.send(Message::Text(msg))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn recv(&mut self) -> Result<serde_json::Value, RecvError> {
|
pub fn recv(&mut self) -> Result<Vec<serde_json::Value>, RecvError> {
|
||||||
let msg = self.sock.read()?;
|
let msg = self.sock.read()?;
|
||||||
let Message::Text(txt) = msg else { return Err(RecvError::BadMessageType) };
|
let Message::Text(txt) = msg else { return Err(RecvError::BadMessageType) };
|
||||||
let json: &str = match txt.strip_suffix("\x1E") {
|
|
||||||
None => return Err(RecvError::NotSignalRMessage(txt)),
|
let msgs = txt.split_terminator('\x1E')
|
||||||
Some(json) => json
|
.filter_map(|s| serde_json::from_str(s).ok())
|
||||||
};
|
.collect();
|
||||||
dbg!(&json);
|
|
||||||
Ok(serde_json::from_str(json)?)
|
Ok(msgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe(&mut self, id: &str) -> Result<(), tungstenite::Error> {
|
pub fn subscribe(&mut self, id: &str) -> Result<(), tungstenite::Error> {
|
||||||
|
Loading…
Reference in New Issue
Block a user