Swim Rust SDK에는 다중 스트리밍 API를 통해 상호 작용할 수있는 상태의 응용 프로그램을 구축하기위한 소프트웨어 프레임 워크가 포함되어 있습니다. Tokio 비동기 런타임 위에 구축되었으며 모든 수영 응용 프로그램에는 Tokio 런타임이 필요합니다.
각 응용 프로그램은 일부 수의 상태 에이전트로 구성되며 각 에이전트는 별도의 Tokio 작업으로 실행되며 URI에 의해 개별적으로 해결 될 수 있습니다. 에이전트는 공공 및 개인 상태를 모두 가질 수 있으며, 이는 전적으로 메모리 또는 선택적으로 지속적으로 보관 될 수 있습니다. 에이전트의 공개 상태는 레코드의 필드와 유사한 다수의 차선으로 구성됩니다. 예를 들어 단일 값을 포함하는 차선과 키 값 쌍의 맵을 포함하는 차선이 여러 종류가 있습니다.
차선의 상태는 다른 에이전트 인스턴스 또는 전용 클라이언트에서 링크를 설정하여 관찰 할 수 있습니다. 확립 된 링크는 모든 업데이트를 해당 레인의 상태로 푸시하여 가입자가 상태에 대한 변경 사항 (이를 지원하는 레인 종류의 경우)을 요청할 수 있습니다. 링크는 웹 소켓 연결을 통해 작동하며 다중화되므로 동일한 호스트의 여러 차선으로의 링크가 단일 웹 소켓 연결을 공유 할 수 있습니다.
웹 사이트 | 개발자 가이드 | 서버 API 문서 | 클라이언트 API 문서
녹에서 수영 에이전트를 구현합니다
수영 서버 응용 프로그램 구축
참조 문서
다음 예제 응용 프로그램은 각 에이전트 인스턴스에 lane 이라는 단일 레인이있는 단일 에이전트 경로를 호스팅하는 Swimos 서버를 실행합니다. 차선이 변경 될 때마다 서버가 콘솔에 인쇄됩니다.
[ 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 와 함께 업데이트를 Lane lane 의 URI /examples/name 에이전트에 보내는 경우, ExampleLifecycle 사용하여 ExampleAgent 인스턴스가 서버에서 시작됩니다. 그런 다음 차선의 값이 5 로 설정되고 다음은 콘솔에 인쇄됩니다.
Received value: 5 for 'lane' on agent at URI: /examples/name.
개별 기능과보다 포괄적 인 응용 프로그램을 보여주는 exames_apps 디렉토리에서 여러 가지 예제 응용 프로그램을 사용할 수 있습니다.
개발 안내서를 참조하십시오.
이 프로젝트는 Apache 2.0 라이센스에 따라 라이센스가 부여됩니다.