A
A
Andrey Kostyaev2018-04-12 01:25:08
Java
Andrey Kostyaev, 2018-04-12 01:25:08

Java chat (JavaFX client interface), how to do?

I am doing a chat, with registration and authorization (I have no experience in Java).
Question one: What is the best way to send an object from the client to the server? Don't be too smart. I wanted to first convert the object to xml , then convert it to a string and send it to the server, and on the server already "pull out" the object from xml . Everything seemed to be going well, the line came to the server, I could even output it somewhere, but it doesn’t want to be converted into an object, although it’s xml code (I climbed everywhere, but nothing helps, if necessary, I’ll throw an example when, like me do). If I'm still not doing it right, then tell me how to do it better. Which serialization is better to use and how to send the serialized object to the server.
Question two: I have not yet decided how exactly I will do it, or a general chat, or so that clients can chat with each other, or even create groups (rooms). But the question is different, Here is the Login and Password will be stored in the database. But I would also like to make sure that there is a history of messages, and not just a chat every time from scratch for each client. So here's the question: How to save correspondence in general, in a database or somehow else, for example, in files, maybe some on the server itself (This is my guess).
If it's not difficult, then help me, I'm doing the chat purely for myself, and let me remind you that I've been doing Java recently. A simple chat has already been made and parsed, which worked as an echo server, when one user sent a message and this message was sent to other users.
PS I do on sockets.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Alexandrov, 2018-04-12
@mrdestroii

This is the second question you've asked, and it's still stupid.
First of all, what does javafx have to do with it? This is generally ui and no side, and even more complexities, simply by definition, there can be no way at all and no side.
Secondly, absolutely, I emphasize ABSOLUTELY, all that should worry at the moment is architecture. And you NEED to start with it, sit down, just take a piece of paper and write in paragraphs in a column what you need from the chat, that’s all you want from it, draw a line and group the Wishlist on the right in a column according to their meaning, then for the received groups on the right write pseudocode what will be used for this feature and what data types will go. I already cited something like this in your previous question, but I’ll throw in an example here as well.
Thirdly, you need to come to the toaster with a specific problem, and even with which it was not possible to google anything, something like here I have serialization, here is the serialization method, here is the deserialization, but here is an error and I can’t understand why.
For the second point, an example
I want | Association of similar | How to do
------------------------------------------------ -------------------------------------------------- -------------
1 chat with a friend | 1 | Format (to whom, from whom, message)
2 story | 2 | Store in sql db message format
3 chat with everyone | 1 | Format (from whom, message)
4 chat with everyone but in the room | 1 | Format (from whom, room, message)
5 authorization | 3 | store in sql database, transfer Format(login, password)
6 communication client server | | Text view, Json
7 registration | 3 | store in sql , pass in Format(login, password)
n
According to the result, you take and systematize more specifically like this:
1) Many different message formats have been drawn and it is necessary to distinguish them somehow, which means I will make a parent class with an int field that will store the message type. The problem of information exchange between the client and the server is completely solved on both sides.

abstract class AMessage{
int messType;
}

2) A universal method for serializing and deserializing objects from \ to json is needed. I'll take the gson library, after reading its docks, it turned out such a simple way without a collective farm. The problem is completely solved.
public <T> T fromJson(String json, Class<T> classOfT){
        Gson gson = new GsonBuilder().create();
        try{
            return gson.fromJson(json, classOfT);
        }catch(JsonSyntaxException jse){
            return null;
        }
    }
    public String toJson(Object obj){
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        try{
            return gson.toJson(obj);
        }catch(JsonSyntaxException jse){
            return null;
        }
    }

3) You need to assign and write classes for each message.
For a message to a room, it will be like this
//от кого, комната, сообщение
class RoomMessage extends AMessage{
String from;
String roomName;
String message;
RoomMessage(String from, String roomName, String message){
 this.messType = 10;
 this.from = from;
 this.roomName = roomName;
 this.message = message;
}
}

n - this will be the registration class, etc.
Step n.
Now you just start writing code. Moreover, when writing, you go through all the steps and only as described earlier, even if during the implementation process it turned out to be difficult/crooked/stupid/wrong.
Programming actually consists of 90 percent of such red tape with writing everything and everything on paper and only 10% of implementation. In addition, in this way everything can be done very quickly, logically and clearly, it will be very easy to look for errors in the program and debug it. In addition, you will also immediately receive almost ready-made documentation that will only be combed and can be shown to another coder and he will immediately enter what is here and how it works. But writing code in an "empirical" way is always a dead end, in which you will stall on the spot, constantly forget what is here and how, and most importantly, why and why it does not work.
ps if I were a teacher, and you were a student, I would kick your ass and generally force me to write the entire logic of the program on paper instead of such work and then tell why and why I did it.

K
Keyn Din, 2018-04-12
@Lure_of_Chaos

What is the best way to send an object from the client to the server?

Looking as the server is made. You can, for example, use the good old sockets and ObjectOutputStream \ ObjectInputStream. Or, if the server understands HTTP (say, some Spring WebMVC \ WebFlux is used), then use Apache HTTP Components or something similar.
In what?
It is best, of course, to use a database - it is easier to make selections than from files.
But in general, since
so do everything the same, only in Java, the difficulty here is not in using the platform, but in thinking through the architecture.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question