Skip to content

Instantly share code, notes, and snippets.

@dguggemos
Last active February 9, 2016 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dguggemos/3c1c3316052e1dbeb663 to your computer and use it in GitHub Desktop.
Save dguggemos/3c1c3316052e1dbeb663 to your computer and use it in GitHub Desktop.
import io.undertow.Handlers;
import io.undertow.Undertow;
import io.undertow.websockets.WebSocketConnectionCallback;
import io.undertow.websockets.core.AbstractReceiveListener;
import io.undertow.websockets.core.BufferedTextMessage;
import io.undertow.websockets.core.WebSocketChannel;
import io.undertow.websockets.core.WebSockets;
import io.undertow.websockets.spi.WebSocketHttpExchange;
public class ChatServer {
public static void main(final String[] args) {
System.out.println("To see chat in action is to open two different browsers and point them at http://localhost:8080");
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setBufferSize(128)
.setHandler(Handlers.path()
.addPrefixPath("/myapp", Handlers.websocket(new WebSocketConnectionCallback() {
@Override
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) {
final String messageData = message.getData();
for (WebSocketChannel session : channel.getPeerConnections()) {
WebSockets.sendText(messageData, session, null);
}
}
});
channel.resumeReceives();
}
})))
.build();
server.start();
}
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.asynchttpclient.DefaultAsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.ws.WebSocket;
import org.asynchttpclient.ws.WebSocketTextListener;
import org.asynchttpclient.ws.WebSocketUpgradeHandler;
public class Client
{
public static final int WEB_SOCKET_MAX_FRAME_SIZE = 256;
public static final Random random = new Random();
public static final DefaultAsyncHttpClientConfig CONFIG =
new DefaultAsyncHttpClientConfig.Builder().setWebSocketMaxFrameSize(WEB_SOCKET_MAX_FRAME_SIZE).build();
public static final DefaultAsyncHttpClient CLIENT = new DefaultAsyncHttpClient(CONFIG);
public static final String CHARACTERS =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZäöüÄÖÜ!\"§$%&/()=?*'_:;1234567890";
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException
{
final WebSocketUpgradeHandler handler =
new WebSocketUpgradeHandler.Builder().addWebSocketListener(new MyWebSocketListener()).build();
CLIENT.prepareGet("ws://localhost:8080/myapp").execute(handler).get(20, TimeUnit.SECONDS);
}
private static class MyWebSocketListener implements WebSocketTextListener
{
@Override
public void onMessage(final String message)
{
System.out.println("MESSAGE: " + message);
}
@Override
public void onOpen(final WebSocket websocket)
{
System.out.println("OPEN");
websocket.sendMessage(randomString(2 * WEB_SOCKET_MAX_FRAME_SIZE));
}
@Override
public void onClose(final WebSocket websocket)
{
System.out.println("CLOSE");
}
@Override
public void onError(final Throwable t)
{
System.out.println("ERROR: " + t.getMessage());
}
}
public static String randomString(final int length)
{
final StringBuilder buffer = new StringBuilder();
for (int i = 0; i < length; i++)
{
buffer.append(CHARACTERS.charAt(random.nextInt(CHARACTERS.length())));
}
return buffer.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment