Commit fa206dac by qlintonger xeno

first-st

parents
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="EMPTY_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="mu-outisde" uuid="4bdb4fa9-27e9-44f3-bd86-f006b6092212">
<driver-ref>mysql.8</driver-ref>
<synchronize>true</synchronize>
<remarks>东航外网数据库</remarks>
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
<jdbc-url>jdbc:mysql://124.71.148.16:3306</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
</data-source>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/WTF-WS.iml" filepath="$PROJECT_DIR$/.idea/WTF-WS.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
This diff is collapsed. Click to expand it.
[package]
name = "WTF-WS"
version = "0.1.0"
edition = "2021"
[dependencies]
actix-web = "4"
actix-ws = "0.3.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread", "macros"] }
tokio-tungstenite = "0.26.1"
tungstenite = "0.26.1"
futures = "0.3.31"
url = "2.5.4"
log = "0.4.22"
env_logger = "0.10.0"
\ No newline at end of file
use futures::{SinkExt, StreamExt};
use tokio_tungstenite::accept_hdr_async;
use tungstenite::handshake::server::{Request, Response};
use tungstenite::{Error, Message};
use crate::utils;
pub(crate) async fn handle_client(stream: tokio::net::TcpStream) -> Result<(), Error> {
let must_existed_params = vec!["deviceId", "fromId", "wsPwd"];
let mut ws_stream = accept_hdr_async(stream, |req: &Request, resp: Response| {
println!("新客户端连接: {}", req.uri());
let connection_params = match utils::get_connection_params(req.uri().to_string()) {
Ok(p) => p,
Err(e) => {
println!("缺少重要连接数据段: {}", e);
return Err(Error::ConnectionClosed);
}
};
let not_existed = must_existed_params.find(|param| !connection_params.contains_key(param));
if not_existed != None {
println!("缺少重要连接数据段: {}", not_existed);
return Err(Error::ConnectionClosed);
}
Ok(resp)
})
.await
.expect("WebSocket handshake error");
while let Some(msg) = ws_stream.next().await {
let msg = msg?;
if msg.is_text() {
println!("Client message: {}", msg.to_text()?);
ws_stream
.send(Message::text(format!("Your message is {}", msg)))
.await?;
}
}
println!("Client finished");
Ok(())
}
mod client;
mod utils;
use client::handle_client;
use tokio::net::TcpListener;
#[tokio::main]
async fn main() {
let addr = "127.0.0.1:8080";
let listener = TcpListener::bind(addr).await.unwrap();
while let Ok((stream, _)) = listener.accept().await {
tokio::spawn(handle_client(stream));
}
}
use std::collections::HashMap;
pub(crate) fn get_connection_params(connection_url: String) -> Result<HashMap<String, String>, &'static str> {
if let Some(query_part) = connection_url.split('?').nth(1) {
let mut params_mapping = HashMap::new();
for param in query_part.split('&') {
if let Some((key, value)) = param.split_once('=') {
params_mapping.insert(key.to_string(), value.to_string());
} else {
return Err("Invalid parameter format, missing '='");
}
}
Ok(params_mapping)
} else {
Ok(HashMap::new())
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment