go-steam icon indicating copy to clipboard operation
go-steam copied to clipboard

how can I disconnect correctly without a data race?

Open pashatag opened this issue 2 years ago • 0 comments
trafficstars

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()
	}()
}

pashatag avatar Aug 03 '23 16:08 pashatag