3

I have found quite a lot of other posts on this topic but none seem to have the answer I need.
I have written a Bukkit plugin for Minecraft that can send post data to a PHP page and get a return from the page.

Now the one thing I can't figure out. I would like to have a button on the page, and when the button is clicked, send data to the Java plugin and have the plugin print the message.

I have seen something about sockets. But after reading about them I can't figure out how to set them up.
Pretty much at any time you should be able to click the button and it sends data to the Java plugin and I can use that data however I like.

Does anyone know how I can have the Java plugin constantly waiting for data from the page?

My current code:
(This sends the players name to the website.)

String re = "";  
URL url = new URL("address here");  
URLConnection con = url.openConnection();  
con.setDoOutput(true);  
PrintStream ps = new PrintStream(con.getOutputStream());  
ps.print("player=" + player.getName());  
con.getInputStream();  
BufferedReader rd = new BufferedReader(new InputStreamReader(con.getInputStream()));  
String line;  
while ((line = rd.readLine()) != null) {  
   re += line + "\n";  
}  
rd.close();  
ps.close();

And my php just returns any post data it gets.
It works fine, but I would like to listen in my java plugin for data from the php page.

spongebob
  • 8,370
  • 15
  • 50
  • 83
Parker Jones
  • 83
  • 2
  • 10

2 Answers2

1

There are many ways to make communication between two servers. I'd use one of them:

  • Sockets
  • JMS - Java Message Service such as ActiveMQ

Both of them have tutorials available, just google.

Aron
  • 61
  • 1
  • 2
1

You could use a database, or setup a json/xml api on the PHP end, and access the database, or access the json/xml from Java with this example code to open the url.

URL url = new URL("site.com/api/foo.json");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
}

You can look at this tutorial to parse JSON with Java.

Un3qual
  • 1,332
  • 15
  • 27
  • So you say I should write to a file with Java and read it with PHP and write a file with PHP and read it with Java? I think that would only work one way because the PHP can't access files where the Java is running. – Parker Jones Jan 04 '15 at 03:11
  • @ParkerJones Generate the `JSON` from a database in PHP, and read it from Java. Write from Java using an api in PHP. It's pretty complicated, but doable. The simplest way is to use a database. – Un3qual Jan 04 '15 at 05:14