Page 1 of 1

Event handling problem in C#

Posted: 08 Feb 2010, 11:47
by kaskalak

Code: Select all

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form

    {
        private string ip = "127.0.0.1";
        private int port = 9339;
        private string status = "";
        private SmartFoxClient smartFox;
        private string zone = "simpleChat";
        private string username = "";
        private string password = "";
        private bool bo;


        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.Start();

        }
        void Start()
        {
            smartFox = new SmartFoxClient();
            SFSEvent.onConnection += OnConnection;
            SFSEvent.onConnectionLost += OnConnectionLost;
            smartFox.Connect(ip, port);   
           
        }


        //----------------------------------------------------------
        // Handle connection response from server
        //----------------------------------------------------------
        void OnConnection(bool success, string error)
        {
            if (success)
                label1.Text = "Connected";
            else
                label1.Text = "Could not connect";
        }

        void OnConnectionLost()
        {
           
            label1.Text = "Connection lost / no connection to server";
        }

       
    }
}



Problem is that program never triggers the function OnConnection or OnconnectionLost.

When i try to connect server , it should show the status on label1. But there is no change. When i debugged i realized that "OnConnection" was not triggered. But on the server administration panel i can see the socket connection.

Could you please help me?

Posted: 09 Feb 2010, 06:54
by ThomasLund
I personally dont code ASP stuff, so I might not be the best to help you.

But I do have a hunch from when I made the Silverlight port.

If I understood corrent, then ASP uses a response/request model, where you dont get updates after the initial execution.

So try to run the SFS API in queue mode and make a button that empties the queue (actively forcing any network events that sit inside the API to call the callback methods in your code).

If that triggers the delegates, then what you need to do is set up a timer that every 50-100 ms tries to empty the queue. The Silverlight API port does this internally by itself, as it ended up being the only practical way to use SFS from the webpage.

Hope that helps!

/Thomas

Posted: 11 Feb 2010, 08:53
by kaskalak
I took those codes from examples that come with smartfoxserver. With small changes i tried to run it on C# . Why does it not work. Do i have to change any configuration on my compiler which makes it to trigger eventhandling functions.

Posted: 11 Feb 2010, 08:59
by ThomasLund
Are you coding C# as a console app or using it to code web pages? There is a huge difference in how the underlying system works (not the API itself).

Your code looks like a webpage, where there is no "running main program". Its all UI event driven.

In a console application you typically have a main loop that keeps the entire program "alive".

The API works perfectly with console apps written in C#. If used in a webpage you need to manually make sure to process the event queue as explained using a timer that runs in your ASP page.

Give some more info on what it is you are doing, and I hope I can help you.

/Thomas

Posted: 11 Feb 2010, 09:32
by kaskalak
It's a windows Form application . I create a form and put a button on that form. I do not think i need a loop.

Posted: 11 Feb 2010, 09:58
by ThomasLund
There is no reason for a Windows form application to not work really out of the box afaik.

Are you by any chance running the "lite" version of SFS (which is not supported)?

Posted: 11 Feb 2010, 12:05
by kaskalak
ThomasLund wrote:There is no reason for a Windows form application to not work really out of the box afaik.

Are you by any chance running the "lite" version of SFS (which is not supported)?


No, i'm running pro version.

Posted: 11 Feb 2010, 12:23
by ThomasLund
I just made a reworked copy/paste of your code and made it as a small console app - and here it works as expected.

So I'm sorry, but I cant help without more information. It must be something specific to Windows forms (which I have no experience with) or local network or firewall permissions maybe?!?!?!

/Thomas

Complete test code:

Code: Select all

using System;
using SmartFoxClientAPI;

namespace sfssimpleconsoletest
{
   class MainClass
   {
      public static void Main (string[] args)
      {
         SFSClient client = new SFSClient();
         client.Connect();
        }
   }
   
   public class SFSClient {
      private string ip = "127.0.0.1";
        private int port = 9339;
        private string status = "";
        private SmartFoxClient smartFox;
        private string zone = "simpleChat";
        private string username = "";
        private string password = "";
        private bool bo;
      
      public SFSClient ()
      {
         Console.WriteLine ("Start connection!");

            smartFox = new SmartFoxClient();
            SFSEvent.onConnection += OnConnection;
            SFSEvent.onConnectionLost += OnConnectionLost;
      }
      
      public void Connect() {
            smartFox.Connect(ip, port);             
        }


        //----------------------------------------------------------
        // Handle connection response from server
        //----------------------------------------------------------
        void OnConnection(bool success, string error)
        {
            if (success)
            Console.WriteLine ("Connected");
            else
            Console.WriteLine ("Could not connect");
        }

        void OnConnectionLost()
        {
         Console.WriteLine ("Connection lost / no connection to server");
        }
   }
}


Output is:

Code: Select all

Start connection!
Connected

Posted: 11 Feb 2010, 12:30
by kaskalak
ThomasLund wrote:I just made a reworked copy/paste of your code and made it as a small console app - and here it works as expected.

So I'm sorry, but I cant help without more information. It must be something specific to Windows forms (which I have no experience with) or local network or firewall permissions maybe?!?!?!

/Thomas

Complete test code:

Code: Select all

using System;
using SmartFoxClientAPI;

namespace sfssimpleconsoletest
{
   class MainClass
   {
      public static void Main (string[] args)
      {
         SFSClient client = new SFSClient();
         client.Connect();
        }
   }
   
   public class SFSClient {
      private string ip = "127.0.0.1";
        private int port = 9339;
        private string status = "";
        private SmartFoxClient smartFox;
        private string zone = "simpleChat";
        private string username = "";
        private string password = "";
        private bool bo;
      
      public SFSClient ()
      {
         Console.WriteLine ("Start connection!");

            smartFox = new SmartFoxClient();
            SFSEvent.onConnection += OnConnection;
            SFSEvent.onConnectionLost += OnConnectionLost;
      }
      
      public void Connect() {
            smartFox.Connect(ip, port);             
        }


        //----------------------------------------------------------
        // Handle connection response from server
        //----------------------------------------------------------
        void OnConnection(bool success, string error)
        {
            if (success)
            Console.WriteLine ("Connected");
            else
            Console.WriteLine ("Could not connect");
        }

        void OnConnectionLost()
        {
         Console.WriteLine ("Connection lost / no connection to server");
        }
   }
}


Output is:

Code: Select all

Start connection!
Connected


Ok, it worked on console application. Really,really thanks