today i have configured all things to use smartfox server with Unity 3D, because we're creating a battle royale game.
I have wrote this handler to do a login from client, reading my datas on table.
Code: Select all
package mainScreen_package;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.smartfoxserver.bitswarm.sessions.ISession;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSConstants;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.db.IDBManager;
//import com.smartfoxserver.v2.entities.User;
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 LoginEventHandler extends BaseServerEventHandler {
@Override
public void handleServerEvent(ISFSEvent event) throws SFSException {
trace("I'm asking to the server to signup a user");
// Grab parameters from client request
String userName = (String) event.getParameter(SFSEventParam.LOGIN_NAME);
String cryptedPass = (String) event.getParameter(SFSEventParam.LOGIN_PASSWORD);
//String email = (String)event.getParameter(SFSEventParam.LOGIN_PASSWORD);
ISession session = (ISession) event.getParameter(SFSEventParam.SESSION);
trace("Username----------" + userName);
trace("Password----------" + cryptedPass);
trace("Session----------" + session);
ISFSObject outData = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA);
// ISFSObject outData2 = (ISFSObject) event.getParameter(SFSEventParam.LOGIN_OUT_DATA);
//User user = null;
// Add data to the object
// Get password from DB
IDBManager dbManager = getParentExtension().getParentZone().getDBManager();
Connection connection = null;
// Grab a connection from the DBManager connection pool
try {
connection = dbManager.getConnection();
// Build a prepared statement
PreparedStatement stmt = connection.prepareStatement("SELECT ID_User, Username, "
+ "Password, Email , mkoin , profile_img "
+ "FROM [dbo].[Downfall_users] "
+ "where Username='"+userName+"' or Email ='"+userName+"'");
// Execute query
ResultSet res = stmt.executeQuery();
while(res.next())
{
int id_user = res.getInt("id_user");
trace(id_user);
String username = res.getString("username");
trace(username);
String email = res.getString("Email");
trace(email);
int mkoin = res.getInt("mkoin");
trace(mkoin);
String profile_img = res.getString("profile_img");
if(profile_img == null){
profile_img = "";
}
trace(profile_img);
outData.putInt("id_user", id_user);
outData.putUtfString("nome_utente", username);
outData.putUtfString("email", email);
outData.putInt("mkoin", mkoin);
outData.putUtfString("profile_img", profile_img);
outData.putUtfString(SFSConstants.NEW_LOGIN_NAME, username);
}
// Verify that one record was found
if (!res.first())
{
// This is the part that goes to the client
SFSErrorData errData = new SFSErrorData(SFSErrorCode.LOGIN_BAD_USERNAME);
errData.addParameter(userName);
trace("mi sono intoppato qui???");
// Sends response if user gave incorrect user name
throw new SFSLoginException("Bad user name: " + userName, errData);
}
String dbPword = res.getString("password");
// Verify the secure password
if (!getApi().checkSecurePassword(session, dbPword, cryptedPass))
{
trace("mi sono intoppato dopo???");
SFSErrorData data = new SFSErrorData(SFSErrorCode.LOGIN_BAD_PASSWORD);
data.addParameter(userName);
// Sends response if user gave incorrect password
throw new SFSLoginException("Login failed for user: " + userName, data);
}
res.close();
stmt.close();
}
// User name was not found
catch (SQLException e)
{
SFSErrorData errData = new SFSErrorData(SFSErrorCode.GENERIC_ERROR);
errData.addParameter("SQL Error: " + e.getMessage());
trace(" mi sono intoppato quiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii???");
// Sends response about mysql errors
throw new SFSLoginException("A SQL Error occurred: " + e.getMessage(), errData);
}
finally {
try{
connection.close();
}catch (SQLException e){
throw new SFSLoginException("A SQL Error occurred: " + e.getMessage());
}
}
}
}
and onclient code i do this
Code: Select all
void Start()
{
//See if there isn't SFS connection active
if (sfs == null || !sfs.IsConnected)
{
Debug.Log("Connecting to the Server.....");
//Inizialize SFS variable
sfs = new SmartFox();
// Set ThreadSafeMode explicitly, or Windows Store builds will get a wrong default value (false)
sfs.ThreadSafeMode = true;
sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
// Set other call after connection event
sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnRoomJoin);
sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnRoomJoinError);
sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
sfs.AddLogListener(LogLevel.INFO, OnInfoMessage);
sfs.AddLogListener(LogLevel.WARN, OnWarnMessage);
sfs.AddLogListener(LogLevel.ERROR, OnErrorMessage);
// Set connection parameters
ConfigData cfg = new ConfigData();
cfg.Host = defaultHost;
cfg.Port = defaultTcpPort;
cfg.Zone = "DownFall - The Lost City";
// Connect to SFS2X
sfs.Connect(cfg);
}else
{
sfs.Disconnect();
}
}
public void LoadLevel (int sceneIndex)
{
////See if there isn't SFS connection active
//if (sfs == null || !sfs.IsConnected)
//{
// Debug.Log("Connecting to the Server.....");
// //Inizialize SFS variable
// sfs = new SmartFox();
// // Set ThreadSafeMode explicitly, or Windows Store builds will get a wrong default value (false)
// sfs.ThreadSafeMode = true;
// sfs.AddEventListener(SFSEvent.CONNECTION, OnConnection);
// sfs.AddEventListener(SFSEvent.CONNECTION_LOST, OnConnectionLost);
// // Set other call after connection event
// sfs.AddEventListener(SFSEvent.LOGIN, OnLogin);
// sfs.AddEventListener(SFSEvent.LOGIN_ERROR, OnLoginError);
// sfs.AddEventListener(SFSEvent.ROOM_JOIN, OnRoomJoin);
// sfs.AddEventListener(SFSEvent.ROOM_JOIN_ERROR, OnRoomJoinError);
// sfs.AddEventListener(SFSEvent.PUBLIC_MESSAGE, OnPublicMessage);
// sfs.AddLogListener(LogLevel.INFO, OnInfoMessage);
// sfs.AddLogListener(LogLevel.WARN, OnWarnMessage);
// sfs.AddLogListener(LogLevel.ERROR, OnErrorMessage);
// // Set connection parameters
// ConfigData cfg = new ConfigData();
// cfg.Host = defaultHost;
// cfg.Port = defaultTcpPort;
// cfg.Zone = "DownFall - The Lost City";
// // Connect to SFS2X
// sfs.Connect(cfg);
//Login request
GameObject UsernameGameObject = GameObject.Find("Canvas/TopMenu/UserField");
InputField inputUsername = UsernameGameObject.GetComponent<InputField>();
Debug.Log("Captured Username : " + inputUsername.text);
GameObject PasswordGameObject = GameObject.Find("Canvas/TopMenu/PasswordField");
InputField inputPassword = PasswordGameObject.GetComponent<InputField>();
Debug.Log("Captured Password : " + inputPassword.text);
string username = inputUsername.text;
string password = inputPassword.text;
sfs.Send(new LoginRequest(username, password, "DownFall - The Lost City"));
StartCoroutine(LoadAsynchronously(sceneIndex));
}
but i have an error, on the log i can see my result set but the extension at the end goes on the catch.
the error is this
Code: Select all
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
>> Zone: DownFall - The Lost City
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
26 giu 2018 | 22:13:02,435 | INFO | main | entities.managers.SFSRoomManager | | Room created: { Zone: DownFall - The Lost City }, [ Room: Lobby, Id: 1, Group: default, isGame: false ], type = SFSRoom
26 giu 2018 | 22:13:02,445 | INFO | main | Extensions | | {Downfall_server}: Hello everybody... this is the first extension of DownFall - The Lost City
26 giu 2018 | 22:13:02,451 | INFO | main | Extensions | | {Downfall_server}: This Extension work to do Login Procedure from DB(SQL Server)
26 giu 2018 | 22:13:02,455 | INFO | main | entities.managers.SFSRoomManager | | Room created: { Zone: --=={{{ AdminZone }}}==-- }, [ Room: AdminRoom, Id: 2, Group: default, isGame: false ], type = SFSRoom
26 giu 2018 | 22:13:02,635 | INFO | main | v2.core.AdminToolService | | AdminTool Service started
26 giu 2018 | 22:13:02,952 | INFO | SFSWorker:Sys:1 | smartfoxserver.v2.SmartFoxServer | | Listening Sockets: { 0.0.0.0:9933, (Tcp) } { 127.0.0.1:9933, (Udp) }
26 giu 2018 | 22:13:02,958 | INFO | SFSWorker:Sys:1 | smartfoxserver.v2.SmartFoxServer | |
_____ _____ _____ ___ __ __
| __| __| __| |_ | | |
|__ | __|__ | | _|- -|
|_____|__| |_____| |___|__|__|
_____ _____ _____ ____ __ __
| __ | __| _ | \| | |
| -| __| | | |_ _|
|__|__|_____|__|__|____/ |_|
[ 2.13.0 ]
26 giu 2018 | 22:13:02,968 | INFO | SFSWorker:Sys:1 | smartfoxserver.v2.SmartFoxServer | | SmartFoxServer 2X (2.13.0) READY!
26 giu 2018 | 22:13:06,185 | INFO | main | bluebox.v3.SessionFilter | | BlueBox-2X Service (3.1.0) READY.
26 giu 2018 | 22:13:18,502 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session created: { Id: 1, Type: DEFAULT, Logged: No, IP: 127.0.0.1:50884 } on Server port: 9933 <---> 50884
26 giu 2018 | 22:13:38,826 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: I'm asking to the server to signup a user
26 giu 2018 | 22:13:38,850 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: Username----------PippoBUG
26 giu 2018 | 22:13:38,853 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: Password----------e31b72f0699a19290868c7fcb95fb0d5
26 giu 2018 | 22:13:38,868 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: Session----------{ Id: 1, Type: DEFAULT, Logged: No, IP: 127.0.0.1:50884 }
26 giu 2018 | 22:13:39,149 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: 1
26 giu 2018 | 22:13:39,157 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: PippoBUG
26 giu 2018 | 22:13:39,168 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: pippobug@gmail.com
26 giu 2018 | 22:13:39,181 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: 0
26 giu 2018 | 22:13:39,308 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: abcd
26 giu 2018 | 22:13:39,311 | INFO | SFSWorker:Ext:4 | Extensions | | {Downfall_server}: mi sono intoppato quiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii???
26 giu 2018 | 22:13:39,314 | WARN | SFSWorker:Ext:4 | entities.managers.SFSExtensionManager | | com.smartfoxserver.v2.exceptions.SFSLoginException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: com.smartfoxserver.v2.exceptions.SFSLoginException
Message: A SQL Error occurred: L'operazione richiesta non è supportata sui set di risultati forward-only.
Description: Extension's Login Handler exception
+--- --- ---+
Stack Trace:
+--- --- ---+
mainScreen_package.LoginEventHandler.handleServerEvent(LoginEventHandler.java:142)
com.smartfoxserver.v2.extensions.SFSExtension.handleServerEvent(SFSExtension.java:259)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchEvent(SFSExtensionManager.java:768)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchZoneLevelEvent(SFSExtensionManager.java:689)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.handleServerEvent(SFSExtensionManager.java:887)
com.smartfoxserver.v2.core.SFSEventManager$SFSEventRunner.run(SFSEventManager.java:65)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
26 giu 2018 | 22:13:39,431 | INFO | SocketReader | bitswarm.sessions.DefaultSessionManager | | Session removed: { Id: 1, Type: DEFAULT, Logged: No, IP: 127.0.0.1:50884 }
how can i solve??
i'm using sql server 2017 developer edition