Page 1 of 1

Visual Basic .NET

Posted: 21 Dec 2010, 02:18
by sjarman
Does anyone know of any examples anywhere for using SmartFox in VB .NET?

I have an existing Windows service that I'd like to add functionality to rather than rewrite. I can get the SmartFox object created, get it to connect to my server, but I'm not sure how to go about the event handling.

If anyone has done any SmartFox/VB stuff, I'd appreciate any clues you might have for me.

Posted: 21 Dec 2010, 05:07
by sjarman
By the way, I'm working with the example below for testing.

Code: Select all

using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;

public class MyTest
   {
      private SmartFoxClient smartFox;

      public function MyTest()
      {
          // Create instance
          smartFox = new SmartFoxClient();

          // Add event handler for connection 
          SFSEvent.onConnection += OnConnection;

          // Connect to server
          smartFox.Connect("127.0.0.1", 9339)    
      }

      // Handle connection event
      public void OnConnection(bool success, string error)
      {
          if (success)
              Trace.WriteLine("Great, successfully connected!");
          else
              Trace.WriteLine("Ouch, connection failed!");
      }    
   }
The biggest problem seems to be that there's no equivalent way (that I know of) to assign the event handler in VB - i.e.

Code: Select all

SFSEvent.onConnection += OnConnection;
You would usually do something like this:

Code: Select all

AddHandler SFSEvent.onConnection, AddressOf OnConnection
But that only works if onConnection is of type "Event"... which it's not.

If anyone has any ideas, please let me know.

Posted: 21 Dec 2010, 05:33
by dragagon
Seems you are going about it wrong. You should be doing something closer to

Code: Select all

         smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
not doing that on a SFSEvent.

Posted: 21 Dec 2010, 06:07
by ThomasLund
It works in C# console - so it _should_ work in VB too, but I'm not qualified to tell you the syntax. Last time I did any basic coding was on my Amstrad 464 like........22-23 years ago. Lol

/Thomas

Posted: 21 Dec 2010, 07:04
by sjarman
dragagon wrote:Seems you are going about it wrong. You should be doing something closer to

Code: Select all

         smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
not doing that on a SFSEvent.
There's no AddEventListener method. Not sure what you mean.

Posted: 21 Dec 2010, 07:59
by ThomasLund
You might have posted in the wrong forums possibly!

This is for SFS2X API. It uses AddEventListener()

What I guess you are trying is to use the SFS1 API - thats a different forum further down the list.

/Thomas

Posted: 21 Dec 2010, 08:16
by sjarman
Ahhh... no I am using SFS2X - but I'm using the wrong client API. I'm not the smartest lollypop in the candy store.

I'll sort that out and let you know how I go.

Posted: 21 Dec 2010, 08:30
by ThomasLund
Yeah - that wont work :-)

/Thomas

Posted: 22 Dec 2010, 00:09
by sjarman
ThomasLund wrote:Yeah - that wont work :-)
Yeah......... thanks for the wise words there Thomas ;)

If anyone a bit smarter than me gets a chance to take a look at this on their Visual Studio sometime, I'd really appreciate it. I can't get past not being able to create the event handlers. Pretty much dead in the water at the moment and I so don't want to rewrite this entire service in C# :(

Posted: 22 Dec 2010, 00:58
by tchen
I tried in in VB2005 and there's at least a one-to-one correspondence with the C# as far as I could tell. Intellisense on VB was a bit sticky though, so that's probably why you didn't see AddEventListener at first.

Code: Select all

Public Class Form1
    Dim m_sfs As Sfs2X.SmartFox

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_sfs = New Sfs2X.SmartFox(True)
        m_sfs.AddEventListener(Sfs2X.Core.SFSEvent.LOGIN, AddressOf OnLogin)
    End Sub

    Private Sub OnLogin(ByVal evt As Sfs2X.Core.BaseEvent)
        'DO SOMETHING
    End Sub
End Class

Posted: 22 Dec 2010, 00:59
by sjarman
By the way, this is what I'm working with. Just a default Windows Application project with the SmartFox reference added.

This code executes fine, but the events never fire.

Code: Select all

Imports Sfs2X
Imports Sfs2X.Core
Imports Sfs2X.Requests

Public Class Form1
    Dim sf As New SmartFox

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sf.AddEventListener(SFSEvent.CONNECTION, AddressOf OnConnection)
        sf.AddEventListener(SFSEvent.CONNECTION_LOST, AddressOf onConnectionLost)
        sf.AddEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, AddressOf onConfigLoadSuccess)
        sf.AddEventListener(SFSEvent.CONFIG_LOAD_FAILURE, AddressOf onConfigLoadFailure)
        sf.AddEventListener(SFSEvent.LOGIN, AddressOf onLogin)
        sf.AddEventListener(SFSEvent.LOGIN_ERROR, AddressOf onLoginError)

        sf.Connect("70.32.106.51", 9933)
        sf.Send(New LoginRequest("", "", "Test"))
    End Sub

    Public Sub OnConnection()
        Debug.Print("OnConnection")
    End Sub

    Public Sub onConnectionLost()
        Debug.Print("onConnectionLost")
    End Sub

    Public Sub onConfigLoadSuccess()
        Debug.Print("onConfigLoadSuccess")
    End Sub

    Public Sub onConfigLoadFailure()
        Debug.Print("onConfigLoadFailure")
    End Sub

    Public Sub onLogin()
        Debug.Print("onLogin")
    End Sub

    Public Sub onLoginError()
        Debug.Print("onLoginError")
    End Sub
End Class

Posted: 22 Dec 2010, 01:10
by sjarman
@tchen - that code worked for you? i.e. the event actually fired?

Posted: 22 Dec 2010, 01:11
by tchen
Don't forget to call

Code: Select all

m_sfs.ProcessEvents()
in a timer or elsewhere.

Posted: 22 Dec 2010, 06:34
by sjarman
IT'S WORKING!!!

It was the ProcessEvents call that I was missing, once the syntax for the event handlers was in place.

For anyone else's reference who comes looking for this:

1) Create project, "Windows Forms Application" (or whatever).
2) Project, Add Reference, Browse - locate the SmartFox2.dll file.
3) Basic working example below. In this case I've just added a second button that allows you to manually execute ProcessEvents but obviously that should be on a timer or something like tchen said.

Thanks so much for the help guys! I really appreciate it! :D

Code: Select all

Imports Sfs2X
Imports Sfs2X.Core
Imports Sfs2X.Requests

Public Class Form1
    Dim sf As New SmartFox(False)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        sf.AddEventListener(SFSEvent.CONNECTION, AddressOf OnConnection)
        sf.AddEventListener(SFSEvent.CONNECTION_LOST, AddressOf OnConnectionLost)

        sf.Connect("???.???.???.???", 9933) ' Your server's IP
    End Sub

    Public Sub OnConnection(ByVal evt As Sfs2X.Core.BaseEvent)
        Debug.Print("OnConnection" & StrDup(20, "*"))
    End Sub

    Public Sub OnConnectionLost(ByVal evt As Sfs2X.Core.BaseEvent)
        Debug.Print("OnConnectionLost" & StrDup(20, "*"))
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        sf.ProcessEvents()
    End Sub
End Class