Reputation: 885
Using Ning to create and connect to Websocket, following is my configuration ,
NettyAsyncHttpProviderConfig config = new NettyAsyncHttpProviderConfig();
config.addProperty(NettyAsyncHttpProviderConfig.USE_BLOCKING_IO, "true");
AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder()
.setAsyncHttpClientProviderConfig(config);
AsyncHttpClient client = new AsyncHttpClient(
new NettyAsyncHttpProvider(builder.build()));
AsyncHttpClient.BoundRequestBuilder requestBuilder = client.prepareGet(createUri(method))
.addHeader(HttpHeaders.Names.CONNECTION, "Upgrade")
.addHeader(HttpHeaders.Names.UPGRADE, "WebSocket");
websocket = requestBuilder.execute(new WebSocketUpgradeHandler.Builder()
.addWebSocketListener(this).build()).get();
using websocket to send text message,
if (websocket!=null && websocket.isOpen())
websocket.sendTextMessage(jObj.toString());// send
onMessage()
method of the listener will add the response to a list
@Override
public void onMessage(String message) {
serverResponse.add(message);
}
After sending the text message, I have method which formats the response and save the result
result = responseFromServer();
private String responseFromServer() {
String response = null;
sleep(100);
if(!serverResponse.isEmpty())
//format the message which is added in list
return response;
}
issue is, if I dont have 'sleep(100)' in the above method, for request1- response is null and for request2, I get response1. I would like the websocket to work as synchronous so that, once I send the message, it should wait unitll the response is received and go forward! ANy suggestions!
Upvotes: 4
Views: 7735
Reputation: 1554
You can also use the CountDownLatch concept as illustrated in the link below:
@ClientEndpoint
public class WordgameClientEndpoint {
private static CountDownLatch latch;
private Logger logger = Logger.getLogger(this.getClass().getName());
@OnOpen
public void onOpen(Session session) {
// same as above
}
@OnMessage
public String onMessage(String message, Session session) {
// same as above
}
@OnClose
public void onClose(Session session, CloseReason closeReason) {
logger.info(String.format("Session %s close because of %s", session.getId(), closeReason));
latch.countDown();
}
public static void main(String[] args) {
latch = new CountDownLatch(1);
ClientManager client = ClientManager.createClient();
try {
client.connectToServer(WordgameClientEndpoint.class, new URI("ws://localhost:8025/websockets/game"));
latch.await();
} catch (DeploymentException | URISyntaxException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
https://blog.openshift.com/how-to-build-java-websocket-applications-using-the-jsr-356-api/
Upvotes: 3
Reputation: 301
Use wait
and notify
on a object,
synchronized (someObject){
try {
someObject.wait();
result = responseFromServer();
} catch (InterruptedException e) {
//when the object is interrupted
}
}
and in onMessage
notify the object once you got the message,
@Override
public void onMessage(String message) {
serverResponse.add(message);
synchronized(someObject) {
someObject.notify();
}
}
Upvotes: 7