Package com.bitheads.braincloud.services
Class RelayService
java.lang.Object
com.bitheads.braincloud.services.RelayService
public class RelayService extends Object
-
Field Summary
Fields Modifier and Type Field Description static intCHANNEL_HIGH_PRIORITY_1static intCHANNEL_HIGH_PRIORITY_2static intCHANNEL_LOW_PRIORITYstatic intCHANNEL_NORMAL_PRIORITYstatic intINVALID_NET_IDstatic longTO_ALL_PLAYERS -
Constructor Summary
Constructors Constructor Description RelayService(BrainCloudClient client) -
Method Summary
Modifier and Type Method Description voidconnect(RelayConnectionType connectionType, org.json.JSONObject options, IRelayConnectCallback callback)Start a connection, based on connection type to brainClouds Relay Servers.voidderegisterRelayCallback()Deregisters callback for relay messages coming from peers.voidderegisterSystemCallback()Deregister callback for RelayServer system messages.voiddisconnect()Disconnects from the relay servervoidendMatch(org.json.JSONObject json)Terminate the match instance by the owner.StringgetCxIdForNetId(int netId)Returns the connection id associated with a netId.intgetNetIdForCxId(String cxId)Returns the netId associated with a connection id.intgetNetIdForProfileId(String profileId)Returns the netId associated with a profileId.StringgetOwnerCxId()Get the lobby's owner connection Id.StringgetOwnerProfileId()Get the lobby's owner profile Id.intgetPing()Get the current ping for our user.StringgetProfileIdForNetId(int netId)Returns the profileId associated with a netId.booleanisConnected()Returns whether or not we have a successful connection with the relay servervoidregisterRelayCallback(IRelayCallback callback)Register callback for relay messages coming from peers.voidregisterSystemCallback(IRelaySystemCallback callback)Register callback for RelayServer system messages.voidsend(byte[] data, long toNetId, boolean reliable, boolean ordered, int channel)Send a packet to peer(s)voidsendToAll(byte[] data, boolean reliable, boolean ordered, int channel)Send a packet to all except yourselfvoidsendToPlayers(byte[] data, long playerMask, boolean reliable, boolean ordered, int channel)Send a packet to any players by using a maskvoidsetPingInterval(int intervalMS)Set the ping interval.
-
Field Details
-
TO_ALL_PLAYERS
public static final long TO_ALL_PLAYERS- See Also:
- Constant Field Values
-
CHANNEL_HIGH_PRIORITY_1
public static final int CHANNEL_HIGH_PRIORITY_1- See Also:
- Constant Field Values
-
CHANNEL_HIGH_PRIORITY_2
public static final int CHANNEL_HIGH_PRIORITY_2- See Also:
- Constant Field Values
-
CHANNEL_NORMAL_PRIORITY
public static final int CHANNEL_NORMAL_PRIORITY- See Also:
- Constant Field Values
-
CHANNEL_LOW_PRIORITY
public static final int CHANNEL_LOW_PRIORITY- See Also:
- Constant Field Values
-
INVALID_NET_ID
public static final int INVALID_NET_ID- See Also:
- Constant Field Values
-
-
Constructor Details
-
RelayService
-
-
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, UDPoptions- { 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
Get the lobby's owner profile Id.- Returns:
- The lobby owner's profile Id
-
getProfileIdForNetId
Returns the profileId associated with a netId.- Parameters:
netId- The netId the profileId is associated with- Returns:
- The profileId associated with a netId
-
getNetIdForProfileId
Returns the netId associated with a profileId.- Parameters:
profileId- The profileId the netId is associated with- Returns:
- The netId associated with a profileId
-
getOwnerCxId
Get the lobby's owner connection Id.- Returns:
- The lobby owner's connection Id
-
getCxIdForNetId
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
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
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
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 sendtoNetId- 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 sendplayerMask- 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 sendreliable- 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)
-