Android app with sockets

Discussion in 'Plugin Development' started by koesie10, Oct 25, 2013.

Thread Status:
Not open for further replies.
  1. I'm developing an Android app in which you should be able to manage Bukkit servers. I'm doing this with JSONAPI. My plan is to do this using sockets so I have a persistent connection and I don't have to make HTTP request every time. This also makes it easier to add chat, performance, etc.

    I haven't done very much with sockets/threads, so I need some help with this. I have no idea on how to implement an interface to the server. I would like to have some methods like:
    Code:java
    1. public static interface BukkitApi {
    2. public void subscribe(String method, SubscriptionHandler handler);
    3.  
    4. public void call(String method, CallHandler handler);
    5. }
    6.  
    7. public static interface SubscriptionHandler {
    8. public void onConnect();
    9. // This method will be called many times
    10. public void onReceive(JSONObject response);
    11.  
    12. public void onDisconnect();
    13. }
    14.  
    15. public static interface CallHandler {
    16. // This method will be called only once
    17. public void onSuccess(JSONObject response);
    18.  
    19. public void onFailure();
    20. }


    My idea is to generate tags and based on the tags to call the right handler. I think this should work but I'm stuck on implementing this. My progress so far is here.

    Could anyone suggest me how to do it?
     
  2. Offline

    The_Doctor_123

    I'm not all that familiar with this JSONAPI stuff, but don't they handle all the data for you? Why would you have a Socket involved?

    Also, if you don't mind me asking, what does the JSONAPI even do to help?
     
  3. JSONAPI is a plugin that I use to communicate with the server. I am not using it in my app, I just would like to communicate with it. Iwill be using the default org.json library to use JSON on Android.

    There are three ways to communicate with JSONAPI. One of them is HTTP, the other is sockets and the last one is WebSockets. I would like to use sockets so I can receive chat and console messages via one socket. There is documentation on this on their wiki. Unfortunately some are outdated and using the old version 1 API, but as you can see here I've already figured out how to use the stream API with the new version 2 API.
     
  4. Offline

    The_Doctor_123

    koesie10
    So basically, it's a more secure data stream?
     
  5. It's not exactly more secure, I think in this case it is easier to use a socket than HTTP. HTTP is a one-time connection while sockets are long-running connections to which data can also be send by the server to which it is connected.

    In can for example ask for a one-time response. I can for example get the player count and it will respond with the player count. This can also be done with HTTP. But you have a problem when trying to receive messages from the server. With only HTTP I cannot receive messages from the server, for this you would need polling. With sockets the server can write to you as well, so that is possible. This means I can subscribe to chat events and every time when someone chats on the server, I will get a response from the server.

    I hope this makes it a little clearer:
    Code:java
    1. // to what I want to subscribe, in this case the chat stream
    2. String source = "chat";
    3. // connect to the socket server
    4. socket = new Socket(server.getIp(), server.getPort()+1);
    5.  
    6. // out stream
    7. .getOutputStream())), true);
    8. // in stream
    9. BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    10.  
    11. // create an object which contains the subscribe data
    12. JSONObject object = new JSONObject();
    13. // name = source
    14. object.put("name", source);
    15. // key is generated by the server class
    16. object.put("key", server.getKey(source));
    17. // username is needed
    18. object.put("username", server.getUsername());
    19. // tag to track subscriptions, this should be generated so we can keep track of handlers etc.
    20. object.put("tag", source);
    21. // whether to receive the past 50 messages
    22. object.put("show_previous", true);
    23.  
    24. // the string that should be written to the server
    25. // this subscribes to the stream in JSONAPI
    26. String outString = "/api/2/subscribe?json=" + URLEncoder.encode(object.toString(), "UTF-8");
    27.  
    28. // print (send) it to the server
    29. out.println(outString);
    30.  
    31. String inputLine;
    32. while ((inputLine = in.readLine()) != null) {
    33. // receive a message back
    34. // this will be called on every message send by JSONAPI
    35. Log.i(TAG, "Received " + inputLine);
    36. }
     
  6. Offline

    whitehooder

    I've used sockets in C++, but I don't know how in Java :/ I'll play around and do my searches for a while and post back if I find anything useful :)
     
  7. Offline

    The_Doctor_123

    koesie10
    Meh.. I'd prefer that long-term connection. I don't think it's really necessary that you have this API in your plugin and you should just do it the normal way.
     
  8. I also prefer a long-term connection and that are sockets.

    I don't have any time right now, but I'll try a few things tomorrow and post back when I find something new/solved the problem. I can also try to use a Java socket library, but I'll look into that tomorrow.
     
  9. Offline

    NathanWolf

  10. I've already successfully completed this tutorial and sockets are also working. The main problem is that I have to handle writes and reads at the same time and that I have to post them back to handlers of which I all have to keep track. The next step is then of course stability, which means reconnecting etc., and that will probably be a complex part.
     
  11. Offline

    The_Doctor_123

    Well, you're going to have to have multiple Threads. On the client, one Thread should always listen for incoming data and another does all the other stuff. On the server, there will be a Thread with a ServerSocket listening for incoming connections and another Thread for the other stuff, like the client.
     
  12. I'm sorry that I have not been clear enough before, but I'm not going to write a plugin. I will use an already existing plugin called JSONAPI and that has all the features that I want to implement. That means I have the main thread in the Android app and also another thread that handles the Socket. I will try to develop something tomorrow and I hope that will make it clearer both for me and for you.
     
Thread Status:
Not open for further replies.

Share This Page