无法使用 SteamKit2 连接到蒸汽

本文关键字:连接 SteamKit2 | 更新日期: 2023-09-27 17:57:15

我正在使用 SteamKit2 在 C# 中制作交易报价机器人,大多数时候它都成功连接到 Steam。 但有时当我让它输出"连接到 Steam..."就在调用client.connect();之前。 它经常发生,需要修复,但我不知道问题是什么。 这是我的代码(很多内容取自 SteamKit2 教程):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SteamKit2;
namespace ATO
{
    class OfferSender
    {
        string username;
        string password;
        SteamClient client;
        CallbackManager manager;
        SteamUser user;
        bool isRunning = false;
        public OfferSender()
        {
        }
        public void login()
        {
            Console.Write("Please enter your username: ");
            username = Console.ReadLine();
            Console.Write("Please enter your password: ");
            password = Console.ReadLine();

            client = new SteamClient();
            manager = new CallbackManager(client);
            user = client.GetHandler<SteamUser>();
            new Callback<SteamClient.ConnectedCallback>(OnConnected, manager);
            new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
            isRunning = true;
            Console.WriteLine("'nConnecting to Steam...'n");
            client.Connect();

            while(isRunning)
            {
                manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
            }
            Console.ReadKey();
        }
        public void OnConnected(SteamClient.ConnectedCallback callback)
        {
            if (callback.Result != EResult.OK)
            {
                Console.WriteLine("Error connecting to Steam: {0}", callback.Result);
                isRunning = false;
                return;
            }
            Console.WriteLine("Connected to steam.'nLogging in {0}...'n", username);
            user.LogOn(new SteamUser.LogOnDetails {
                Username = username,
                Password = password
            });
        }
        public void OnLoggedOn(SteamUser.LoggedOnCallback callback)
        {
            if (callback.Result == EResult.AccountLogonDenied)
            {
                Console.WriteLine("This account is SteamGuard protected.");
                return;
            }
            if(callback.Result != EResult.OK)
            {
                Console.WriteLine("Unable to log in to steam {0}'n", callback.Result);
                isRunning = false;
                return;
            }
            Console.WriteLine("successfully logged in!");
            Environment.Exit(0);
        }
    }
}

我该如何解决这个问题?

无法使用 SteamKit2 连接到蒸汽

好吧,在用头撞键盘之后,我终于发现 Steam 会像任何其他新计算机或浏览器登录一样请求 AuthCode。

就是这样...

下面的代码处理身份验证和双因素身份验证:https://github.com/SteamRE/SteamKit/tree/master/Samples/5.SteamGuard

你需要处理更多的回调。

以下是我如何使用我的机器人登录蒸汽。

蒸汽机器人.cs

using System;
using SteamKit2;
using System.Collections.Generic;
namespace StackOverflow
{
    class SteamBot
    {
        CallbackManager             m_CallbackManager;
        SteamUser                   m_SteamUser;
        SteamClient                 m_SteamClient;
        SteamFriends                m_SteamFriends;
        SteamID                     m_SteamID;
        Dictionary<string, string>  m_Config;
        public bool isLoggedOn { get; private set; }
        public bool isReady { get; private set; }
        public SteamBot(string pUsername, string pPassword)
        {
            isLoggedOn = false;
            isReady = false;
            m_Config = new Dictionary<string, string>();
            m_Config.Add("username", pUsername);
            m_Config.Add("password", pPassword);
            Init();
            RegisterCallbacks();
            Connect();            
        }
        private void Init()
        {
            m_SteamClient = new SteamClient();
            m_CallbackManager = new CallbackManager(m_SteamClient);
            m_SteamFriends = m_SteamClient.GetHandler<SteamFriends>();
            m_SteamUser = m_SteamClient.GetHandler<SteamUser>();
        }
        private void RegisterCallbacks()
        {
            m_CallbackManager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
            m_CallbackManager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
            m_CallbackManager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
            m_CallbackManager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
            m_CallbackManager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
        }
        public void WaitForCallbacks()
        {
            m_CallbackManager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
        }
        public string GetConfigValue(string pKey)
        {
            return m_Config[pKey];
        }
        public void Connect()
        {
            m_SteamClient.Connect();
            isReady = true;
        }
        void OnConnected(SteamClient.ConnectedCallback pData)
        {
            Console.WriteLine("Connected to Steam! Logging in '{0}'...", GetConfigValue("username"));
            SteamUser.LogOnDetails details = new SteamUser.LogOnDetails
            {
                Username = GetConfigValue("username"),
                Password = GetConfigValue("password"),
            };
            m_SteamUser.LogOn(details);
            m_SteamID = m_SteamClient.SteamID;
        }
        void OnDisconnected(SteamClient.DisconnectedCallback pData)
        {
            m_SteamClient.Disconnect();
        }
        void OnLoggedOff(SteamUser.LoggedOffCallback pData)
        {
            isLoggedOn = false;
        }
        void OnLoggedOn(SteamUser.LoggedOnCallback pData)
        {
            if (pData.Result != EResult.OK)
            {
                Console.WriteLine("Unable to login to Steam: {0} / {1}", pData.Result, pData.ExtendedResult);
                return;
            }
            Console.WriteLine("Successfully logged on!");
            isLoggedOn = true;
        }
        void OnAccountInfo(SteamUser.AccountInfoCallback pData)
        {
            //Announce to all friends that we are online
            m_SteamFriends.SetPersonaState(EPersonaState.Online);
        }
    }
}

程序.cs

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            SteamBot bot = new SteamBot("botname", "password");
            while(true)
            {
                if(bot.isReady)
                {
                    bot.WaitForCallbacks();
                }
            }
        }
    }
}

您需要在尝试连接之前添加Steam Directory.Initialize().Wait();,以便 SteamKit 可以更新其内部服务器列表,如此处所述。