Skip to content

Instantly share code, notes, and snippets.

@survivant
Created December 6, 2011 18:31
Show Gist options
  • Save survivant/1439322 to your computer and use it in GitHub Desktop.
Save survivant/1439322 to your computer and use it in GitHub Desktop.
Socketio-Websocket usecase
import static org.testng.Assert.assertEquals;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import com.ning.http.client.AsyncHandler;
import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClientConfig;
import com.ning.http.client.HttpResponseBodyPart;
import com.ning.http.client.Response;
import com.ning.http.client.async.AbstractBasicTest.AsyncCompletionHandlerAdapter;
import com.ning.http.client.providers.grizzly.GrizzlyAsyncHttpProvider;
import com.ning.http.client.websocket.WebSocket;
import com.ning.http.client.websocket.WebSocketListener;
import com.ning.http.client.websocket.WebSocketUpgradeHandler;
public class SocketioTest {
public static String sessionid = null;
public static String sessionidWS = null;
public static String transports = null;
public static AsyncHttpClient getAsyncHttpClient(AsyncHttpClientConfig config) {
if (config == null) {
config = new AsyncHttpClientConfig.Builder().build();
}
return new AsyncHttpClient(new GrizzlyAsyncHttpProvider(config), config);
}
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// https://gist.github.com/1401544/ee3de92f73e702693cbe0a0e321fbae2274cc7f7
AsyncHttpClient c = getAsyncHttpClient(new AsyncHttpClientConfig.Builder().setFollowRedirects(true).build());
// on va chercher une sessionID
c.prepareGet("http://localhost:8080/socketiochat/ChatAtmosphereHandler/1/").execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
try {
String body = response.getResponseBody();
String array[] = body.split(":");
sessionid = array[0];
} finally {
}
return response;
}
}).get();
// on ouvre une connection en long-polling pour faire le connect
c.prepareGet("http://localhost:8080/socketiochat/ChatAtmosphereHandler/1/xhr-polling/" + sessionid).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
try {
String body = response.getResponseBody();
System.err.println("XHR Body=" + body);
} finally {
}
return response;
}
}).get();
// on ouvre une 2e connection en long-polling pour etre en suspend
c.prepareGet("http://localhost:8080/socketiochat/ChatAtmosphereHandler/1/xhr-polling/" + sessionid).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
try {
String body = response.getResponseBody();
System.err.println("XHR Body=" + body);
} finally {
}
return response;
}
}).get();
// on ouvre une 3e connection en long-polling pour etre en suspend
c.prepareGet("http://localhost:8080/socketiochat/ChatAtmosphereHandler/1/xhr-polling/" + sessionid).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
try {
String body = response.getResponseBody();
System.err.println("XHR Body=" + body);
} finally {
}
return response;
}
}).get();
// on va chercher une sessionID pour les Websocket
c.prepareGet("http://localhost:8080/socketiochat/ChatAtmosphereHandler/1/").execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
try {
String body = response.getResponseBody();
String array[] = body.split(":");
sessionidWS = array[0];
} finally {
}
return response;
}
}).get();
// on ouvre une connection en WebSocket
WebSocket websocket = c.prepareGet("ws://localhost:8080/socketiochat/ChatAtmosphereHandler/1/websocket/" + sessionidWS).execute(new WebSocketUpgradeHandler()).get();
websocket.addMessageListener(new WebSocketListener() {
@Override
public void onMessage(byte[] message) {
System.err.println("WS onMessage = " + new String(message));
}
@Override
public void onClose() {
System.err.println("WS onClose");
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
}
});
// on fait un connect, donc la connection en long-polling va recevoir un message
websocket.sendMessage("5:1+::{\"name\":\"nickname\",\"args\":[\"test\"]}".getBytes());
Thread.sleep(5000);
// on ouvre une 3e connection en long-polling pour recevoir les messages non recus
c.prepareGet("http://localhost:8080/socketiochat/ChatAtmosphereHandler/1/xhr-polling/" + sessionid).execute(new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
try {
String body = response.getResponseBody();
System.err.println("XHR Body=" + body);
} finally {
}
return response;
}
}).get();
Thread.sleep(5000);
c.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment