Mysql Implementation

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Mysql Implementation

Postby azaroth » 01 Jun 2013, 09:40

Hey again,

im doing some testing on the game and i need to communicate with a local mysql database. How can i do it? I mean download and setup mysql locally.
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Mysql Implementation

Postby Lapo » 01 Jun 2013, 17:38

Installing MySQL is out of the scope of our support, but you can find all the directions from the mysql website.
In order to communicate with the database via SmartFoxServer read the documentation here:
http://docs2x.smartfoxserver.com/Gettin ... wtos#item2

Then you can take a look at these "recipes":
http://docs2x.smartfoxserver.com/Develo ... se-recipes

And this too:
http://docs2x.smartfoxserver.com/Develo ... -assistant

cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Re: Mysql Implementation

Postby azaroth » 03 Jun 2013, 09:40

Thank you, i will check it
azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Re: Mysql Implementation

Postby azaroth » 03 Jun 2013, 16:35

Ok, some problems here,

i used the MySQL JDBC Driver but i dont know where to drop the file. SFS2X\extensions\__lib__ or SFS2X\lib?

how can i find the database driver class and the connection string (what port)?
User avatar
xLite
Posts: 106
Joined: 05 May 2010, 12:42

Re: Mysql Implementation

Postby xLite » 04 Jun 2013, 00:18

SFS2X\lib

driver class = org.gjt.mm.mysql.Driver

connection string = jdbc:mysql://host/dbname

* change host and dbname to the actual host and database name

More info: here
azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Re: Mysql Implementation

Postby azaroth » 04 Jun 2013, 07:21

Thank you, i installed wampserver for the mysql and it uses 127.0.0.1 (cauz localhost is for sfs) and i put jdbc:mysql://127.0.0.2:3306/mydb but it says cant access "root@localhost". So it seems it still tries to connect to 127.0.0.1 instead of .2

I also want to send a query via c# script so i use

IDBManager connection = dbManager.getConnection();

but the system doesn't recognize the IDBManager class. Do i have to import a certain sfs class?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Mysql Implementation

Postby Lapo » 04 Jun 2013, 09:34

Queries cannot be executed from client side, only from server side.
You will have to send a request from the client to your server side Extension, which in turn will talk to your database.

If you're new to these concepts take it one step at a time from the documentation, from the Development Basics section.
http://docs2x.smartfoxserver.com/
Lapo

--

gotoAndPlay()

...addicted to flash games
azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Re: Mysql Implementation

Postby azaroth » 04 Jun 2013, 10:26

Thank you again, and what of the other problem?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Mysql Implementation

Postby Lapo » 04 Jun 2013, 13:38

I don't understand the other problem.
i installed wampserver for the mysql and it uses 127.0.0.1 (cauz localhost is for sfs)

no localhost is another way of saying 127.0.0.1, they are the same thing actually, only the first is a domain name and the second is the actual IP address where the domain resolves to.

and i put jdbc:mysql://127.0.0.2:3306/mydb but it says cant access "root@localhost". So it seems it still tries to connect to 127.0.0.1 instead of .2

If the database runs on 127.0.0.1 you should use 127.0.0.1, not .2
Lapo

--

gotoAndPlay()

...addicted to flash games
azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Re: Mysql Implementation

Postby azaroth » 04 Jun 2013, 15:45

No, database runs on .2 but no matter what ip i use on admin tool, i get 'root@localhost'. Another question though. I created an extension with 2 classes:

Code: Select all

package mysql;

import com.smartfoxserver.v2.extensions.SFSExtension;

public class Main extends SFSExtension {

   public void init() {
      
      addRequestHandler("PickUp", PickUp.class);
      
   }
   
}


Code: Select all

package mysql;
import java.sql.SQLException;

import com.smartfoxserver.v2.db.IDBManager;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.extensions.BaseClientRequestHandler;

public class PickUp extends BaseClientRequestHandler{

   public void handleClientRequest(User sender, ISFSObject params){
      
      IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
      String sql = "myquery";
         try {
         dbManager.executeQuery(sql);
      } catch (SQLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
      
   }
   
}



I get those two:

Image
Image
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Mysql Implementation

Postby Lapo » 05 Jun 2013, 08:21

In both your screenshots it's not possible to read the error message, it is truncated.
Can you please copy it and paste it here?

Also I'd like to see the settings of you DBManager in the AdminTool

thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Re: Mysql Implementation

Postby azaroth » 08 Jun 2013, 11:19

Hello sorry for late to answer. I fixed the problem, but i need help with custom login.

I followed this script:

http://docs2x.smartfoxserver.com/Gettin ... wtos#item3

but it only says what happens in case of wrong credentials. If they are correct what happpens?
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: Mysql Implementation

Postby rjgtav » 08 Jun 2013, 18:43

Hello,

If the credentials are correct, then no exception is thrown and some events are fired: SFSEvent.LOGIN on the client and SFSEventType.USER_JOIN_ZONE on the server-side. After an User is successfully logged in, it can interact with the server: send custom extension requests, join a room, chat with buddies, chat with other users, play games, etc.

For more information about how the server architecture works, I suggest you to read the [url)http://docs2x.smartfoxserver.com/DevelopmentBasics/introduction]Development Basics[/url] section of the official documentation.

Cheers
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
azaroth
Posts: 45
Joined: 21 Dec 2012, 09:23

Re: Mysql Implementation

Postby azaroth » 09 Jun 2013, 07:55

Hey, thank you, i did it with a fake-custom login. But i have another question :D

When someone registers the extension checks if the username or the mail he entered already exist. So it uses this code:

Code: Select all


      ISFSArray check = dbManager.executeQuery("SELECT * FROM player WHERE username = ?", new Object[] {params.getUtfString("Username")});
      ISFSArray check2 = dbManager.executeQuery("SELECT * FROM player WHERE email = ?", new Object[] {params.getUtfString("E-Mail")});
      
      //for(int i = 0; i <= check.size()-1; i++){
      
      ISFSObject Credential = check.getSFSObject(0);
      ISFSObject Credential2 = check2.getSFSObject(0);
     
                //do something

                }



My question is what do the arrays contain of they don't find a match. If they find one the access index is 0 right?
User avatar
Lapo
Site Admin
Posts: 23026
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Mysql Implementation

Postby Lapo » 09 Jun 2013, 08:34

Just a little side note. We provide a specific class called LoginAssistant which makes this much more easy, have you taken a look here?
http://docs2x.smartfoxserver.com/Develo ... -assistant

In your code you're running 2 queries when you actually need just one,

Code: Select all

"SELECT * FROM player WHERE username = ? AND email = ?"


My question is what do the arrays contain of they don't find a match. If they find one the access index is 0 right?

The array contains as many rows of data as your query produced. If your query didn't find anything the size of the array will be zero.

Code: Select all

if (returnArray.size() > 0)
{
  // Ok, the query did return data
}
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 114 guests