Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to build an Alchemy Websockets server with VB.NET. I have taken the sample code, in C#, for setting up a new instance of the server and ran it though a C# to VB converter.


ORGINAL C# CODE:-

C#
{
  // instantiate a new server - acceptable port and IP range,
  // and set up your methods.

  var aServer = new WebSocketServer(81, IPAddress.Any) {
    OnReceive = OnReceive,
    OnSend = OnSend,
    OnConnect = OnConnect,
    OnConnected = OnConnected,
    OnDisconnect = OnDisconnect,
    TimeOut = new TimeSpan(0, 5, 0)
  };

  aServer.Start();
}


AFTER CONVERSION:-

VB
Dim aServer = New WebSocketServer(81, IPAddress.Any) With { _
    Key .OnReceive = OnReceive, _
    Key .OnSend = OnSend, _
    Key .OnConnect = OnConnect, _
    Key .OnConnected = OnConnected, _
    Key .OnDisconnect = OnDisconnect, _
    Key .TimeOut = New TimeSpan(0, 5, 0) _
}

aServer.Start()


This doesn't work, it doesn't link the "Key", so, this is how I've ended up:-

VB
__aServer = New WebSocketServer(8181, IPAddress.Any)
With __aServer
  .OnConnect = New OnEventDelegate(AddressOf OnUserConnect)
  .OnDisconnect = New OnEventDelegate(AddressOf OnUserDisconnect)
  .OnReceive = New OnEventDelegate(AddressOf OnDataReceive)
  .OnSend = New OnEventDelegate(AddressOf OnDataSend)
  .TimeOut = New TimeSpan(0, 5, 0)
End With
__aServer.Start()



This seems to pass a syntax check, and the code runs ok, but, the only event to fire is the "OnConnect". So when I send data from the client to the server, nothing fires.

Can anyone throw some light on this for me?

Regards

Mark
Posted

1 solution

Well, at first glance the object initializer isn't formatted correctly. How about this:

VB
Dim aServer = New WebSocketServer(81, IPAddress.Any) With { _
     .OnReceive    = OnReceive,
     .OnSend       = OnSend,
     .OnConnect    = OnConnect,
     .OnConnected  = OnConnected,
     .OnDisconnect = OnDisconnect,
     .TimeOut      = New TimeSpan(0, 5, 0)
}
aServer.Start()


Also, looking at your fix, you are creating new OnEventDelegates. Have you tried simply passing each one AddressOf yourCallbackSub? Ie:

VB
Dim aServer = New WebSocketServer(81, IPAddress.Any) With { _
     .OnReceive    = AddressOf OnDataReceive,
     .OnSend       = AddressOf OnDataSend,
     .OnConnect    = AddressOf OnUserConnect,
     .OnConnected  = AddressOf ?, ' You left this one out of your fix. This could be your missing event...
     .OnDisconnect = AddressOf OnUserDisconnect,
     .TimeOut      = New TimeSpan(0, 5, 0)
}
aServer.Start()


- Pete
 
Share this answer
 
v4
Comments
Mark R Slade 7-Oct-13 9:55am    
I've tried it this way, but it errors saying that I need to generate a method stub, which I do but I don't know how to pass the parameters to the stub.
pdoxtader 7-Oct-13 9:57am    
I just added to the solution. Have you tried the 2nd way?
pdoxtader 7-Oct-13 9:58am    
Also, look the the callback methods in your c# code. That's where you'll find the signature for your callback methods.
Mark R Slade 7-Oct-13 10:15am    
I've just tried the second way. This is looking more promising, but, I get this error on the OnUserConnect sub:-

'Private Sub OnUserConnect(context As Alchemy.Classes.UserContext)' and 'Private Function OnUserConnect(context As Alchemy.Classes.UserContext) As Alchemy.OnEventDelegate' cannot overload each other because they differ only by return types
pdoxtader 7-Oct-13 10:26am    
Yes... the signatures have to be different. That's a VB.NET thing. You'll have to choose one or the other.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900