Adventures of an Entrepreneur
  • Creating a Mobile Apps/Games Company

Creating a VB.net "WebService" for your Corona app/game

5/25/2013

1 Comment

 
Hi there.

When you want to create multiplayer games or communicate data between your app in different devices you need a server to be the "middleman". Today you can find some companies offering that kind of service for you (Corona has the Corona Cloud, Google now have the Google Play Game Services, ...) but sometimes these services are not sufficient for you (because the free tier price is not enough, or you need more flexibility of types of data that you store,...). So sometimes you need to have your own server.

These type of server that has the mission of providing data for application is called WebService, WebAPI or REST API and in this post I will show you how you can create your own "WebService" for you Corona app. 

You can use any type of language to create the server (PHP, ASP,....). In this case I will be creating a VB.NET server.

I use the quotes in WebService because we are not actually creating a WebService, but a simple WebPage that will receive your app request, process it and give a response. It will work as the same as a WebService and until not I did not find any big difference between the two, even in terms of performance. So, Let's go.

This example will be a game requesting the server all multiplayer games of that user and a specific opponent. So, the app will make a request for the server passing the params via GET and receive the response in JSON format.

Code in the app

-- Function that makes a http request to a server and returns false if not succeed, or returns true and the response if  operation was done with success

function getJSONFromServer(serverURL, paramsToBePassed)

    local json = require "json"
    local jsonData = {}
    local linkComplete

    local paramToString = function(paramsTable)
        
        local str = ""
        local i = 1
        
        for paramName,paramValue in pairs(paramsTable) do 
            if i == 1 then
                str = paramName .. "=" .. paramValue
            else
                str = str .. "&" .. paramName .. "=" .. paramValue
            end
            i=i+1
            
        end
        
        return str
        
    end

    local linkComplete = serverURL .. "?" .. paramToString(paramsToBePassed)
        
    -- Makes the request to the server
    local requestResult, requestResultCode = http.request{url = linkComplete, sink = ltn12.sink.table(jsonData), headers = {accept = "application/json"} }
    
    if (requestResult == nil or requestResultCode ~= 200) then  -- Something wrong occurred in trying to download that page, return false
            return false
    else -- download from internet was done with success
        local jsonFree = json.decode(table.concat(jsonData))  -- decodes the JSON data to a table        
            return true, jsonFree
    end
    
end

Code in the server (VB.NET) - filename: getgames.aspx

Partial Class getGames

    Inherits System.Web.UI.Page

   Public Class gameInfoClass
        Public localPlayerID As Integer
        Public opponentPlayerID As Integer
        Public opponentName As String
        Public gameScore As Integer
    End Class

    Public Class OutputClass
        Public status As Boolean
        Public errorMessage As String         
        Public gamesList As New List(Of gameInfoClass)
    End Class


    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Dim output As New OutputClass

        Dim OKToProceed As Boolean = True

        'Retrieving the GET params
        Dim localPlayerID As String = Page.Request.QueryString("playerID")     
        Dim opponentPlayerID As String = Page.Request.QueryString("opponentID")
      
        'Verifying if required params were entered

        Dim allParamsEntered As Boolean = True

        If localPlayerID = "" Or opponentPlayerID = "" Then

            allParamsEntered = False        

        End If

        If allParamsEntered = False Then
            output.errorMessage = "Required parameters missing"
            OKToProceed = False
        End If

        output.status = OKToProceed

        If OKToProceed = True Then

            ''Opening Connection
            Dim BDConnection = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString

            Dim sqlConnection1 As New System.Data.SqlClient.SqlConnection(BDConnection)

            Dim cmd As New System.Data.SqlClient.SqlCommand

            cmd.CommandType = System.Data.CommandType.Text

            cmd.Connection = sqlConnection1

            cmd.CommandText = "SELECT * FROM vw_games WHERE (local_player_id = @localPlayerID and opponet_player_id = @opponentPlayerID)"
            cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@localPlayerID", localPlayerID))
            cmd.Parameters.Add(New System.Data.SqlClient.SqlParameter("@opponentPlayerID", opponentPlayerID))

            Try

                sqlConnection1.Open()
                Dim reader As System.Data.SqlClient.SqlDataReader
                reader = cmd.ExecuteReader()
                Do While reader.Read()                    Dim gameInfo As New gameInfoClass
                    
                    gameInfo.localPlayerID = reader("localplayer_id")
                    gameInfo.opponentPlayerID = reader("opponent_id")
                    gameInfo.opponentName = reader("opponent_name")
                    gameInfo.gameScore = reader("game_score")

                    output.gamesList.Add(gameInfo)                     

                Loop

                sqlConnection1.Close()

            Catch ex As Exception

                output.status = False

                output.errorMessage = "Database Error"

            End Try

        End If


        'Writing the output to JSON format
        Dim outputJSON As String
        outputJSON = Newtonsoft.Json.JsonConvert.SerializeObject(output)

        'Printing the JSON in the screen
        Response.Clear()
        Response.Write(outputJSON)
        Response.ContentType = "application/json"
        Response.End()

    End Sub


