Examples/C#.NET & VB.NET

Creating a new project

We're going to create a new project to use the SwiftApi client library. It will be called SwiftApiClient.

  1. Open up Visual Studio
  2. Create a new project (I'm creating a Visual Basic Windows Forms application) called SwiftApiClient

Adding the new project

Download the Thrift base library

The SwiftApi generated code (which we will generate in a minute) depends on the Thrift project. We need to add the Thrift source code to our project.

  1. Download the Apache Thrift source code from: http://thrift.apache.org/download/ (You need to download the thrift-0.9.0.tar.gz file at the top of the page)
  2. Open the downloaded file using WinRAR or 7zip or something
  3. Extract the folder /thrift-0.9.0/lib/csharp/src to somewhere
  4. Back in Visual Studio, click File > Add > Existing Project
  5. Browse to wherever you extracted the /thrift-0.9.0/lib/csharp/src and choose Thrift.csproj
  6. You should now see the Thrift library added to your project

Successfully added the Thrift project

Generating the SwiftApi client code

If you haven't already, you need to download or install the Thrift compiler. You can grab thrift-0.9.0.exe from http://thrift.apache.org/download/ .

Once you've downloaded the Thrift compiler and grabbed the SwiftApi.thrift and Errors.thrift file from GitHub, you can run the following command to generate the C# sources:

thrift -r --gen csharp SwiftApi.thrift

This will leave you with a "gen-csharp" folder that contains the code (under gen-csharp).

Now:

  1. You need to add the generated code to your new project
  2. In Visual Studio, click File > Add > New Project
  3. Choose Visual C# as the language, and Class Library as the type, and SwiftApi as the name

Adding the new project

Now drag-and-drop the generated C# code from the gen-csharp/org/phybros/thrift folder into the new project you just created

Add references

The project is probably full of errors now. You need to add references in a couple of places.

  1. Right click the SwiftApi project and select Add Reference
  2. Under the Projects tab, choose the Thrift project
  3. Right click the SwiftApiClient and select Add Reference
  4. Under the Projects tab, choose the Thrift project
  5. Right click the SwiftApiClient AGAIN and select Add Reference
  6. Under the Projects tab, choose the SwiftApi project

Now the projects are all set up properly

Using the code in your project

If you add a Button to your form, in the Click event handler type the following code:

Dim socket As New TSocket("your.bukkitserver.org", 21111)
Dim transport As New TFramedTransport(socket)
Dim protocol As New TBinaryProtocol(transport)
Dim client As New SwiftApi.Client(protocol)

'This is very important
transport.Open()

Dim serverVersion As String = client.getServerVersion(getAuthString("getServerVersion"))
MessageBox.Show(String.Format("Got server version: {0}", serverVersion))

transport.Close()

You will need to also add a getAuthString() method to your code file. Here's one that works:

Public Function getAuthString(ByVal MethodName As String)
    Dim PreHash As String = "swiftapiusername" & MethodName & "swiftapipassword" & "swiftapisalt"
    Return GetSHA256String(PreHash)
End Function

Public Function GetSHA256String(ByVal data As String) As String
    Dim sh As New System.Security.Cryptography.SHA256Managed()
    Dim result() As Byte = sh.ComputeHash(Encoding.UTF8.GetBytes(data))
    Dim sb As New StringBuilder()

    For i As Integer = 0 To result.Length - 1
        sb.Append(result(i).ToString("x2"))
    Next

    Return sb.ToString().ToLower()
End Function

Note: don't forget to change the login details to the ones you set in your SwiftApi yml file on your server.

Now, when you click the button, your server's version should pop up in a message box.

Print A List Of All Online Players (in C#, but VB is very similar)

//open a connection to a server running SwiftApi
TSocket socket = new TSocket("your.bukkitserver.org", 21111);
socket.Open();

//create a new client object
TBinaryProtocol protocol = new TBinaryProtocol(new TFramedTransport(socket));
SwiftApi.Client client = new SwiftApi.Client(protocol);

// get all online players
List<Player> Players = client.getPlayers(authString);

//print out the total number of players online
Console.WriteLine("There are " + Players.Count.ToString() + " players online");

foreach (Player p in Players) {
	// print out each player's name
	Console.WriteLine(p.Name);
}

//close the connection
socket.Close();

Comments

Posts Quoted:
Reply
Clear All Quotes