I'm developing a chat system, in it I have a Java desktop application that needs to communicate with a WebService (server) which will distribute the messages to all connected clients. What is the best way to do this?
I have a WebService RestFul Jersey running, with only one method that returns the data passed as URL parameters on the client. Here are the WS codes:
@Path("/chat")
@GET
@Path("connect/{username}/{msg}/{ip}/{port}")
@Produces("application/json")
public String getConnection(@PathParam("username") String userName, @PathParam("msg") String msg, @PathParam("ip") String ip, @PathParam("port") int port){
String result = "";
if (msg.equals(null) || msg.equals("exit") || msg.equals("sair")){
System.out.println("Desconectando...");
result = "Descontado.";
}else{
result = "Usuário: "+userName+"\n"+
"Mensagem: "+msg+"\n"+
"IP: "+ip+"\n"+"Porta: "+port;
}
return result;
}
The customer code is below:
public static void main(String[] args) {
try {
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost:8080/Restful/chat/connect/Usuario/mensagem/192.168.0.2/8080");
ClientResponse response = webResource.accept("application/json")
.get(ClientResponse.class);
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
} catch (Exception e) {
e.printStackTrace();
}
}
When I start WS and run a client I get the following return: The problem is this: how do I have multiple WS clients sending and receiving messages at the same time?