Philipp15b / go-steam

Steam's protocol in Go to allow automation of different actions on the Steam network without running an actual Steam client. Includes APIs for friends, chatting, trading, trade offers and TF2 crafting.
https://pkg.go.dev/github.com/Philipp15b/go-steam/v3
Other
391 stars 131 forks source link

how can I disconnect correctly without a data race? #134

Open pashatag opened 1 year ago

pashatag commented 1 year ago
func (c *Client) Launch(ctx context.Context, username, password string) {
    go func() {
        events := c.client.Events()
        for event := range events {
            switch event.(type) {
            case *steam.ConnectedEvent:
                c.client.Auth.LogOn(&steam.LogOnDetails{
                    Username: username,
                    Password: password,
                })

            case *steam.LoggedOnEvent:
                c.client.Social.SetPersonaState(steamlang.EPersonaState_Online)
                if c.onLoggin != nil {
                    c.onLoggin()
                }

            case *steam.DisconnectedEvent:
                log.Println("disconnected")

            default:
                if c.onRaw != nil {
                    c.onRaw(event)
                }
            }
        }
    }()

    steam.InitializeSteamDirectory()
    c.client.Connect()

    go func() {
        <-ctx.Done()
        c.client.Disconnect()
    }()
}