BytexDigital / BytexDigital.BattlEye.Rcon

A simple to use RCON library for BattlEye
Apache License 2.0
11 stars 9 forks source link

Help a noob #12

Closed rjclarkewp007 closed 2 weeks ago

rjclarkewp007 commented 3 years ago

Hey, anyone interested in helping out a noob here please?

I've got a little launcher setup in VS c# and im trying to connect to a Dayz server just to show how many players are online. That's all I'm trying to do and i just cant get it to work hehe.. anyone able to share some coding which i can use to show online players in a textbox or label or something please?

rjclarkewp007 commented 2 years ago

Please help :( I am still trying to figure this out.

I can make a rcon connection, but as soon as the Fetch command start running the application freezes and won't return the players count. Is the code below incorrect? Strangely enough when there are 0 players then the count shows 0 and no freezes..

        //Connection info
        RconClient networkClient = new RconClient("192.168.1.105", 2320, "12345");
        networkClient.Connected` += NetworkClient_Connected;

        // Connect
        networkClient.Connect();

        // Optionally make sure we are connected
        networkClient.WaitUntilConnected();

        var requestSuccess = networkClient.Fetch(
        command: new GetPlayersRequest(),
        timeout: 5000,
        result: out List<Player> onlinePlayers);

        if (requestSuccess)
        {
            label_count.Text = onlinePlayers.Count.ToString();
        }
        else
        {
            label1.Text = "Disconnected from Rcon";
            label_count.Text = "Playing Err/60";
        }
RyanTT commented 2 years ago

Hi, I'll get you a working example once I'm back home later today, have you tried debugging whether maybe it gets stuck at WaitUntilConnected?

rjclarkewp007 commented 2 years ago

hey, thanks so much for the reply! Much appreciated! I did comment out WaitUntilConnected, but same things happens.

RyanTT commented 2 years ago

Try putting in Console.WriteLine before and after the WaitUntilConnected to check whether it gets stuck there or after it

rjclarkewp007 commented 2 years ago

Okay i did this and it got stuck at the Fetch command. I never got the last message box.

        MessageBox.Show("This is before wait");
        networkClient.WaitUntilConnected();
        MessageBox.Show("This is after wait");

        bool requestSuccess = networkClient.Fetch(
        command: new GetPlayersRequest(),
        timeout: 5000,
        result: out List<Player> onlinePlayers);

        MessageBox.Show("This is after Fetch");
rjclarkewp007 commented 2 years ago

But also once again, when the server is empty then the output shows 0 and the application does not get stuck. Weird hehe oh and by the way, I am using Windows Forms and not console. If I use BytexDigital.BattlEye.Rcon.TestClient then it works perfectly fine. So maybe it just does not work with Forms? Remember I am trying to display the count in a Label/RichTextbox.TextBox. Which ever one will work.

RyanTT commented 2 years ago

Do you use Windows Forms on .NET Core or .NET Framework?

RyanTT commented 2 years ago

Please also try putting your code into a try catch and putting a break point/printing the Exception that might get thrown. Maybe an exception is being thrown that is just not appearing visible to you, which is why your MessageBox is never shown. Sometime like this:

try
{
    MessageBox.Show("This is before wait");
    networkClient.WaitUntilConnected();
    MessageBox.Show("This is after wait");

    bool requestSuccess = networkClient.Fetch(
    command: new GetPlayersRequest(),
    timeout: 5000,
    result: out List<Player> onlinePlayers);

    MessageBox.Show("This is after Fetch");
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}
rjclarkewp007 commented 2 years ago

Hey I tried that and still the same problem. After the FETCH command the app hangs and does not even get to the exception part. Strangely enough, when I connect to my local dayz server it always gives me 0 as the player count even if there is 1 player online. The app does not hang then. Only when I connect out to my actual live server does the app hang. I can use any other Rcon tool to connect to my server and that works perfectly fine.

I am using .Net Framework 4.8. Previously tried it on 4.7.2 and VS2019, but now I am using VS2022 and new Framework 4.8.

Code which I tried to use is like this now:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using BytexDigital.BattlEye.Rcon;
using BytexDigital.BattlEye.Rcon.Commands;
using BytexDigital.BattlEye.Rcon.Domain;

namespace RconTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        RconClient networkClient = new RconClient("192.168.1.105", 2320, "12345");

        private void rconConnect()
        {
            // Connection Info

            //networkClient.MessageReceived += NetworkClient_MessageReceived;
            networkClient.Connected += NetworkClient_Connected;

            // Connect
            networkClient.Connect();

            // Optionally make sure we are connected
            try
            {
                MessageBox.Show("This is before wait");
                networkClient.WaitUntilConnected();
                MessageBox.Show("This is after wait");

                bool requestSuccess = networkClient.Fetch(
                command: new GetPlayersRequest(),
                timeout: 5000,
                result: out List<Player> onlinePlayers);

                MessageBox.Show("This is after Fetch command");

                if (requestSuccess)
                {
                    label_count.Text = onlinePlayers.Count.ToString();
                    MessageBox.Show("Request Successfull");
                }
                else
                {
                    label1.Text = "Request not successfull";
                    label_count.Text = "Error";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

            MessageBox.Show("This is after TRY command");

        }

        private void NetworkClient_Connected(object sender, EventArgs e)
        {

            label1.Text = "Connected to Rcon";
            MessageBox.Show("Rcon Connected!");
            button_connect.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            rconConnect();
        }

    }

}
RyanTT commented 2 years ago

It seems that .NET Framework breaks something in the functionality of this library. If you are able to, I suggest you switch your project to .NET 5 instead (for example using this guide https://docs.microsoft.com/en-us/dotnet/desktop/winforms/migration/?view=netdesktop-6.0) or by creating a new project with .NET 5 WinForms selected. I'm afraid I cannot reproduce the issue with .NET Framework 4.7.2, so it is hard for me to help.

rjclarkewp007 commented 2 years ago

Hey there, I created a new project and selected .Net 5 Windows Application. Setup a small test again, same as before and still the same thing happens. Application gets stuck at the Fetch command.

image

Gets stuck after: MessageBox.Show("This is after wait");

image

strzemek commented 1 year ago

@rjclarkewp007 I had the same issue. It can be solved by making rconConnect an async function and using async version of Fetch. Try using it like that: var onlinePlayers = await networkClient.FetchAsync<List<Player>, GetPlayersRequest>(new GetPlayersRequest(), new CancellationToken()); Then onlinePlayers.Item1 will be the bool result and onlinePlayers.Item2 the List of Player objects. Hope it helps.

rjclarkewp007 commented 1 year ago

@rjclarkewp007 I had the same issue. It can be solved by making rconConnect an async function and using async version of Fetch. Try using it like that: var onlinePlayers = await networkClient.FetchAsync<List<Player>, GetPlayersRequest>(new GetPlayersRequest(), new CancellationToken()); Then onlinePlayers.Item1 will be the bool result and onlinePlayers.Item2 the List of Player objects. Hope it helps.

Hi, thanks for the reply. I tried this, but I receive an error for FetchAsync: image

If I remove the async from Fetch, then I still have an error: image

strzemek commented 1 year ago

@rjclarkewp007 what version you are using? If 1.0.2 stable, upgrade to 1.0.3 preview version via NuGet manager. I get same error as you on 1.0.2 version but on 1.0.3 it works perfectly fine ;)

rjclarkewp007 commented 1 year ago

Oh yay! That works perfectly yes. Thanks so much man! You are awesome! :)