Skip to main content

WebSocketConfig

Struct WebSocketConfig 

Source
pub struct WebSocketConfig {
    pub ws_endpoint: String,
    pub access_token: Option<String>,
}
Expand description

WebSocket的配置项

Fields§

§ws_endpoint: String

服务端的WebSocket 接入点 e.g. ws://127.0.0.1:3000

§access_token: Option<String>

可选的访问令牌,用于认证。

Implementations§

Source§

impl WebSocketConfig

Source

pub fn new(ws_endpoint: String, access_token: Option<String>) -> Self

创建一个新的 WebSocketCofig 实例。

§参数
  • ws_endpoint: 服务端的WebSocket 接入点 e.g. ws://127.0.0.1:3000
  • access_token: 可选的访问令牌,用于认证。
§返回

成功则返回 Result<Self>,其中 Self 是新创建的 WebSocket 实例。 如果URL解析失败或协议不受支持,则返回错误。

Examples found in repository?
examples/basic_usage.rs (line 28)
18async fn main() -> Result<()> {
19    logger::init_logger(Some(LevelFilter::Info)); // 初始化日志
20
21    // 创建事件通道
22    let (event_tx, mut event_rx) = mpsc::channel::<Event>(100);
23
24    // 初始化 MilkyClient
25    // 示例中使用的是WebSocket的通信方式,如果你想通过WebHook方式与服务端通信,可以参考下面的代码
26    // let wh_config = WebHookConfig::new(None, 8080, "http://127.0.0.1:3000".to_string(), None);
27    // let client = MilkyClient::new(Communication::WebHook(wh_config), event_tx)?;
28    let ws_config = WebSocketConfig::new("ws://127.0.0.1:3002".to_string(), None);
29    let client = MilkyClient::new(Communication::WebSocket(ws_config), event_tx)?;
30    let client = Arc::new(client);
31
32    // 连接到件流
33    if let Err(e) = client.connect_events().await {
34        error!("未能连接到事件流: {e:?}");
35        return Err(e);
36    }
37    info!("成功连接到 Milky 服务器事件流。");
38
39    // 启动一个异步任务来处理接收到的事件
40    let client_for_task = Arc::clone(&client);
41    let _event_handle = tokio::spawn(async move {
42        info!("事件监听器已启动。");
43        while let Some(event) = event_rx.recv().await {
44            info!("收到事件: {event:?}",); // 打印原始事件
45
46            match event.kind {
47                EventKind::MessageReceive {
48                    message: message_event,
49                } => {
50                    match message_event {
51                        MessageEvent::Friend(friend_msg) => {
52                            let plain_text = get_plain_text_from_segments(&friend_msg.message.segments);
53                            info!(
54                                "收到好友消息: {} (QQ: {}) - {}",
55                                friend_msg.friend.remark,
56                                friend_msg.message.sender_id,
57                                plain_text
58                            );
59
60                            // 示例:复读
61                            if plain_text.starts_with("/echo") {
62                                let reply_segments =
63                                    vec![text_segment(plain_text.replace("/echo", "").trim())];
64                                match client_for_task
65                                    .send_private_message(friend_msg.message.sender_id, reply_segments)
66                                    .await
67                                {
68                                    Ok(resp) => info!("自动回复成功: seq={}", resp.message_seq),
69                                    Err(e) => error!("自动回复失败: {e:?}",),
70                                }
71                            }
72                        }
73                        MessageEvent::Group(group_msg) => {
74                            let plain_text = get_plain_text_from_segments(&group_msg.message.segments);
75                            info!(
76                                "收到群消息: [{}] {} (QQ: {}) - {}",
77                                group_msg.group.group_name,
78                                group_msg.group_member.card,
79                                group_msg.message.sender_id,
80                                plain_text
81                            );
82                        }
83                        MessageEvent::Temp(temp_msg) => {
84                            let plain_text = get_plain_text_from_segments(&temp_msg.message.segments);
85                            info!(
86                                "收到临时消息: QQ: {} - {}",
87                                temp_msg.message.sender_id,
88                                plain_text
89                            );
90                        }
91                    }
92                }
93                EventKind::GroupMemberIncrease {
94                    group_id, user_id, ..
95                } => {
96                    info!("群 {} 新成员加入: {}", group_id, user_id);
97                }
98                // ... 处理其他事件类型
99                _ => {}
100            }
101        }
102        info!("事件监听器已停止。");
103    });
104
105    // 等待连接稳定和事件监听器启动
106    tokio::time::sleep(std::time::Duration::from_secs(2)).await;
107
108    // 调用 API 示例
109    // 获取登录信息
110    match client.get_login_info().await {
111        Ok(login_info) => {
112            info!(
113                "登录信息: QQ={}, 昵称='{}'",
114                login_info.uin, login_info.nickname
115            );
116        }
117        Err(e) => {
118            error!("未能获取登录信息: {e:?}",);
119        }
120    }
121
122    // 发送私聊消息 (请替换为有效的 user_id)
123    let user_id_to_send: i64 = 123456789; // 示例QQ号
124    let message_to_send = vec![text_segment("你好,这是一个来自 Vivian SDK 的测试消息!")];
125    match client
126        .send_private_message(user_id_to_send, message_to_send)
127        .await
128    {
129        Ok(response) => {
130            info!(
131                "私聊消息成功发送至 {}: message_seq={}",
132                user_id_to_send, response.message_seq
133            );
134        }
135        Err(e) => {
136            error!("未能发送私聊消息至 {user_id_to_send}: {e:?}");
137        }
138    }
139
140    // 保持主程序运行以处理事件。
141    info!("示例正在运行。按 Ctrl-C 退出。");
142    tokio::signal::ctrl_c().await?; // 等待 Ctrl-C信号
143    info!("收到 Ctrl-C,正在关闭...");
144    client.shutdown().await;
145    // 可以选择性地等待一小段时间,以确保关闭消息被处理
146    tokio::time::sleep(std::time::Duration::from_millis(250)).await;
147
148    Ok(())
149}

Trait Implementations§

Source§

impl Clone for WebSocketConfig

Source§

fn clone(&self) -> WebSocketConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,