1.15.2 How do I link my plugin to a web page?

Discussion in 'Spigot Plugin Development' started by Nosmoze, Apr 4, 2020.

  1. Hello, as said in the title I would like to know that it is the easiest way to have a bidirectional relationship between my plugin and a web page. I would like that when a player executes an order he receives a personalized link to reach a personalized page.

    While doing research, I saw several times websockets appear but it did not correspond to what I wanted or then I did not find good documentation. So I hope someone here can point me to furoms documentation or discussion. I don't have a code to show you yet.
     
  2. Strahan

    Benefactor

    Websockets would be a logical way to go. How does that not correspond to what you want? User on website clicks buy on something, webserver initiates socket connection to MC and tells listener to give item X to player Y.
     
  3. I can't figure out how to link. And I can't figure out how to communicate in java and the web. Which language to use? JS? PHP?
     
  4. Strahan

    Benefactor

    What do you mean, how to link? Link the server and website? The language for the website is whatever you are comfortable with. Personally, I like PHP but it can be any language that supports socket connections. There are other ways besides sockets as well, I only mention that because that's the only one I've had experience with. Do some Googling.
     
  5. I want to link the server and the site
     
  6. I definitely would say that google is your friend. With processes like this it is best to start at the design phase with a map of what you want everything to do at each point, Freeplane is an awesome tool for this mapping process but paper and pen is fine.

    What I understand you want is that when you execute a command on the server, it generates a webpage for the sender to go to in order to complete the order. A very basic flow would be as follows:

    1) Plugin starts -> create connection to website (WebSocket is a really useful tool and does really well)
    2) Player executes commands -> generate web page (this can be done one of two ways either create the page locally in whatever language and send it to the web server to process or have a page that is dynamic that just fills with the information the plugin sends)
    3) Get url to generated page and send to player
    4) Some action occurs on the page -> send packet back to plugin
    5) Plugin is listening for packet and does stuff based on packet.
     
  7. You are mixing some things up here.
    You are not creating a connection with a website (as this is normally a static / dynamic document) - you are creating it to the web(socket)-server.
    The web-server then takes your A) generated web-page content or B) your informations to the application-server, which contains the defined logic for those requests / actions. After processing it in one way or another and possibly storing informations for the upcomming session of that player, it sends back a result to the web-server which hands it back to your minecraft server to deal with it - it could for example return an error code that's needs to be dealt with.
    When the player received their working link by your plugin, they can then click on it and will land on the specifically for them generated page.
    They can click stuff, fill out stuff, do whatever and send an action / request to the web-server on their own (since they are on the web-page now), which will go through the web-server again, to the application-server, triggering a response there (could be an error for example if something was wrong) and possibly sends a notification through the web-servers socket to the minecraft-server to let it know, that the player has done the specified action.

    Yes this is long, but this is how it (should) work in reality. Assuming that you A) don't expose the application-server directly to the web (please don't do that) and B) that your application-server is not also the web-server at the same time.
     
  8. What the plugin should do:

    1) plugin starts -> opens a link to the website
    2) the player executes a command -> a page is generated
    3) depending on what the player does, the plugin sends information to the player page
    4) The player can interact with the page (from the start) which will have consequences in the game
    6) When the player disconnects from the minecraft server, the page is also cut
    7) For each request, the page url is unique
    8) When the server is turned off, all connections close as well as the link
     
  9. Basically how the technical setup for my plugin OpenAudio works, feel free to checkout the github repo to see how it works on both the web end as well as the java

    Here's a diagram on how all of it sticks together if that helps you out
    [​IMG]

    You can ignore the voice encoders, only the session router, session controller and relay server are relevant for what you are trying to build. You can easily incorporate them into one server or have the WebSocket connection managed by your plugin using something like Netty, in that case your plugin does basically everything
     
  10. I had already taken a look at your github for openaudio but I did not understand much about the operation. I don't know Netty. What is its function ?
     
  11. If I understood correctly for what I want to do I have to use websocket and Netty? Is the data encrypted or should I use Gson?
     
  12. What do you want to do? Did you really need to use WebSockets? Can you use a database?

    In order to help you better, can you explain me with an example of what you want as a result?
     
  13. I already explained what I wanted, I have no example of an existing plugin. It's like a panel for the player. The player can manage an attraction. Open / close the harnesses. Basically when he clicks on a button on the site, it makes the player execute an order. There is not everything but it is one of the thing that the plugin will have to do
     
  14. If you really need a realtime update, you can use WebSockets but maybe that's not the best solution. Maybe you can create just a queue list of operations and periodically performing them.

    Writing a WebSocket server was simple, it's like a chat. You send and receive messages.

    PHP was for backend development, it works at the web server side, not the browser client side. You have to use Javascript at the website to connect with the Minecraft server or any other intermediator.
    https://developer.mozilla.org/en-US/docs/Web/API/WebSocket

    I suggest you create the website uncoupled from the spigot server. And create a system to generate a "room" for that user/operation/game and working just the room-id. That in order to avoid long massive links with a lot of information and security issues, but you can do it as you want. You can use a room creation system or sending a lot of params at the URL to work with a lot of variables when the user opens the link.

    Then, at the website, with WebSockets, you can send messages like:
    Code (Text):
    {room: "room-id-here", operation: "enable-night-vision"}
    Then, at the spigot server-side, just process that string to get the operation and performing it.

    I suggested you creating rooms on the spigot server-side to avoid security problems, for example, sending the username instead of that room-id. Everyone can manually send a message to the WebSocket, that will means your system is gone be hacked easy.
     
  15. The problem with php is that every time i want to change a variable, i also have to change the link. Unless there is a way to make a dynamic page
     
  16. The link you sent me is for JS but for java how should I do?
     
  17. Here's a link on how to use WebSockets in Java.

    ... We're happy to give you a guiding hand as to where to start, but we won't write the code for you. That's your job. Also, there are numerous libraries available for both the server and client aspects of a WebSocket connection. Use your resources, search for them.
     
  18. I don't want code, I just want to understand. I searched but I couldn't find. Maybe i searched wrong
     
    • Agree Agree x 1
  19. A WebSocket is a bi-directional protocol used to communicate data between a server and a multitude of clients. Data is sent as a "message" and is received, parsed, and verified by both parties. That's all there is to it.
     
  20. You have to build two programs, one for the server and one for the client.

    For the server, you can use Java or anther language as an intermediate, but in the end it has to communicate with your Java spigot plugin. The main goal of this program will be listening for new connections. It's a WebSocket server.

    For the client, you will need to write a JS script for running it on the browser side, executed by the user who will join the website, not your server. That script will be a WebSocket client and it has to connect to the previously created Java WebSocket server.

    Then, start the message exchange. What more doubts do you have? How can I help you?