Package com.appspot.mscheckers

Source Code of com.appspot.mscheckers.DisconnectedHandler

package com.appspot.mscheckers;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.channel.ChannelMessage;
import com.google.appengine.api.channel.ChannelPresence;
import com.google.appengine.api.channel.ChannelService;
import com.google.appengine.api.channel.ChannelServiceFactory;
import com.google.gson.Gson;

public class DisconnectedHandler extends HttpServlet{
  /**
   *
   */
  private static final long serialVersionUID = 1L;

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    ChannelService channelService = ChannelServiceFactory.getChannelService();
    ChannelPresence presence = channelService.parsePresence(req);
    String userId = presence.clientId();
   
    User u = Checkers.userMap.remove(userId);
    //send a note to all connected users
    Enumeration<String> keys = Checkers.userMap.keys();
    String key;
    String message = "{\""+Constant.MESSAGE_TYPE+"\":\""+Constant.USER_SIGNOUT+"\"}";
    Gson gson = new Gson();
    String gsu = gson.toJson(u);
   
    while(keys.hasMoreElements()){
      key = keys.nextElement();
      if( key.equals("123321")) continue;
      try{
        channelService.sendMessage(new ChannelMessage(key, message));
        channelService.sendMessage(new ChannelMessage(key, gsu));
      }catch(Exception e){
        e.printStackTrace();
      }
    }   
  }
 
}
TOP

Related Classes of com.appspot.mscheckers.DisconnectedHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.