Package io.sockit.gameclient
Class Client
java.lang.Object
io.sockit.gameclient.Client
public class Client extends Object
This class represents a Client interacting with the server. It has methods to login a User to the Server, enter a game, fetch list of rooms, join a room, etc. See startup code example below
// instantiate a client with the server url
Client client = new Client("ws://localhost");
// register a ClientEventListener (event/callback handler) with the client
client.setClientEventListener(new TicTacToeClientListener());
// register user and login to the server with emailId, password, user name and game name as parameters
client.registerWithEmailId("a@a.com", "123", "Rohan", "TicTacToe");
-
Constructor Summary
-
Method Summary
Modifier and Type Method Description void
changeAvtarId(int avtarId)
Attempts to Change the avtarId of user loggedin to the sessionvoid
disconnect()
Disconnects this Client from the servervoid
enterGame(String gameName)
Attempts to Link a Game with the client sessionvoid
exitGame()
Attempts to Delink the currently linked game from the client sessionint
getAvtarId()
Returns the avtarId of the User loggedIn to the client sessionString
getEmailId()
Returns the emailId of the User loggedIn to the client sessionString
getGameName()
returns the Name of the game that this client session is linked toJsonObject
getGameUserData()
Returns the Game Specific User Data of the User loggedIn to the client sessionRoom
getJoinedRoom()
Returns the room joined by this client or null if no room has been joinedvoid
getLocations()
Fetches the list of game locations from the serverLoginType
getLoginType()
Returns the LoginType of the Client session - i.e.String
getName()
Returns the name of the User loggedIn to the client sessionString
getOtherId()
Returns the otherId of the User loggedIn to the client sessionPlayer
getPlayer()
Returns the player object associated with this Client or null if the Client is not seatedString
getProfilePic()
Returns the URL of the profile picture of the User loggedIn to the client sessionvoid
getRooms(String location, RoomType roomType)
Fetches the list of rooms from the serverString
getSessionId()
Returns the Client sessionIdString
getUserId()
Returns the user ID of the user loggedIn to the client session.boolean
hasJoinedRoom()
Checks if Client has joined a room.boolean
isActivePlayer()
Checks if this Client is an active player.boolean
isConnected()
Checks if this Client is connected to the game server or notboolean
isCurTurn()
Returns true if the current turn belongs to this Client.boolean
isGamePlayInProgress()
Checks if Game play is in progress.boolean
isLoggedIn()
Returns whether this Client is loggedIn to the server or notboolean
isOwnerOfJoinedRoom()
Checks if the User loggedIn via this Client owns the joined room or not.boolean
isSeated()
Checks if this Client is seated.boolean
isSpectator()
Checks if Client is a spectator.void
joinRoom(long roomId)
Attempts to Make the client session join the specified room.void
leaveRoom()
Attempts to leave the room.void
leaveSeat()
Attempts to leave the seat in the room.void
logInWithEmailId(String emailId, String password, String gameName)
Attempts to Login a user onto server with emailId and starts a new sessionvoid
logInWithGoogle(String idToken, String gameName)
Attempts to Login a user onto server with google's oAuth JWT (json web token) and starts a client session.void
logInWithOtherId(String otherId, String password, String gameName)
Attempts to Login a user onto server with otherId and starts a client sessionvoid
logOut()
Attempts to Logout the user from the server and end the client sessionvoid
playAction(String action, JsonObject actionData)
Attempts to Play an action.void
refreshRoomFromServer()
Attempts to refresh the room state(data) from the servervoid
registerWithEmailId(String emailId, String password, String name, String gameName)
Attempts to Register a new user with emailId on the Game Servervoid
registerWithOtherId(String otherId, String password, String name, String gameName)
Attempts to Register a new user with otherId on the Game Servervoid
rejoinSession()
Attempts to rejoin this Client's session if the session is still alive on the servervoid
sendBinaryMessage(String command, byte[] data)
Attempts to sends a binary message to the servervoid
sendJsonMessage(String command, JsonObject data)
Attempts to Send a json message to the servervoid
sendTxtMessage(String command, String data)
Attempts to Send a text message to the servervoid
setClientEventListener(ClientEventListener clientEventListener)
Sets the ClientEventListener to which the client's events/callbacks will be forwardedvoid
setErrorLogger(ErrorLogger errorLogger)
Sets the Error logger that will be used to log errors by this Clientvoid
takeSeat(long roomId, int seatNo, JsonObject data)
Attempts to take a seat in the room.
-
Constructor Details
-
Client
Creates a new Client instance- Parameters:
url
- - the URL of the server that the client will connect to
-
-
Method Details
-
setErrorLogger
Sets the Error logger that will be used to log errors by this Client- Parameters:
errorLogger
- - the error logger used for logging errors
-
registerWithEmailId
Attempts to Register a new user with emailId on the Game Server- Parameters:
emailId
- - the user emailIdpassword
- - the user passwordname
- - the user namegameName
- - the game that will be linked with the client session on successful user registration - can be null
-
registerWithOtherId
Attempts to Register a new user with otherId on the Game Server- Parameters:
otherId
- - the user otherId (which is not an email), such as otherId or mobileNopassword
- - the user passwordname
- - the user namegameName
- - the game that will be linked with the client session on successful user registration - can be null
-
logInWithEmailId
Attempts to Login a user onto server with emailId and starts a new session- Parameters:
emailId
- - the user emailIdpassword
- - the user passwordgameName
- - the game which should be linked with the client session on successful login - can be null
-
logInWithGoogle
Attempts to Login a user onto server with google's oAuth JWT (json web token) and starts a client session. Note it is the job of the game developer to add google oAuth capability to the game client.- Parameters:
idToken
- - The jsonWebToken sent by google on successful authentication by googlegameName
- - the game which should be linked with the client session on successful login - can be null
-
logInWithOtherId
Attempts to Login a user onto server with otherId and starts a client session- Parameters:
otherId
- - the user otherIdpassword
- - the user passwordgameName
- - the game which should be linked with the client session on successful login - can be null
-
enterGame
Attempts to Link a Game with the client session- Parameters:
gameName
- - the name of the game to which the session should be linked
-
exitGame
public void exitGame()Attempts to Delink the currently linked game from the client session -
isConnected
public boolean isConnected()Checks if this Client is connected to the game server or not- Returns:
- - true if this Client is connected to the game server
-
disconnect
public void disconnect()Disconnects this Client from the server -
sendJsonMessage
Attempts to Send a json message to the server- Parameters:
command
- - the message commanddata
- - the message data as json
-
sendTxtMessage
Attempts to Send a text message to the server- Parameters:
command
- - the message commanddata
- - the message data as string
-
sendBinaryMessage
Attempts to sends a binary message to the server- Parameters:
command
- - the message commanddata
- - the message data as a byte array
-
changeAvtarId
public void changeAvtarId(int avtarId)Attempts to Change the avtarId of user loggedin to the session- Parameters:
avtarId
- - the new avtar ID
-
logOut
public void logOut()Attempts to Logout the user from the server and end the client session -
getLocations
public void getLocations()Fetches the list of game locations from the server -
getRooms
Fetches the list of rooms from the server- Parameters:
location
- - the location whose rooms should be fetchedroomType
- - the room type - normal or fast
-
joinRoom
public void joinRoom(long roomId)Attempts to Make the client session join the specified room.- Parameters:
roomId
- - the roomId of the room to join
-
refreshRoomFromServer
public void refreshRoomFromServer()Attempts to refresh the room state(data) from the server -
takeSeat
Attempts to take a seat in the room. Until the client is seated he/she remains a spectator and cannot take part in the game play. The seat numbers are numbered 1 to n.- Parameters:
roomId
- - the roomId of the room to joinseatNo
- - the seat number to sit on - starting from 1data
- - additional data sent along with the take seat command to the server. For eg. in Poker this could be the chips to put on table
-
leaveSeat
public void leaveSeat()Attempts to leave the seat in the room. -
leaveRoom
public void leaveRoom()Attempts to leave the room. -
setClientEventListener
Sets the ClientEventListener to which the client's events/callbacks will be forwarded- Parameters:
clientEventListener
- - the ClientEventListener that will handle the client's events/callbacks
-
getUserId
Returns the user ID of the user loggedIn to the client session. The userId uniquely identifies a user registered on the GameServer- Returns:
- - the user ID of the user loggedIn to the client session
-
getSessionId
Returns the Client sessionId- Returns:
- - Client sessionId
-
getName
Returns the name of the User loggedIn to the client session- Returns:
- - the name of the User loggedIn to the client session
-
getAvtarId
public int getAvtarId()Returns the avtarId of the User loggedIn to the client session- Returns:
- - the avtarId of the User loggedIn to the client session
-
getProfilePic
Returns the URL of the profile picture of the User loggedIn to the client session- Returns:
- - the URL of the profile picture of the User loggedIn to the client session
-
getEmailId
Returns the emailId of the User loggedIn to the client session- Returns:
- - the emailId of the User loggedIn to the client session
-
getOtherId
Returns the otherId of the User loggedIn to the client session- Returns:
- the otherId of the User loggedIn to the client session
-
getGameName
returns the Name of the game that this client session is linked to- Returns:
- - the Name of the game that this client session is linked to
-
getGameUserData
Returns the Game Specific User Data of the User loggedIn to the client session- Returns:
- - the Game Specific User Data of the User loggedIn to the client session
-
isLoggedIn
public boolean isLoggedIn()Returns whether this Client is loggedIn to the server or not- Returns:
- - true if this Client is loggedIn to the server
-
getLoginType
Returns the LoginType of the Client session - i.e. whether Client loddeIn with emailId or otherId or google- Returns:
- - the LoginType of the Client session - i.e. whether Client loddeIn with emailId or otherId or google
-
getJoinedRoom
Returns the room joined by this client or null if no room has been joined- Returns:
- - the room joined by this client
-
hasJoinedRoom
public boolean hasJoinedRoom()Checks if Client has joined a room. Returns true if this Client has joined a room- Returns:
- - true if this Client has joined a room
-
isSpectator
public boolean isSpectator()Checks if Client is a spectator. Returns true if this Client has joined a room and is a spectator- Returns:
- - true if this Client has joined a room and is a spectator
-
isSeated
public boolean isSeated()Checks if this Client is seated. Returns true if this Client is seated in a room.- Returns:
- - true if this Client is seated in a room.
-
isGamePlayInProgress
public boolean isGamePlayInProgress()Checks if Game play is in progress. Returns true if this Client has joined a room and Game play is in progress in the room.- Returns:
- - true if this Client has joined a room and Game play is in progress in the room.
-
isActivePlayer
public boolean isActivePlayer()Checks if this Client is an active player. Returns true if this Client is seated in a room and is taking part in the current game play in the room..- Returns:
- - true if this Client is seated in a room and is taking part in the current game play in the room..
-
getPlayer
Returns the player object associated with this Client or null if the Client is not seated- Returns:
- - the player object associated with this Client or null if the Client is not seated
-
isCurTurn
public boolean isCurTurn()Returns true if the current turn belongs to this Client.- Returns:
- - true if the current turn belongs to this Client.
-
playAction
public final void playAction(String action, JsonObject actionData) throws GamePlayNotInProgressExceptionAttempts to Play an action. Sends a playAction message to the Server.- Parameters:
action
- - the action playedactionData
- - the data associated with the action- Throws:
GamePlayNotInProgressException
- - if GamePlay is not in progress
-
isOwnerOfJoinedRoom
public boolean isOwnerOfJoinedRoom()Checks if the User loggedIn via this Client owns the joined room or not. Returns true if the User loggedIn via this Client owns the joined room- Returns:
- - true if the User loggedIn via this Client owns the joined room
-
rejoinSession
public void rejoinSession()Attempts to rejoin this Client's session if the session is still alive on the server
-