Facebook
From Mustard Tapir, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 274
  1.    public Socket getSocket() {
  2.         Client client = ClientFactory.getDefault().newClient();
  3.  
  4.         RequestBuilder request = client.newRequestBuilder()
  5.                 .method(Request.METHOD.GET)
  6.                 .uri("http://localhost:8080" + "/notification/async_socket/1")
  7.                 .encoder(new Encoder<Object, String>() {
  8.                     @Override
  9.                     public String encode(Object data) {
  10.                         int a = 1;
  11.                         return "ala";
  12.                     }
  13.                 })
  14.                 .decoder(new Decoder<String, Object>() {
  15.                     @Override
  16.                     public Object decode(Event type, String data) {
  17.  
  18.                         data = data.trim();
  19.  
  20.                         // Padding
  21.                         if (data.length() == 0) {
  22.                             return null;
  23.                         }
  24.  
  25.                         if (type.equals(Event.MESSAGE)) {
  26.                                 return "brak danych";
  27.                         } else {
  28.                             return null;
  29.                         }
  30.                     }
  31.                 })
  32.                 .transport(Request.TRANSPORT.LONG_POLLING);
  33.  
  34.         final org.atmosphere.wasync.Socket socket = client.create();
  35.         socket.on("message", new Function<Object>() {
  36.             @Override
  37.             public void on(final Object t) {
  38.                 int w = 1;
  39.             }
  40.         }).on(new Function<Throwable>() {
  41.             @Override
  42.             public void on(Throwable t) {
  43.                 t.printStackTrace();
  44.             }
  45.         });
  46.  
  47.         try {
  48.             socket.open(request.build());
  49.             socket.fire("jacek");
  50.         } catch (IOException e) {
  51.             e.printStackTrace();
  52.         }
  53.  
  54.         return mSocket;
  55.         }