use of javax.websocket.OnMessage in project jetty.project by eclipse.
the class InfoSocket method onMessage.
@OnMessage
public String onMessage(String msg) {
StringWriter str = new StringWriter();
PrintWriter out = new PrintWriter(str);
String[] args = msg.split("\\|");
switch(args[0]) {
case "info":
out.printf("websocketSession is %s%n", asPresent(session));
out.printf("httpSession is %s%n", asPresent(httpSession));
out.printf("servletContext is %s%n", asPresent(servletContext));
break;
case "data":
dataMaker.processMessage(args[1]);
break;
}
return str.toString();
}
use of javax.websocket.OnMessage in project javaee7-samples by javaee-samples.
the class MyEndpointReader method echoReader.
@OnMessage
public String echoReader(Reader reader) {
System.out.println("echoReader");
CharBuffer buffer = CharBuffer.allocate(20);
try {
reader.read(buffer);
} catch (IOException ex) {
Logger.getLogger(MyEndpointReader.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
return new String(buffer.array());
}
use of javax.websocket.OnMessage in project javaee7-firstcup by ecabrerar.
the class DeviceWebSocketServer method handleMessage.
@OnMessage
public void handleMessage(String message, Session session) {
try (JsonReader reader = Json.createReader(new StringReader(message))) {
JsonObject jsonMessage = reader.readObject();
if ("add".equals(jsonMessage.getString("action"))) {
Device device = new Device();
device.setName(jsonMessage.getString("name"));
device.setDescription(jsonMessage.getString("description"));
device.setType(jsonMessage.getString("type"));
device.setStatus("Off");
sessionHandler.addDevice(device);
}
if ("remove".equals(jsonMessage.getString("action"))) {
int id = (int) jsonMessage.getInt("id");
sessionHandler.removeDevice(id);
}
if ("toggle".equals(jsonMessage.getString("action"))) {
int id = (int) jsonMessage.getInt("id");
sessionHandler.toggleDevice(id);
}
}
}
Aggregations