End Class

So, an example of the usage of the code above in your app would be:
local params = {}
params.playerID = "3435434"
params.opponentID = "122343"

local gamesList = getJSONFromServer("http://www.mywebsite.com/getgames.aspx", params)

Some Important Information:
  • You need to add the Newtonsoft.JSon reference to your VB.net site. You can do that by going in Tools->"Add Library Package Reference" and installing the "Newtonsoft.JSON" in your Visual Studio / Visual Web developer IDE.
  • The code above is a mix of some codes that we use in our games and I did not test. If you find any difficult in running, please let me know and I can help you. Maybe I made a mistake when putting together the code
  • I highly recommend you to use a https server so you data in encrypted during communication between your device and your server

The code above use GET params and sometimes GET is not the best method to pass the params (Ex: Specially if you have a large amount of data to pass). In that case, you would want to pass the params via POST. The code with POST params will be almost the same but you would have to make the following modifications:


1) Delete these lines in the app code:
local linkComplete = serverURL .. "?" .. paramToString(paramsToBePassed)
        
    -- Makes the request to the server
    local requestResult, requestResultCode = http.request{url = linkComplete, sink = ltn12.sink.table(jsonData), headers = {accept = "application/json"} }
2) Add these lines in the app code in the same place of the lines above:
local body = paramToString(paramsToBePassed)
    
    local headers = {
        accept = "application/json", 
        ["Content-Type"] = "application/x-www-form-urlencoded",
        ["Content-Length"] = string.len(body),
        ["Accept-Language"] = "en-US"        
        }

    -- Downloading a specific page of popular actors
    local requestResult, requestResultCode = http.request{url = serverURL  sink = ltn12.sink.table(jsonData), method = "POST", headers = headers, source = ltn12.source.string(body) }
3) Delete these lines in the Server Code
      'Retrieving the GET params
        Dim localPlayerID As String = Page.Request.QueryString("playerID")     
        Dim opponentPlayerID As String = Page.Request.QueryString("opponentID")
4) Add these lines in the Server Code in the same place of the lines above
      'Retrieving the POST params
        Dim localPlayerID As String = Page.Request.Form("playerID")
        Dim opponentPlayerID As String = Page.Request.Form("opponentID")

Hope that this example helps you in developing your server.

See you.
1 Comment
magento developer link
8/29/2013 08:45:39 pm

All functions of this script are well made

Reply



Leave a Reply.

    Author

    This blog it not updated anymore.  

    Archives

    February 2015
    June 2014
    May 2014
    April 2014
    March 2014
    January 2014
    December 2013
    November 2013
    October 2013
    September 2013
    August 2013
    July 2013
    May 2013
    April 2013
    March 2013
    February 2013
    January 2013
    December 2012
    November 2012
    October 2012
    September 2012
    August 2012
    July 2012

    Categories

    All
    Data
    Event
    Fun
    Games
    Key Success Factors
    Management
    Marketing
    Mobile
    Monetization
    Movies
    Startup
    Statistics
    Technical
    Tutorial
    Videos

Powered by Create your own unique website with customizable templates.