Giter Club home page Giter Club logo

timesup's Introduction

TimesUp

How to send & receive messages from the server

  1. Make sure your class extends ServerIOActivity
  • ... extends ServerIOActivity instead of ... extends AppCompatActivity
  • You will now have to implement the callback(DecodeMessage message) function
    • This is where you will receive a reply from the server once it is available
    • Check out the protocol file section in the "RECEIVING" section to see what type of response you get
    • If you want to access one of the 4 header values simply "get it"
      • Example you want to get the "returnType": message.getReturnType() returns the returnType
    • If you want to access something in the body of the message you have to "get its type".
      • Example you want to get a String array value with name "wordList": message.getStringArray("wordList") returns the wordList as a String array
      • Example you want to get a String value with name "activePlayer": message.getString("activePlayer") returns the activePlayer value as a String array
  1. Change the callback activity to the current activity
  • setCallbackActivity(this);
    • setCallbackActivity() gets inhertied from ServerIOActivity
  1. Send encoded messages to the server
  • sendMessage(encodeMessage)
    • sendMessage() get inherited from ServerIOActivity
  • Here encodedMessage is an object of class EncodeMessage which is basically the counterpart of DecodeMessage
  • Again check the protocol to see what kind of things it is used for (this time the "SENDING" part)
  • You can simply put all the data you need to send into the constructor of the EncodeMessage, the rest will be handled by the implementation of the constructor.

Example of joining a game & receiving the response

public class SomeActivit extends ServerIOActivity{

...
//Some vars
..

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      setCallbackActivity(this);
      
      ...
      //Some code
      int gameId = ... //get those values through shared prefs 
      int clientId = ...
      int teamId = ...
      ...
      
      
      EncodeMessage messageToSend = new EncodeMessage(gameId, clientId, teamId);
      sendMessage(messageToSend);
      
    }
    
    public void callback(DecodeMessage message){
      ...
      //Handle response here
      ...
    }
}

timesup's People

Contributors

anklinv avatar david-flurin avatar hadjimina avatar jkfrie avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.