use of javax.websocket.Session in project jetty.project by eclipse.
the class TextStreamTest method testEcho.
private void testEcho(int size) throws Exception {
char[] data = randomChars(size);
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + PATH);
ClientTextStreamer client = new ClientTextStreamer();
Session session = wsClient.connectToServer(client, uri);
try (Writer output = session.getBasicRemote().getSendWriter()) {
output.write(data);
}
Assert.assertTrue(client.await(5, TimeUnit.SECONDS));
Assert.assertArrayEquals(data, client.getEcho());
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class JsrBrowserSocket method onMessage.
@OnMessage
public void onMessage(String message) {
LOG.info("onTextMessage({})", message);
int idx = message.indexOf(':');
if (idx > 0) {
String key = message.substring(0, idx).toLowerCase(Locale.ENGLISH);
String val = message.substring(idx + 1);
switch(key) {
case "info":
{
writeMessage("Using javax.websocket");
if (StringUtil.isBlank(userAgent)) {
writeMessage("Client has no User-Agent");
} else {
writeMessage("Client User-Agent: " + this.userAgent);
}
if (StringUtil.isBlank(requestedExtensions)) {
writeMessage("Client requested no Sec-WebSocket-Extensions");
} else {
writeMessage("Client Sec-WebSocket-Extensions: " + this.requestedExtensions);
}
Set<Session> openSessions = session.getOpenSessions();
writeMessage("OpenSessions.size() = " + openSessions.size());
int i = 0;
for (Session open : openSessions) {
writeMessage(" OpenSession[%d] = %s", i++, open);
}
break;
}
case "many":
{
String[] parts = StringUtil.csvSplit(val);
int size = Integer.parseInt(parts[0]);
int count = Integer.parseInt(parts[1]);
writeManyAsync(size, count);
break;
}
case "manythreads":
{
String[] parts = StringUtil.csvSplit(val);
int threadCount = Integer.parseInt(parts[0]);
int size = Integer.parseInt(parts[1]);
int count = Integer.parseInt(parts[2]);
Thread[] threads = new Thread[threadCount];
// Setup threads
for (int n = 0; n < threadCount; n++) {
threads[n] = new Thread(new WriteMany(remote, size, count), "WriteMany[" + n + "]");
}
// Execute threads
for (Thread thread : threads) {
thread.start();
}
// Drop out of this thread
break;
}
case "time":
{
Calendar now = Calendar.getInstance();
DateFormat sdf = SimpleDateFormat.getDateTimeInstance(SimpleDateFormat.FULL, SimpleDateFormat.FULL);
writeMessage("Server time: %s", sdf.format(now.getTime()));
break;
}
default:
{
writeMessage("key[%s] val[%s]", key, val);
}
}
} else {
// Not parameterized, echo it back
writeMessage(message);
}
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class SessionTrackingTest method assertServerOpenConnectionCount.
private void assertServerOpenConnectionCount(int expectedCount) {
Set<Session> sessions = serverContainer.getOpenSessions();
int openCount = 0;
for (Session session : sessions) {
Assert.assertThat("Session.isopen: " + session, session.isOpen(), Matchers.is(true));
openCount++;
}
Assert.assertThat("Open Session Count", openCount, Matchers.is(expectedCount));
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class BinaryStreamTest method testEcho.
private void testEcho(int size) throws Exception {
byte[] data = randomBytes(size);
URI uri = URI.create("ws://localhost:" + connector.getLocalPort() + PATH);
ClientBinaryStreamer client = new ClientBinaryStreamer();
Session session = wsClient.connectToServer(client, uri);
try (OutputStream output = session.getBasicRemote().getSendStream()) {
output.write(data);
}
Assert.assertTrue(client.await(5, TimeUnit.SECONDS));
Assert.assertArrayEquals(data, client.getEcho());
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class EndpointEchoTest method testAbstractEchoInstance.
@Test
public void testAbstractEchoInstance() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
EchoStringEndpoint echoer = new EchoStringEndpoint();
Assert.assertThat(echoer, instanceOf(javax.websocket.Endpoint.class));
// Issue connect using instance of class that extends abstract that extends Endpoint
Session session = container.connectToServer(echoer, serverUri);
if (LOG.isDebugEnabled())
LOG.debug("Client Connected: {}", session);
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
echoer.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
Aggregations