使用SteamKit2无法连接到蒸汽

时间:2015-06-19 19:57:06

标签: c# steam

我正在使用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);
        }

    }
}

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

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

就是这样......

以下代码处理AuthCode和TwoFactor身份验证: https://github.com/SteamRE/SteamKit/tree/master/Samples/5.SteamGuard

答案 1 :(得分:1)

您需要处理更多回调。

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

<强> SteamBot.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);
        }
    }
}

<强> Program.cs的

namespace StackOverflow
{
    class Program
    {
        static void Main(string[] args)
        {
            SteamBot bot = new SteamBot("botname", "password");

            while(true)
            {
                if(bot.isReady)
                {
                    bot.WaitForCallbacks();
                }
            }
        }
    }
}

答案 2 :(得分:0)

您需要在尝试连接之前添加Couldn't match expected type ‘A’ with actual type ‘C’ In the first argument of ‘fa’, namely ‘(c :: A)’ In the expression: fa (c :: A) ,以便SteamKit可以更新其内部服务器列表,如Playground所述。