Solved websockets how to sent data back?

Discussion in 'Plugin Development' started by xize, Jul 10, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    xize

    Hello,

    so I'm trying to make my own http socket since jetty is pretty big for just a single callback system for greylisting.

    but whenever I run my own variant, it displays a normal page rather than a json file I think ive used the DataOutputStream incorrectly, could someone explain me how to sent it back to the client?, its my first time making sockets:p

    my whole class is, of course its looks a bit messy and maybe also redurant though:

    Code:java
    1.  
    2. public class GreyListServer implements Runnable {
    3.  
    4. /*
    5. this is a example how the greylist works through php, we can use the json which gets returned by jetty in this way.
    6. currently we have 3 possible return types:
    7.  
    8. success = when the player change from group,
    9. greylisted = when the player already is greylisted
    10. notexist = if the player has never played before
    11. php callback example:
    12. <?php
    13. $string = file_get_contents("[url]http://127.0.0.1:8001/adduser/Xeph0re[/url]");
    14. $json = json_decode($string, true);
    15. $args = $json["xEssentials"]["response"];
    16. if($args == "success") {
    17. echo "player has been promoted";
    18. } else if($args == "greylisted") {
    19. echo "player already is greylisted";
    20. } else if($args == "notexist") {
    21. echo "player has never played before";
    22. }
    23. ?>
    24. */
    25.  
    26. private final int port;
    27. private ServerSocket server;
    28. private Thread thread;
    29.  
    30. public GreyListServer(int port) {
    31. this.port = port;
    32. }
    33.  
    34. public boolean isRunning() {
    35. if(!(thread instanceof Thread)) {
    36. return false;
    37. } else {
    38. return thread.isAlive();
    39. }
    40. }
    41.  
    42. @SuppressWarnings({ "deprecation" })
    43. public void stop() {
    44. if(isRunning()) {
    45. try {
    46. server.close();
    47. } catch (IOException e) {
    48. // TODO Auto-generated catch block
    49. e.printStackTrace();
    50. }
    51. thread.stop();
    52. }
    53. }
    54.  
    55. public void start() {
    56. if(!(thread instanceof Thread)) {
    57. try {
    58. this.server = new ServerSocket(port);
    59. } catch (IOException e) {
    60. // TODO Auto-generated catch block
    61. e.printStackTrace();
    62. }
    63. this.thread = new Thread(this);
    64. this.thread.start();
    65. }
    66. }
    67. @Override
    68. public void run() {
    69. while(true) {
    70. try {
    71. Socket client = server.accept();
    72.  
    73. String uri;
    74. String line;
    75. String user;
    76.  
    77. InputStream stream = client.getInputStream();
    78. BufferedReader reader = new BufferedReader(iread);
    79. DataOutputStream response = new DataOutputStream(client.getOutputStream());
    80.  
    81. while((line = reader.readLine()) != null) {
    82. System.out.print("line: " + line);
    83. if(line.startsWith("GET".toUpperCase())) {
    84. System.out.print("found get: " + line);
    85. //we've found the request, now we need to validate that request, and sent a json back in return.
    86. //if the method is not returned at all then we return a 404 and disconnect the client.
    87.  
    88. uri = line.replace("GET ", "").replace(" HTTP/1.1", "");
    89.  
    90. if(uri.startsWith("/adduser/") && uri.length() > "/adduser/".length()) {
    91. //valid url lets go futher to check if this is a player.
    92.  
    93. response.writeBytes("HTTP/1.1 200 OK\r\n");
    94.  
    95. user = uri.substring("/adduser/".length());
    96.  
    97. System.out.print("user: " + user);
    98. System.out.print("url is correct.");
    99.  
    100. if(xEssentials.isEssentialsPlayer(user)) {
    101. //user is valid.
    102. response.writeBytes("Content-Type: application/json");
    103.  
    104. xEssentialsOfflinePlayer off = xEssentials.getOfflinePlayer(user);
    105. if(!off.isGreyListed()) {
    106. //user is not greylisted before.
    107. response.writeBytes("{\"xEssentials\": {\"response\": \"success\"}}");
    108. off.setGreyListed(true);
    109. if(off.getPlayer() instanceof Player) {
    110. off.getPlayer().sendMessage(ChatColor.GREEN + "you are successfully promoted to " + Configuration.getGrayListConfig().getGroup());
    111. if(Hooks.isVaultEnabled()) {
    112. String oldGroup = xEssentials.getPlugin().getVault().getGroup(Bukkit.getWorlds().get(0), user);
    113. String newgroup = Configuration.getGrayListConfig().getGroup();
    114. xEssentials.getPlugin().getVault().setGroup(Bukkit.getWorlds().get(0), off.getUser(), newgroup);
    115. Bukkit.getPluginManager().callEvent(new PlayerGreyListedEvent(off.getPlayer(), newgroup, oldGroup, GreyListCause.SITE));
    116. }
    117. } else {
    118. if(Hooks.isVaultEnabled()) {
    119. String oldGroup = xEssentials.getPlugin().getVault().getGroup(Bukkit.getWorlds().get(0), user);
    120. String newgroup = Configuration.getGrayListConfig().getGroup();
    121. xEssentials.getPlugin().getVault().setGroup(Bukkit.getWorlds().get(0), off.getUser(), newgroup);
    122. Bukkit.getPluginManager().callEvent(new PlayerGreyListedEvent(off.getPlayer(), newgroup, oldGroup, GreyListCause.SITE));
    123. }
    124. }
    125. } else {
    126. //user already was greylisted
    127. response.writeBytes("{\"xEssentials\": {\"response\": \"greylisted\"}}");
    128. }
    129. } else {
    130. //user is invalid and has never played before.
    131. response.writeBytes("{\"xEssentials\": {\"response\": \"notexist\"}}");
    132. }
    133. } else {
    134. //invalid url.
    135. response.writeBytes("HTTP/1.1 404 Not Found");
    136. }
    137.  
    138. reader.close();
    139. iread.close();
    140. stream.close();
    141. client.close();
    142. return;
    143. }
    144. }
    145. } catch (IOException e) {
    146. e.printStackTrace();
    147. }
    148.  
    149. }
    150. }
    151. }
    152.  


    I think ive forgot a few important things here, all advise and help are welcome:)


    thanks!

    -edit-
    by alot of testing it seems I had to flush the DataOutputStream a few times and by means don't forget to flush when being done creating the headers they are seperated by an enter and then you need to flush again and then you can futher with the content and you need to flush again.
     
Thread Status:
Not open for further replies.

Share This Page