vRp.CD2g_test/examples/simple-server-lockstep/main.go

51 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"github.com/kercylan98/minotaur/component/components"
"github.com/kercylan98/minotaur/game/builtin"
"github.com/kercylan98/minotaur/server"
"github.com/kercylan98/minotaur/utils/synchronization"
)
type Player struct {
*builtin.Player[string]
}
type Command struct {
CMD int
Data string
}
// 访问http://www.websocket-test.com/
// - 使用多个页面连接到服务器后任一页面发送start即可开启帧同步
func main() {
players := synchronization.NewMap[string, *Player]()
srv := server.New(server.NetworkWebsocket, server.WithWebsocketWriteMessageType(server.WebsocketMessageTypeText))
lockstep := components.NewLockstep[string, *Command]()
srv.RegConnectionOpenedEvent(func(srv *server.Server, conn *server.Conn) {
player := &Player{Player: builtin.NewPlayer[string](conn.GetID(), conn)}
players.Set(conn.GetID(), player)
lockstep.JoinClient(player)
})
srv.RegConnectionClosedEvent(func(srv *server.Server, conn *server.Conn, err any) {
players.Delete(conn.GetID())
lockstep.LeaveClient(conn.GetID())
if players.Size() == 0 {
lockstep.StopBroadcast()
}
})
srv.RegConnectionReceiveWebsocketPacketEvent(func(srv *server.Server, conn *server.Conn, packet []byte, messageType int) {
switch string(packet) {
case "start":
lockstep.StartBroadcast()
default:
lockstep.AddCommand(&Command{CMD: 1, Data: string(packet)})
}
})
if err := srv.Run(":9999"); err != nil {
panic(err)
}
}