msg.rs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 module: WSModule,
  9. #[pb(index = 2)]
  10. pub data: Vec<u8>,
  11. }
  12. #[derive(ProtoBuf_Enum, Debug, Clone, Eq, PartialEq, Hash)]
  13. pub enum WSModule {
  14. Doc = 0,
  15. }
  16. impl std::default::Default for WSModule {
  17. fn default() -> Self { WSModule::Doc }
  18. }
  19. impl ToString for WSModule {
  20. fn to_string(&self) -> String {
  21. match self {
  22. WSModule::Doc => "0".to_string(),
  23. }
  24. }
  25. }
  26. impl std::convert::From<WebSocketRawMessage> for TokioMessage {
  27. fn from(msg: WebSocketRawMessage) -> Self {
  28. let result: Result<Bytes, ::protobuf::ProtobufError> = msg.try_into();
  29. match result {
  30. Ok(bytes) => TokioMessage::Binary(bytes.to_vec()),
  31. Err(e) => {
  32. log::error!("WsMessage serialize error: {:?}", e);
  33. TokioMessage::Binary(vec![])
  34. },
  35. }
  36. }
  37. }