public static void sendImage(MediaFeedData feed, WebSocket.Out<JsonNode> out) {

    if (feed != null) {

      Logger.info("			image id:" + feed.getId());

      Caption caption = feed.getCaption();
      Images images = feed.getImages();

      ObjectNode msg = Json.newObject();
      msg.put("kind", "newImage");
      msg.put("id", feed.getId());
      msg.put("createdTime", feed.getCreatedTime());
      if (caption != null) {
        msg.put("authorFullName", caption.getFrom().getFullName());
        msg.put("authorPic", caption.getFrom().getProfilePicture());
      }
      msg.put("nLikes", feed.getLikes().getCount());
      msg.put("uLikes", feed.getLikes().getLikesUserList().toString());
      msg.put("nComments", feed.getComments().getCount());
      msg.put("uComments", feed.getComments().getComments().toString());
      msg.put("stdImageUrl", images.getStandardResolution().getImageUrl());
      out.write(msg);
    }
  } // sendImage
 /** @param event */
 private void handleGeolocation(JsonNode event) {
   GeolocationExtractor geoExtractor = new GeolocationExtractor();
   String lat = event.get("lat").asText();
   String lng = event.get("lng").asText();
   String location = null;
   try {
     location = geoExtractor.getPlaceName(geoExtractor.getLocationInfo(lat, lng));
   } catch (Exception e) {
     e.printStackTrace();
   }
   ObjectNode message = Json.newObject();
   message.put("geo", location);
   socketOut.write(message);
 }
  protected void handleEvent(JsonNode event) { // obslug eventow z websocketa

    if (!event.has("request")) {
      return;
    }

    String request = event.get("request").asText();

    if (request.equals("geolocation")) {
      handleGeolocation(event);
      return;
    } else if (request.equals("search")) {

      // get request data
      String pattern = event.get("pattern").asText();
      Boolean limit = event.get("limit").asText().equals("true");
      List<String> tagList = ctxEx.extractTags(pattern);
      String searchPattern = ctxEx.stripTags(pattern);

      Logger.info("searching for:" + pattern + " with" + (limit ? "out" : "") + " limit");
      Logger.info("search:" + searchPattern + "\ntags:" + tagList.toString());

      ArrayList<ArrayList<String>> searchResult = search(searchPattern, limit);

      if (!tagList.isEmpty()) searchResult = filterOutByTags(searchResult, tagList);

      if (searchResult == null) {
        sendEmptyResults();
      } else {
        Logger.info(String.valueOf(searchResult.size()) + " found");

        ObjectNode message = Json.newObject(); // create message

        ArrayNode results = message.putArray("result"); // results array in message
        Set<String> tagsSet = new HashSet<>();

        for (ArrayList<String> result : searchResult) {
          ObjectNode innerMsg = Json.newObject(); // inner message (file info)
          innerMsg.put("file", result.get(0));
          innerMsg.put("link", result.get(1));
          innerMsg.put("size", result.get(2));

          ArrayNode tags = innerMsg.putArray("tags"); // tags array in innerMsg (for this file)
          // odejmujemy 4 ze wzgledu na to ze na poczatku sa 4 elementy ktore nie sa tagami
          int tagcount = result.size() - 4;
          for (int tagnr = 0; tagnr < tagcount - 2; tagnr++) {
            tags.add(result.get(4 + tagnr));
            tagsSet.add("\"" + result.get(4 + tagnr) + "\"");
          }
          String tempCont = ctxEx.getContext(result.get(3), searchPattern);
          innerMsg.put("context", tempCont);
          innerMsg.put("lat", result.get(result.size() - 2));
          innerMsg.put("lng", result.get(result.size() - 1));
          results.add(innerMsg);
        }
        int temp = searchResult.size();
        String temp2 = "" + temp;
        message.put("resultsCount", temp2);
        message.put("tagList", tagsSet.toString());
        socketOut.write(message);
      }
    }
  }
 private void sendEmptyResults() {
   ObjectNode message = Json.newObject();
   message.put("result", "{}");
   Logger.info("No results");
   socketOut.write(message);
 }
 /**
  * Iterate connection list and write incoming message back
  *
  * @param message, message to write
  */
 public static void notifyAll(String message) {
   for (WebSocket.Out<String> out : wsConnections) {
     out.write(message);
   }
 }
Пример #6
0
  public WebsocketTransport(Messages.JoinWebsocket join) {
    this.out = join.out;
    this.in = join.in;
    this.context = join.context;

    final ActorRef self = getContext().self();

    in.onClose(
        () -> {
          signalJActor.tell(new Messages.Quit(context.connectionId), self);
          self.tell(PoisonPill.getInstance(), self);
        });
    in.onMessage(
        json -> {
          Logger.debug("Message from user: "******" : " + json);
          signalJActor.tell(new Messages.Execute(context, json), self);
        });

    context()
        .setReceiveTimeout(
            Duration.create(
                SignalJPlugin.getConfiguration().getKeepAliveTimeout() / 2, TimeUnit.SECONDS));

    receive(
        ReceiveBuilder.match(
                Messages.JoinWebsocket.class, r -> out.write(JsonHelper.writeConnect(prefix)))
            .match(
                Messages.MethodReturn.class,
                methodReturn -> {
                  out.write(JsonHelper.writeMethodReturn(methodReturn));
                  sendAck(methodReturn);
                })
            .match(
                Messages.ClientFunctionCall.class,
                clientFunctionCall -> {
                  out.write(JsonHelper.writeClientFunctionCall(clientFunctionCall, prefix));
                  sendAck(clientFunctionCall);
                })
            .match(
                Messages.ClientCallEnd.class,
                clientCallEnd -> {
                  out.write(JsonHelper.writeConfirm(clientCallEnd.context));
                  sendAck(clientCallEnd);
                })
            .match(ReceiveTimeout.class, r -> out.write(JsonHelper.writeHeartbeat()))
            .match(
                Messages.ReconnectWebsocket.class,
                r -> Logger.debug("Reconnect Websocket " + r.context.connectionId))
            .match(
                Messages.StateChange.class,
                state -> {
                  out.write(JsonHelper.writeState(state));
                  sendAck(state);
                })
            .match(
                Messages.Error.class,
                error -> {
                  out.write(JsonHelper.writeError(error));
                  sendAck(error);
                })
            .build());
  }