Search in sources :

Example 6 with OnMessage

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();
}
Also used : StringWriter(java.io.StringWriter) PrintWriter(java.io.PrintWriter) OnMessage(javax.websocket.OnMessage)

Example 7 with OnMessage

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());
}
Also used : CharBuffer(java.nio.CharBuffer) IOException(java.io.IOException) OnMessage(javax.websocket.OnMessage)

Example 8 with OnMessage

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);
        }
    }
}
Also used : Device(org.ecabrerar.examples.websockethome.model.Device) StringReader(java.io.StringReader) JsonReader(javax.json.JsonReader) JsonObject(javax.json.JsonObject) ServerEndpoint(javax.websocket.server.ServerEndpoint) OnMessage(javax.websocket.OnMessage)

Aggregations

OnMessage (javax.websocket.OnMessage)8 ServerEndpoint (javax.websocket.server.ServerEndpoint)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 CharBuffer (java.nio.CharBuffer)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Set (java.util.Set)1 ExecutionException (java.util.concurrent.ExecutionException)1 JsonObject (javax.json.JsonObject)1 JsonReader (javax.json.JsonReader)1 Session (javax.websocket.Session)1 Device (org.ecabrerar.examples.websockethome.model.Device)1