Class RelayService

java.lang.Object
com.bitheads.braincloud.services.RelayService

public class RelayService
extends Object
  • Field Details

  • Constructor Details

  • Method Details

    • connect

      public void connect​(RelayConnectionType connectionType, org.json.JSONObject options, IRelayConnectCallback callback)
      Start a connection, based on connection type to brainClouds Relay Servers. Connect options come in from ROOM_ASSIGNED lobby callback.
      Parameters:
      connectionType - The connection type. WEBSOCKET, TCP, UDP
      options - { ssl: false, host: "168.0.1.192" port: 9000, passcode: "somePasscode", lobbyId: "55555:v5v:001" }
      callback - Callback objects that report Success or Failure|Disconnect. Note SSL option will only work with WEBSOCKET connetion type.
    • disconnect

      public void disconnect()
      Disconnects from the relay server
    • endMatch

      public void endMatch​(org.json.JSONObject json)
      Terminate the match instance by the owner.
      Parameters:
      json - Payload data sent in JSON format. It will be relayed to other connnected players
    • isConnected

      public boolean isConnected()
      Returns whether or not we have a successful connection with the relay server
      Returns:
      Whether or not we have a successful connection with the relay server
    • getPing

      public int getPing()
      Get the current ping for our user. Note: Pings are not distributed among other members. Your game will have to bundle it inside a packet and distribute to other peers.
      Returns:
      The current ping
    • setPingInterval

      public void setPingInterval​(int intervalMS)
      Set the ping interval. Ping allows to keep the connection alive, but also inform the player of his current ping. The default is 1000 miliseconds interval. (1 seconds)
      Parameters:
      intervalMS - Seconds between pings.
    • getOwnerProfileId

      public String getOwnerProfileId()
      Get the lobby's owner profile Id.
      Returns:
      The lobby owner's profile Id
    • getProfileIdForNetId

      public String getProfileIdForNetId​(int netId)
      Returns the profileId associated with a netId.
      Parameters:
      netId - The netId the profileId is associated with
      Returns:
      The profileId associated with a netId
    • getNetIdForProfileId

      public int getNetIdForProfileId​(String profileId)
      Returns the netId associated with a profileId.
      Parameters:
      profileId - The profileId the netId is associated with
      Returns:
      The netId associated with a profileId
    • getOwnerCxId

      public String getOwnerCxId()
      Get the lobby's owner connection Id.
      Returns:
      The lobby owner's connection Id
    • getCxIdForNetId

      public String getCxIdForNetId​(int netId)
      Returns the connection id associated with a netId.
      Parameters:
      netId - The netId the connection id is associated with
      Returns:
      The connection id associated with a netId
    • getNetIdForCxId

      public int getNetIdForCxId​(String cxId)
      Returns the netId associated with a connection id.
      Parameters:
      cxId - The connection id the netId is associated with
      Returns:
      the netId associated with a connection id
    • registerRelayCallback

      public void registerRelayCallback​(IRelayCallback callback)
      Register callback for relay messages coming from peers.
      Parameters:
      callback - Called whenever a relay message was received.
    • deregisterRelayCallback

      public void deregisterRelayCallback()
      Deregisters callback for relay messages coming from peers.
    • registerSystemCallback

      public void registerSystemCallback​(IRelaySystemCallback callback)
      Register callback for RelayServer system messages.
      Parameters:
      callback - Called whenever a system message was received. function(json) # CONNECT Received when a new member connects to the server. { op: "CONNECT", profileId: "...", ownerId: "...", netId: # } # NET_ID Receive the Net Id assossiated with a profile Id. This is sent for each already connected members once you successfully connected. { op: "NET_ID", profileId: "...", netId: # } # DISCONNECT Received when a member disconnects from the server. { op: "DISCONNECT", profileId: "..." } # MIGRATE_OWNER If the owner left or never connected in a timely manner, the relay-server will migrate the role to the next member with the best ping. If no one else is currently connected yet, it will be transferred to the next member in the lobby members' list. This last scenario can only occur if the owner connected first, then quickly disconnected. Leaving only unconnected lobby members. { op: "MIGRATE_OWNER", profileId: "..." }
    • deregisterSystemCallback

      public void deregisterSystemCallback()
      Deregister callback for RelayServer system messages.
    • send

      public void send​(byte[] data, long toNetId, boolean reliable, boolean ordered, int channel)
      Send a packet to peer(s)
      Parameters:
      data - Byte array for the data to send
      toNetId - The net id to send to, TO_ALL_PLAYERS to relay to all.
      reliable - Send this reliable or not.
      ordered - Receive this ordered or not.
      channel - One of: (CHANNEL_HIGH_PRIORITY_1, CHANNEL_HIGH_PRIORITY_2, CHANNEL_NORMAL_PRIORITY, CHANNEL_LOW_PRIORITY)
    • sendToPlayers

      public void sendToPlayers​(byte[] data, long playerMask, boolean reliable, boolean ordered, int channel)
      Send a packet to any players by using a mask
      Parameters:
      data - Byte array for the data to send
      playerMask - Mask of the players to send to. 0001 is netId 0, 0010 is netId 1, etc. If you pass ALL_PLAYER_MASK you will be included and you will get an echo for your message. Use sendToAll instead, you will be filtered out. You can manually filter out by : ALL_PLAYER_MASK &= ~(1 << myNetId)
      reliable - Send this reliable or not.
      ordered - Receive this ordered or not.
      channel - One of: (CHANNEL_HIGH_PRIORITY_1, CHANNEL_HIGH_PRIORITY_2, CHANNEL_NORMAL_PRIORITY, CHANNEL_LOW_PRIORITY)
    • sendToAll

      public void sendToAll​(byte[] data, boolean reliable, boolean ordered, int channel)
      Send a packet to all except yourself
      Parameters:
      data - Byte array for the data to send
      reliable - Send this reliable or not.
      ordered - Receive this ordered or not.
      channel - One of: (CHANNEL_HIGH_PRIORITY_1, CHANNEL_HIGH_PRIORITY_2, CHANNEL_NORMAL_PRIORITY, CHANNEL_LOW_PRIORITY)