============================================================================= 演示命令 ============================================================================= runBasicDemo 基础 Ping-Pong 演示
(ctx context.Context, cmd *cli.Command)
| 227 | |
| 228 | // runBasicDemo 基础 Ping-Pong 演示 |
| 229 | func runBasicDemo(ctx context.Context, cmd *cli.Command) error { |
| 230 | fmt.Println("\n🎯 基础 Actor 演示(Ping-Pong)") |
| 231 | fmt.Println(strings.Repeat("=", 50)) |
| 232 | |
| 233 | // 创建 Actor 系统 |
| 234 | system := actor.NewSystem("basic-demo") |
| 235 | defer system.Shutdown() |
| 236 | |
| 237 | // 创建 Echo Actor |
| 238 | echo := &EchoActor{name: "Echo"} |
| 239 | pid := system.Spawn(echo, "echo") |
| 240 | |
| 241 | fmt.Println("\n📤 发送 3 个 Ping 消息...") |
| 242 | |
| 243 | // 发送多个 Ping 并等待 Pong |
| 244 | for i := 1; i <= 3; i++ { |
| 245 | resp, err := pid.Request(&PingMsg{Count: i}, 5*time.Second) |
| 246 | if err != nil { |
| 247 | return fmt.Errorf("请求失败: %w", err) |
| 248 | } |
| 249 | |
| 250 | if pong, ok := resp.(*PongMsg); ok { |
| 251 | fmt.Printf("📥 收到 Pong(%d)\n", pong.Count) |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | fmt.Println("\n✅ 基础演示完成!") |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | // runCounterDemo 并发计数器演示 |
| 260 | func runCounterDemo(ctx context.Context, cmd *cli.Command) error { |