msg.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use bytes::Bytes;
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use std::convert::TryInto;
  4. use tokio_tungstenite::tungstenite::Message as TokioMessage;
  5. #[derive(ProtoBuf, Debug, Clone, Default)]
  6. pub struct WebSocketRawMessage {
  7. #[pb(index = 1)]
  8. pub channel: WSChannel,
  9. #[pb(index = 2)]
  10. pub data: Vec<u8>,
  11. }
  12. #[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Hash)]
  13. pub enum WSChannel {
  14. Document = 0,
  15. Folder = 1,
  16. }
  17. impl std::default::Default for WSChannel {
  18. fn default() -> Self {
  19. WSChannel::Document
  20. }
  21. }
  22. impl ToString for WSChannel {
  23. fn to_string(&self) -> String {
  24. match self {
  25. WSChannel::Document => "0".to_string(),
  26. WSChannel::Folder => "1".to_string(),
  27. }
  28. }
  29. }
  30. impl std::convert::From<WebSocketRawMessage> for TokioMessage {
  31. fn from(msg: WebSocketRawMessage) -> Self {
  32. let result: Result<Bytes, ::protobuf::ProtobufError> = msg.try_into();
  33. match result {
  34. Ok(bytes) => TokioMessage::Binary(bytes.to_vec()),
  35. Err(e) => {
  36. log::error!("WsMessage serialize error: {:?}", e);
  37. TokioMessage::Binary(vec![])
  38. }
  39. }
  40. }
  41. }