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 Details

    • Client

      public Client​(String url)
      Creates a new Client instance
      Parameters:
      url - - the URL of the server that the client will connect to
  • Method Details

    • setErrorLogger

      public void setErrorLogger​(ErrorLogger errorLogger)
      Sets the Error logger that will be used to log errors by this Client
      Parameters:
      errorLogger - - the error logger used for logging errors
    • registerWithEmailId

      public void registerWithEmailId​(String emailId, String password, String name, String gameName)
      Attempts to Register a new user with emailId on the Game Server
      Parameters:
      emailId - - the user emailId
      password - - the user password
      name - - the user name
      gameName - - the game that will be linked with the client session on successful user registration - can be null
    • registerWithOtherId

      public void registerWithOtherId​(String otherId, String password, String name, String gameName)
      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 mobileNo
      password - - the user password
      name - - the user name
      gameName - - the game that will be linked with the client session on successful user registration - can be null
    • logInWithEmailId

      public void logInWithEmailId​(String emailId, String password, String gameName)
      Attempts to Login a user onto server with emailId and starts a new session
      Parameters:
      emailId - - the user emailId
      password - - the user password
      gameName - - the game which should be linked with the client session on successful login - can be null
    • logInWithGoogle

      public void 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. 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 google
      gameName - - the game which should be linked with the client session on successful login - can be null
    • logInWithOtherId

      public void logInWithOtherId​(String otherId, String password, String gameName)
      Attempts to Login a user onto server with otherId and starts a client session
      Parameters:
      otherId - - the user otherId
      password - - the user password
      gameName - - the game which should be linked with the client session on successful login - can be null
    • enterGame

      public void enterGame​(String gameName)
      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

      public void sendJsonMessage​(String command, JsonObject data)
      Attempts to Send a json message to the server
      Parameters:
      command - - the message command
      data - - the message data as json
    • sendTxtMessage

      public void sendTxtMessage​(String command, String data)
      Attempts to Send a text message to the server
      Parameters:
      command - - the message command
      data - - the message data as string
    • sendBinaryMessage

      public void sendBinaryMessage​(String command, byte[] data)
      Attempts to sends a binary message to the server
      Parameters:
      command - - the message command
      data - - 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

      public void getRooms​(String location, RoomType roomType)
      Fetches the list of rooms from the server
      Parameters:
      location - - the location whose rooms should be fetched
      roomType - - 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

      public void takeSeat​(long roomId, int seatNo, JsonObject data)
      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 join
      seatNo - - the seat number to sit on - starting from 1
      data - - 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

      public void setClientEventListener​(ClientEventListener clientEventListener)
      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

      public String 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

      public String getSessionId()
      Returns the Client sessionId
      Returns:
      - Client sessionId
    • getName

      public String 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

      public String 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

      public String getEmailId()
      Returns the emailId of the User loggedIn to the client session
      Returns:
      - the emailId of the User loggedIn to the client session
    • getOtherId

      public String getOtherId()
      Returns the otherId of the User loggedIn to the client session
      Returns:
      the otherId of the User loggedIn to the client session
    • getGameName

      public String 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

      public JsonObject 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

      public LoginType 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

      public Room 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

      public Player 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 GamePlayNotInProgressException
      Attempts to Play an action. Sends a playAction message to the Server.
      Parameters:
      action - - the action played
      actionData - - 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