0

I started my first javafx GUI game using the MVC pattern and I would like to implement a multiplayer feature where multiple people could join and sort of play a death match mode. I looked up online and for the network part I thought of using a TCP connexion (with java.net.Socket and java.net.ServerSocket). What would be send to the server for each client is their state (number of lives) and the server would notify each client of all players lives.

I'm really unsure on how I could connect the client server part with my javafx MVC. Here are the classes in my main folder :

-> main

---> App.java : main loader

---> GameController.java : fetch/modify data from Model and update the View

---> Model.java : class that only encapsulates data such as lives

I know that each of my Client here would have an associated Thread so what exactly is a "client" in this case and where should I put the server at ?

Chae
  • 1
  • 1
  • It looks like you're using "Model" to mean "Presentation Model", but in MVC it really means all of your game logic, domain objects and the presentation model. You should put your connectivity stuff into a "Service" and then access it through the MVC Model. I'd suggest that you look at my articles here: https://www.pragmaticcoding.ca/javafx/mvci/ . The actual nature of your sockets or whatever should be hidden from your MVC construct via the service that you build. – DaveB Jan 01 '23 at 18:41
  • Here is an example of [using sockets with JavaFX](https://stackoverflow.com/questions/70870998/how-to-return-objects-from-inputstream-client-to-a-javafx-controller/70888362#70888362). As suggested in other comments and answers consider a different technology than raw sockets. – jewelsea Jan 02 '23 at 03:37

1 Answers1

0

Instead of using raw sockets it might be more efficient to use a higher layer framework like, e.g., MQTT. The Eclipse foundation provides a free server (Mosquitto) as well as a free Java client (Paho).

mipa
  • 10,369
  • 2
  • 16
  • 35