swim rust
1.0.0
Swim Rust SDK包含用于构建可以通过多路复用流API进行交互的状态应用程序的软件框架。它建立在Tokio异步运行时的顶部,任何游泳应用都需要Tokio运行时。
每个应用程序都由一定数量的状态代理组成,每个代理都作为单独的Tokio任务运行,并且可以由URI单独解决。代理商可能具有公共状态和私人状态,可以仅在内存中保存,也可以选择在持续存储中。代理商的公共状态由许多车道组成,类似于记录中的字段。有多种车道,例如,其中包含单个值的车道和包含键值对图的泳道。
可以通过建立指向它的链接(来自另一个代理实例或专门的客户端)来观察任何车道的状态。已建立的链接将将所有更新向该车道的状态推向订户,还将允许订户向州请求更改(对于支持此局的车道类型)。链接通过Web插座连接操作并具有多路复用,这意味着指向同一主机上多个车道的链接可以共享单个Web插座连接。
网站|开发人员指南|服务器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的更新发送给代理商,则将使用ExampleLifecycle启动Lane lane的URI /examples/name , ExampleAgent的实例将由服务器启动。然后,车道的值将设置为5 ,以下将在控制台上打印:
Received value: 5 for 'lane' on agent at URI: /examples/name.
example_apps目录中提供了许多示例应用程序,这些应用程序演示了单个功能以及更全面的应用程序。
请参阅《开发指南》。
该项目是根据Apache 2.0许可证获得许可的。