Search in sources :

Example 1 with Result

use of com.pusher.rest.data.Result in project java-docs-samples by GoogleCloudPlatform.

the class SendMessageServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Parse POST request body received in the format :
    // [{"message": "my-message", "socket_id": "1232.24", "channel": "presence-my-channel"}]
    String body = CharStreams.readLines(request.getReader()).toString();
    String json = body.replaceFirst("^\\[", "").replaceFirst("\\]$", "");
    Map<String, String> data = gson.fromJson(json, typeReference.getType());
    String message = data.get("message");
    String socketId = data.get("socket_id");
    String channelId = data.get("channel_id");
    User user = UserServiceFactory.getUserService().getCurrentUser();
    // user email prefix as display name for current logged in user
    String displayName = user.getNickname().replaceFirst("@.*", "");
    // Create a message including the user email prefix to display in the chat window
    String taggedMessage = "<strong>&lt;" + displayName + "&gt;</strong> " + message;
    Map<String, String> messageData = new HashMap<>();
    messageData.put("message", taggedMessage);
    // Send a message over the Pusher channel (maximum size of a message is 10KB)
    Result result = PusherService.getDefaultInstance().trigger(channelId, // name of event
    "new_message", messageData, // (optional) use client socket_id to exclude the sender from receiving the message
    socketId);
    // result.getStatus() == SUCCESS indicates successful transmission
    messageData.put("status", result.getStatus().name());
    response.getWriter().println(gson.toJson(messageData));
}
Also used : User(com.google.appengine.api.users.User) HashMap(java.util.HashMap) Result(com.pusher.rest.data.Result)

Example 2 with Result

use of com.pusher.rest.data.Result in project community by GoogleCloudPlatform.

the class SendMessageServlet method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Parse POST request body received in the format:
    // [{"message": "my-message", "socket_id": "1232.24", "channel": "presence-my-channel"}]
    String body = CharStreams.readLines(request.getReader()).toString();
    String json = body.replaceFirst("^\\[", "").replaceFirst("\\]$", "");
    Map<String, String> data = gson.fromJson(json, typeReference.getType());
    String message = data.get("message");
    String socketId = data.get("socket_id");
    String channelId = data.get("channel_id");
    User user = UserServiceFactory.getUserService().getCurrentUser();
    // User email prefix as display name for current currently authenticated user
    String displayName = user.getNickname().replaceFirst("@.*", "");
    // Create a message including the user email prefix to display in the chat window
    String taggedMessage = "<strong>&lt;" + displayName + "&gt;</strong> " + message;
    Map<String, String> messageData = new HashMap<>();
    messageData.put("message", taggedMessage);
    // Send a message over the Pusher channel (maximum size of a message is 10KB)
    Result result = PusherService.getDefaultInstance().trigger(channelId, // name of event
    "new_message", messageData, // (Optional) Use client socket_id to exclude the sender from receiving the message
    socketId);
    // result.getStatus() == SUCCESS indicates successful transmission
    messageData.put("status", result.getStatus().name());
    response.getWriter().println(gson.toJson(messageData));
}
Also used : User(com.google.appengine.api.users.User) HashMap(java.util.HashMap) Result(com.pusher.rest.data.Result)

Aggregations

User (com.google.appengine.api.users.User)2 Result (com.pusher.rest.data.Result)2 HashMap (java.util.HashMap)2