SmartfoxServer and SqlServer With Example Programs
- Step 1:- Configure the databaseManager under the zone in order to establish the communication between extension and database.
- Step 2:- Open ZoneConfiguration
using AdminPanel (default port is 8080 see this link to know
adminpanel port ) and configure the database
tab as
- Step 3:- In the above Database manager tab we need to fill the fields, initially those are empty or filled with the data available in .zone.xml file.
- Step 4:- Fill the Fields with these
FieldName Description Activate enable it Database Driver Class com.microsoft.sqlserver.jdbc.SQLServerDriver ConnectionString jdbc:sqlserver://host:port;databasename=ourdatabasename
(jdbc:sqlserver://127.0.0.1:1433;databasename=tutorialtous)Username root Password Password for the root user Test SQL select getdate() Max Active Connections 10 Max Idle Connections 10 Exhausted Pool Connections GROW (is prefered) - step 6 :- Follow This Step Only If you are unable to open ADMIN PANEL
Open the concerned .zone.xml file in any text editor. In SFS2X folder we will have a 'zones' folder in that our zone file will be there open it in any text editor (SFS2x->zones->CasinoGameServer.zone.xml).
Find/Search the tag <databaseManager> and edit like this (sample SqlServer driver details are mentioned here fill with your db related entries there, for more about tags description check this link
<databaseManager active="true"> <driverName>com.microsoft.sqlserver.jdbc.SQLServerDriver</driverName> <connectionString>jdbc:sqlserver://127.0.0.1:1433;SelectMethod=direct;databasename=tutorialtous</connectionString> <userName>root</userName> <password>www.tutorialtous.com</password> <testSql>select getdate()</testSql> <maxActiveConnections>10</maxActiveConnections> <maxIdleConnections>10</maxIdleConnections> <exhaustedPoolAction>FAIL</exhaustedPoolAction> <blockTime>3000</blockTime> </databaseManager>
- Step 7:- Press the submit and reload buttons
- Step 8:- Load the SqlServer-Jdbc Jar File in the sfs2x’s lib
folder or __ lib folder.
Download the SqlServer-jdbc jar file from either of these links SqlServer Dev JDBC Connector Official Link or from this link. - Step 7:- Assuming I have a class named "LoginRequestHandler" to handle the custom login in that I am writing the code as like this
/** * @author tutorialtous.com */ package com.tutorialtous.smartfoxserver; import java.sql.SQLException; import com.smartfoxserver.bitswarm.sessions.ISession; import com.smartfoxserver.v2.core.ISFSEvent; import com.smartfoxserver.v2.core.SFSEventParam; import com.smartfoxserver.v2.db.IDBManager; import com.smartfoxserver.v2.entities.User; import com.smartfoxserver.v2.entities.data.ISFSArray; import com.smartfoxserver.v2.entities.data.ISFSObject; import com.smartfoxserver.v2.exceptions.SFSErrorCode; import com.smartfoxserver.v2.exceptions.SFSErrorData; import com.smartfoxserver.v2.exceptions.SFSException; import com.smartfoxserver.v2.exceptions.SFSLoginException; import com.smartfoxserver.v2.extensions.BaseServerEventHandler; public class LoginRequestHandler extends BaseServerEventHandler { @Override public void handleServerEvent(ISFSEvent event) throws SFSException { User user = (User) event.getParameter(SFSEventParam.USER); ISession session = user.getSession(); String username = user.getName(); String pwd = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD); SFSErrorData errdata = null; String dbpwd = null; dbpwd = getPasswordFromDb(username); if (dbpwd != null) { if (getApi().checkSecurePassword(session, dbpwd, pwd)) { trace("login success for user " + username); } else { errdata = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD); } } else { errdata = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME); } if (errdata != null) { throw new SFSLoginException("Login Failed",errdata); } } private String getPasswordFromDb(String username) { String password = null; try { IDBManager dbm = getParentExtension().getParentZone() .getDBManager(); ISFSArray resultset = dbm.executeQuery( "select pwd from users where userid=?", new Object[] { username }); if (resultset != null) { ISFSObject obj = resultset.getSFSObject(0); password = obj.getUtfString("pwd"); } } catch (SQLException e) { e.printStackTrace(); } return password; } }
- Step 9:- Create the jar file and restart the Server. (Check this link for jar file creation and exporting to SmartfoxServer)
- Step 10:- Simple Client side code for the above Login Activity (Java client)
/** * @author smartfoxserver@tutorialtous.com * How to Run:- * Step1:- javac MainClass.java * step2:- java MainClass */ package com.avishkarclient; import java.util.Map; import sfs2x.client.SmartFox; import sfs2x.client.core.BaseEvent; import sfs2x.client.core.IEventListener; import sfs2x.client.core.SFSEvent; import sfs2x.client.requests.LoginRequest; import com.smartfoxserver.v2.exceptions.SFSException; class SmartfoxJavaClient implements IEventListener { private SmartFox sfs; public SmartfoxJavaClient() { sfs = new SmartFox(); sfs.addEventListener(SFSEvent.CONNECTION, this); sfs.addEventListener(SFSEvent.CONNECTION_LOST, this); sfs.addEventListener(SFSEvent.LOGIN, this); sfs.addEventListener(SFSEvent.LOGIN_ERROR, this); } public void connectToServer() { sfs.connect("smartfoxserver.tutorialtous.com", 9934); } public void sendLoginRequest(String username, String password, String zonename) { LoginRequest req = new LoginRequest(username, password, zonename); if (sfs.isConnected()) { sfs.send(req); System.out.println("LoginRequest Sent to Server"); } else { System.out .println("Please Connect to server to send the LoginRequest"); } } public void dispatch(BaseEvent event) throws SFSException { Map params = event.getArguments(); switch (event.getType()) { case SFSEvent.CONNECTION: if ((Boolean) params.get("success")) { System.out.println("Connected to server"); } else { System.out.println("Failed to connect server"); } break; case SFSEvent.LOGIN: System.out.println("Login Success"); break; case SFSEvent.LOGIN_ERROR: System.out.println("Login Failed"); break; } } } public class MainClass { public static void main(String args[]) { SmartfoxJavaClient obj = new SmartfoxJavaClient(); obj.connectToServer(); obj.sendLoginRequest("player", "123456", "CasinoGameServer"); } }