Swim Rust SDK มีกรอบซอฟต์แวร์สำหรับการสร้างแอพพลิเคชั่นสถานะที่สามารถโต้ตอบกับ API แบบมัลติเพล็กซ์ มันถูกสร้างขึ้นที่ด้านบนของรันไทม์ tokio assynchronous และต้องใช้เวลาใช้งาน Tokio สำหรับแอปพลิเคชันว่ายน้ำใด ๆ
แต่ละแอปพลิเคชันประกอบด้วยตัวแทนของรัฐจำนวนหนึ่งซึ่งแต่ละรายการทำงานเป็นงาน Tokio แยกต่างหากและสามารถแก้ไขได้โดย URI ตัวแทนอาจมีทั้งรัฐและรัฐส่วนตัวซึ่งสามารถจัดขึ้นในหน่วยความจำหรือทางเลือกในการจัดเก็บถาวร สถานะสาธารณะของตัวแทนประกอบด้วยช่องทางจำนวนหนึ่งซึ่งคล้ายคลึงกับสนามในบันทึก มีหลายประเภทของเลนที่เช่นเลนที่มีค่าเดียวและที่มีแผนที่ของคู่คีย์-ค่า
สถานะของเลนใด ๆ สามารถสังเกตได้โดยการสร้างลิงก์ไปยัง (จากอินสแตนซ์ตัวแทนอื่นหรือไคลเอนต์เฉพาะ) ลิงค์ที่กำหนดจะผลักดันการอัปเดตทั้งหมดไปยังสถานะของเลนนั้นไปยังสมาชิกและจะอนุญาตให้ผู้สมัครสมาชิกร้องขอการเปลี่ยนแปลงสถานะ (สำหรับประเภทเลนที่สนับสนุนสิ่งนี้) ลิงก์ทำงานผ่านการเชื่อมต่อเว็บซ็อกเก็ตและมีมัลติเพล็กซ์ซึ่งหมายความว่าลิงก์ไปยังหลายเลนบนโฮสต์เดียวกันสามารถแชร์การเชื่อมต่อเว็บส็อกเก็ตเดียว
เว็บไซต์ | คู่มือนักพัฒนา Server API เอกสาร | เอกสาร API ของลูกค้า
การใช้ตัวแทนว่ายน้ำในสนิม
การสร้างแอปพลิเคชันเซิร์ฟเวอร์ว่ายน้ำ
เอกสารอ้างอิง
แอปพลิเคชันตัวอย่างต่อไปนี้เรียกใช้เซิร์ฟเวอร์ว่ายน้ำที่โฮสต์เส้นทางตัวแทนเดียวที่แต่ละอินสแตนซ์เอเจนต์มีเลนเดี่ยวเรียกว่า lane ทุกครั้งที่มีการเปลี่ยนแปลงไปที่เลนจะถูกพิมพ์ลงบนคอนโซลโดยเซิร์ฟเวอร์
[ dependencies ]
swimos = { version = " 0.1.1 " , features = [ " server " , " agent " ] } use swimos :: {
agent :: {
agent_lifecycle :: HandlerContext ,
agent_model :: AgentModel ,
event_handler :: { EventHandler , HandlerActionExt } ,
lanes :: ValueLane ,
lifecycle , AgentLaneModel ,
} ,
route :: RoutePattern ,
server :: { until_termination , Server , ServerBuilder } ,
} ;
# [ tokio :: main ]
pub async fn main ( ) -> Result < ( ) , Box < dyn std :: error :: Error > > {
// An agent route consists of the agent definition and a lifecycle.
let model = AgentModel :: new ( ExampleAgent :: default , ExampleLifecycle . into_lifecycle ( ) ) ;
let server = ServerBuilder :: with_plane_name ( "Example Plane" )
. set_bind_addr ( "127.0.0.1:8080" . parse ( ) ? ) // Bind the server to this address.
. add_route ( RoutePattern :: parse_str ( "/examples/{id}" ) ? , model ) // Register the agent we have defined.
. build ( )
. await ? ;
// Run the server until we terminate it with Ctrl-C.
let ( task , handle ) = server . run ( ) ;
let ( ctrl_c_result , server_result ) = tokio :: join! ( until_termination ( handle , None ) , task ) ;
ctrl_c_result? ;
server_result? ;
Ok ( ( ) )
}
// Deriving the `AgentLaneModel` trait makes this type into an agent.
# [ derive ( AgentLaneModel ) ]
struct ExampleAgent {
lane : ValueLane < i32 > ,
}
// Any agent type can have any number of lifecycles defined for it. A lifecycle describes
// how the agent will react to events that occur as it executes.
# [ derive ( Default , Clone , Copy ) ]
struct ExampleLifecycle ;
// The `lifecycle` macro creates an method called `into_lifecycle` for the type, using the
// annotated event handlers methods in the block.
# [ lifecycle ( ExampleAgent ) ]
impl ExampleLifecycle {
# [ on_event ( lane ) ]
fn lane_event (
& self ,
context : HandlerContext < ExampleAgent > ,
value : & i32 ,
) -> impl EventHandler < ExampleAgent > {
let n = * value ;
context . get_agent_uri ( ) . and_then ( move |uri| {
context . effect ( move || {
println ! ( "Received value: {} for 'lane' on agent at URI: {}." , n , uri ) ;
} )
} )
}
} ตัวอย่างเช่นหากไคลเอนต์ว่ายน้ำส่งการอัปเดตด้วยค่า 5 ไปยังตัวแทนที่ URI /examples/name สำหรับ lane อินสแตนซ์ของ ExampleAgent โดยใช้ ExampleLifecycle จะเริ่มต้นโดยเซิร์ฟเวอร์ ค่าของเลนจะถูกตั้งค่าเป็น 5 และจะพิมพ์ต่อไปนี้บนคอนโซล:
Received value: 5 for 'lane' on agent at URI: /examples/name.
แอปพลิเคชันตัวอย่างจำนวนมากมีอยู่ในไดเรกทอรีตัวอย่าง _apps ซึ่งแสดงให้เห็นถึงคุณสมบัติส่วนบุคคลรวมถึงแอปพลิเคชันที่ครอบคลุมมากขึ้น
ดูคู่มือการพัฒนา
โครงการนี้ได้รับใบอนุญาตภายใต้ใบอนุญาต Apache 2.0