I'm developing a simple app with WebSockets.
Client side you can send input texts, while the server side is implemented via a Java class with annotated as ServerEndpoint. Here the input texts received get decoded and sent to all of the open sessions.
This works, but I want to store every message received by the server, so that when a new client connects, (using a method annotated with #OnOpen) it receives all the messages that has been sent while he was "offline". Where can I store this messages without using a database?
Solved it by using Singleton pattern. I created a Singleton class that stores a List, which gets updated in the onMessage method. Then, in onOpen, the messages stored get sent to the client that just opened the connection with the socket.
Related
Can gRPC bidi streaming server respond out of sequence to a client ? All examples on the net show server responding to an incoming request only. The StreamObserver interface contains implementation for the response responding to a request
In onNext method, can the StreamObserver parameter be cached and reused later to send messages ?
What I need :
I have cached the StreamObserver on the first request for Client1
On a request from Client2, I need to send a message to Client1
Using the cached StreamObserver object throws a CANCELLED error and I notice onComplete is called for the first request from Client1
Is there a way to do this ?
This seems to be a similar ask, but was not supported 2015, and am not sure if this now possible
Once the server receives the StreamObserver for responses to the client it can call the object from any thread at any time (although the object is not thread-safe, so concurrent calls are not permitted). There's no need to couple it with incoming requests in onNext().
The client calling onComplete() is normal and does not end the RPC. If your code turns around and calls onComplete(), however, at that point you can no longer send any more messages on the stream. You will probably want to observe client cancellations, which can be achieved by casting the StreamObserver to ServerCallStreamObserver and calling setOnCancelHandler(Runnable) at the start of the RPC.
I am one server and multiple clients via threads. The client(s) send their message to the server. I have worked out how to make the server send the message back to client like a echo system. If I have two clients, I want them to send their message to the server and the server should send it to the client that did not send the message i.e. the other client. How would I go about send the message back to all the clients apart from the one that send the message?
When the message comes in, determine what the userID / other identifying id the incoming message is associated with. Then re-broadcast to all other sockets, but exclude the Socket associated with the ID that sent the message
Make at the server side a list with all the clients...
every time a new msg is received, then iterate the list and send the msg using the port of the socket as id...
I recently wrote a Chat program too. What I did was, I had a class ClientHandler that handles the connection for each individual client.
Inside ClientHandler I had a HashMap. I added each client that had connected to the HashMap, with the Key being the client id. I used a UUID rather than int for the client id.
Inside this handler class, I had a sendMessage(String str) method. Within this method, a for-each loop that loops through each ClientHandler object, checking the values inside the HashMap. Inside this for-each loop, I have an if statement that checks whether you are writing to the ClientHandler object with this id. If the check returns false, you go ahead and write the message on the PrintWriter and the message won't be sent to the client writing the message.
This worked for me. Might not work for you.
How I can send file from one client (A) to another one (B) via socket? and vice versa, send file from B to A. I mean that make the client sender and receiver at the same time.
In other word, when muticlient connect to server, how I distinguish between clients ?
You need to implement you own communication message format in short a simple protocol .
You keep a list of all active sockets in a shared list/map , and based on the request from the message you pick up the apt client and push the desired message to that.
You can implement the actual message format as you want, but this can be the blueprint.
In this case lets say your client A sends message : 1. Client Id 2. File Start 3 X . File Content 4. File End
as soon as you get a connection you get the target client id , the file start message lets you understand the next message just needs to be diverted to target and file End message defines the transfer complete.
Also, you may would like to send Acknowledgement message from server to client, in order to eradicate transfer issues.
It is good way to manage client using their id(i.e. a unique long or string or any other for each user). At the time of connection to socket client send their id , store that is in collection. And when a user(Client) want to send file send with own id and Id of that user(Client) want to send.
I am trying to write a client server application that communicate using Message Objects(Message Class is defined in my application). there is a scenario in which i want to transfer file between them. First I must send a message to client to notify it about specific file information and after that the file itself is going to be written to channel.
The problem is how can I handle this scenario in client?
Is it a good solution to remove Message handler after receiving message and replace it with a byte array handler?
what are the alternatives?
Sure you can just modify the pipeline on the fly. We do something similar in our portunification example[1].
[1] https://github.com/netty/netty/blob/4.0/example/src/main/java/io/netty/example/portunification/PortUnificationServerHandler.java
I have two clients A, B
also I have a server S
A sends a request to the server S, due to this request the server S wants to inform client B that client A made a request.
the same can happen if B sends a request to server S but this time server S will inform A
is this possible with the client server model? I have managed to create a simple program where client A sends a request to server S and displays the server's response. In this program server S listens to endless connections which means that I have something like this:
while(true){
Socket userSocket = serverSocket.accept();
new Connection(clientSocket);
//where in the Connection class we manage the connection events
}
but I can't understand how I can make the server 'S' a client inside the class "Connection" to the client that didn't send the request. If I do the same process I usually do for any client (A or B) I get an exception which prints some IP address and I'm not sure which ones because I run everything locally.
thanks in advance
Practically speaking you can do what you said, which is to have S notify B that A has made a request or vice versa. Conceptually though this doesn't mean that S is now a client of B. S is still a server it just happens to be sending B a message on its own rather than reacting to input from B.
Think about S representing a chat server. If A sends a message to S and S forwards it to B, is S now a client of B or still a server?