Visual Basic .NET

Post here your questions about the Unity / .Net / Mono / Windows 8 / Windows Phone 8 API for SFS2X

Moderators: Lapo, Bax

Post Reply
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Visual Basic .NET

Post 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.
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Post 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.
dragagon
Posts: 19
Joined: 05 Dec 2010, 00:23

Post 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.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post 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
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Post 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.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post 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
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Post 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.
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Post by ThomasLund »

Yeah - that wont work :-)

/Thomas
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Post 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# :(
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Post 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
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Post 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
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Post by sjarman »

@tchen - that code worked for you? i.e. the event actually fired?
tchen
Posts: 191
Joined: 11 Dec 2010, 14:14

Post by tchen »

Don't forget to call

Code: Select all

m_sfs.ProcessEvents()
in a timer or elsewhere.
sjarman
Posts: 15
Joined: 18 Dec 2010, 10:56

Post 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
Post Reply