Visual Basic .NET
Visual Basic .NET
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.
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.
By the way, I'm working with the example below for testing.
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.
You would usually do something like this:
But that only works if onConnection is of type "Event"... which it's not.
If anyone has any ideas, please let me know.
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!");
}
}
Code: Select all
SFSEvent.onConnection += OnConnection;
Code: Select all
AddHandler SFSEvent.onConnection, AddressOf OnConnection
If anyone has any ideas, please let me know.
Seems you are going about it wrong. You should be doing something closer to
not doing that on a SFSEvent.
Code: Select all
smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);-
ThomasLund
- Posts: 1297
- Joined: 14 Mar 2008, 07:52
- Location: Sweden
There's no AddEventListener method. Not sure what you mean.dragagon wrote:Seems you are going about it wrong. You should be doing something closer tonot doing that on a SFSEvent.Code: Select all
smartFox.AddEventListener(SFSEvent.CONNECTION, OnConnection);
-
ThomasLund
- Posts: 1297
- Joined: 14 Mar 2008, 07:52
- Location: Sweden
-
ThomasLund
- Posts: 1297
- Joined: 14 Mar 2008, 07:52
- Location: Sweden
Yeah......... thanks for the wise words there ThomasThomasLund wrote:Yeah - that wont work
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#
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 ClassBy 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.
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
Don't forget to call
in a timer or elsewhere.
Code: Select all
m_sfs.ProcessEvents()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!
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!
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