How to Send & Receive Data

For the remaining integration steps, it is recommended that you have already developed a simple game template that requires a small amount of data to be exchanged. Otherwise, follow along by exchanging some small sample data. Here’s how you can send data using the sendData function:

Step 1: sendData

Here’s how you can send data using the sendData function.

Pass your data to a call to the static method, SkillzSync.sendData(), as a byte array (byte[]) to be sent to our Skillz servers which will be forwarded to the opponent’s device. Here’s an example of how we could send a simple String object back and forth:

Example:

content_sync_game.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

       <EditText
           android:id="@+id/editText"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_margin="10dp"/>

       <Button
           android:id="@+id/sendMessageButton"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_margin="10dp"
           android:text="@string/send_message" />

</LinearLayout


GameActivity.java:


public class GameActivity extends SkillzActivity {
    ...
    @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.content_sync_game);

       SkillzSync.registerSyncDelegate(this, this);

       EditText editText = (EditText) findViewById(R.id.editText);
       View sendMessageButton = findViewById(R.id.sendMessageButton);
       sendMessageButton.setOnClickListener( new View.OnClickListener() {
          @Override
          public void onClick(View v) {
             String msg = editText.getText().toString()
             byte[] data = msg.getBytes();
             SkillzSync.sendData(data);
          }
       });
       ...
    }
    ...
}

Note: The bytes passed to sendData above will be sent to the Sync game server and forwarded to your opponent’s onDidReceiveData implementation:

public class GameActivity extends SkillzActivity {
    ...
    @Override
    public void onDidReceiveData(@NonNull byte[] data) {
       Log.d(“MyActivity”, “I received a message! ⇒ ” + new String(data));
    }
    ...
}
 

Tips & Information:

  1. SkillzSync game server currently provides support for 2 player games.
  2. onDidReceiveData and ALL other SyncDelegate methods are called on a background thread. Any UI displayed directly within these methods will not render to the screen without being moved to the Main Thread first:

@Override
public void onDidReceiveData(@NonNull byte[] data) {
    final String msg = new String(data);
    runOnUiThread(new Runnable() {
       @Override
       public void run() {
       Toast.makeText(this, "I received a message! ⇒ " + msg, Toast.LENGTH_SHORT).show();
       }
    });
}

Make sure to pass in all the data that’s needed to ensure that the game will stay in sync on both devices. The size limit is 2 KB.

Recommended: For the data you send, have an object type for each type of message you send so that you can easily handle the different types of data when it reaches the other device. JSON serialization will work, but is likely not the most performant way to send data.

Recommended: Handle finishing states for the match in SkillzSync.onMatchCompleted and SkillzSync.onDidReceiveData.

Note: Reliability is guaranteed through disconnects, but the system maintains a cap on data stored in this state. Reliability is only guaranteed up to a certain point when your opponent is disconnected. As soon as you receive notification that a player’s  opponent has been disconnected, you should stop attempting to send messages in order to ensure no data is lost. Also, if you find you are sending more than 30 messages a second, you may find that some messages could be lost on disconnect/reconnect and game state may need to be reconciled on reconnection.

 

Step 2: onDidReceiveData 

Here is how you can receive data using the onDidReceiveData function. This function needs to accept a byte array (byte[]) and is called whenever it receives data.

Example:

public class GameActivity extends SkillzActivity {
    ...
    @Override
    public void onDidReceiveData(@NonNull byte[] data) {
       Log.d(“MyActivity”, “I received a message! ⇒ ” + new String(data));
       verifyWinnerAndReportScore();
    }
    ...
    @Override
    public void onMatchCompleted() {
       verifyWinnerAndReportScore();
    }
}

Next Step: Handle Game States