use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.
the class BrowserSocket method onTextMessage.
@OnWebSocketMessage
public void onTextMessage(String message) {
if (message.length() > 300) {
int len = message.length();
LOG.info("onTextMessage({} ... {}) size:{}", message.substring(0, 15), message.substring(len - 15, len).replaceAll("[\r\n]*", ""), len);
} else {
LOG.info("onTextMessage({})", message);
}
// Is multi-line?
if (message.contains("\n")) {
// echo back exactly
writeMessage(message);
return;
}
// Is resource lookup?
if (message.charAt(0) == '@') {
String name = message.substring(1);
URL url = Loader.getResource(name);
if (url == null) {
writeMessage("Unable to find resource: " + name);
return;
}
try (InputStream in = url.openStream()) {
String data = IO.toString(in);
writeMessage(data);
} catch (IOException e) {
writeMessage("Unable to read resource: " + name);
LOG.warn("Unable to read resource: " + name, e);
}
return;
}
// Is parameterized?
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":
{
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 requested Sec-WebSocket-Extensions: " + this.requestedExtensions);
writeMessage("Negotiated Sec-WebSocket-Extensions: " + session.getUpgradeResponse().getHeader("Sec-WebSocket-Extensions"));
}
break;
}
case "many":
{
String[] parts = val.split(",");
int size = Integer.parseInt(parts[0]);
int count = Integer.parseInt(parts[1]);
writeManyAsync(size, count);
break;
}
case "manythreads":
{
String[] parts = val.split(",");
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(session.getRemote(), 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);
}
}
return;
}
// Not parameterized, echo it back as-is
writeMessage(message);
}
use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.
the class BigEchoSocket method onBinary.
@OnWebSocketMessage
public void onBinary(Session session, byte[] buf, int offset, int length) throws IOException {
if (!session.isOpen()) {
LOG.warn("Session is closed");
return;
}
RemoteEndpoint remote = session.getRemote();
remote.sendBytes(ByteBuffer.wrap(buf, offset, length), null);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
}
use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.
the class EchoSocket method onBinary.
@OnWebSocketMessage
public void onBinary(byte[] buf, int offset, int len) throws IOException {
LOG.debug("onBinary(byte[{}],{},{})", buf.length, offset, len);
// echo the message back.
ByteBuffer data = ByteBuffer.wrap(buf, offset, len);
RemoteEndpoint remote = this.session.getRemote();
remote.sendBytes(data, null);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
}
use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.
the class EchoSocket method onText.
@OnWebSocketMessage
public void onText(String message) throws IOException {
LOG.debug("onText({})", message);
// echo the message back.
RemoteEndpoint remote = session.getRemote();
remote.sendString(message, null);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
}
use of org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage in project jetty.project by eclipse.
the class RFCSocket method onBinary.
@OnWebSocketMessage
public void onBinary(byte[] buf, int offset, int len) throws IOException {
LOG.debug("onBinary(byte[{}],{},{})", buf.length, offset, len);
// echo the message back.
ByteBuffer data = ByteBuffer.wrap(buf, offset, len);
RemoteEndpoint remote = session.getRemote();
remote.sendBytes(data, null);
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
}
Aggregations