springmvcʵÏÖlong-pulling¼¼Êõ(Ò»)

2014-11-23 19:04:02 ¡¤ ×÷Õß: ¡¤ ä¯ÀÀ: 41

±³¾°½éÉÜ£º

ÏîÄ¿ÖÐÓÐÒ»¸öͨѶģ¿é£¬±¾À´ÊÇÓÃwebsocketȫ˫¹¤¼¼ÊõʵÏֵ쬵«IE10ÒÔϲ»Ö§³Öwebsocket£¬¶ø¹úÄÚµÄ360¡¢2345ä¯

ÀÀÆ÷·â×°µÄÈ«²¿ÊÇIE10ÒÔϵÄÄںˣ¬¿¼Âǵ½ÍøÕ¾ÔÚ¹úÄڵĿͻ§£¬²»µÃ²»ÔÚ²»Ö§³ÖwebsocketʱºòÒ²ÒªÌṩͨѶ֧³Ö£¬ÓÚ

ÊǾö¶¨ÔÚ²»Ö§³ÖwebsocketµÄä¯ÀÀÆ÷ÉÏÓÃlong-pulling¼¼ÊõÌæ´ú¡£


¿ÉÐÐÐÔ·ÖÎö£º

Servlet 3.0ÒѾ­¿ªÊ¼Ö§³Öasync£¬Spring MVC 3.2Ò²¿ªÊ¼¶ÔÒì²½Ìṩ֧³Ö£¬ÓÚÊǽáºÏDeferredResultÀ´ÊµÏÖÁÄÌì¼¼Êõ¡£


¾ßÌåʵÏÖ£º

1 ÎļþÅäÖãº

¼ÙÉèÄãÒѾ­ÓÐÁËspring+springmvc¿ò¼Ü£¬ÎÒÃÇÖ»Ðè¶ÔÅäÖÃÎļþ×ö΢С¸Ä¶¯£¬ÒªÔÚweb.xmlÖеÄËùÓеÄfilter¼°servletÖÐÐèÒªÉùÃ÷ʹÓÃasync£º

  
   true
  
web.xmlÍêÕûÅäÖÃÈçÏ£º

  

  
	
   
	
    
    
     contextConfigLocation
     
    
     classpath:spring-mybatis.xml
     
   
	
    
    
     encodingFilter
     
    
     org.springframework.web.filter.CharacterEncodingFilter
     
    
     true
     
     
     
      encoding
      
     
      UTF-8
      
     
   
	
    
    
     encodingFilter
     
    
     /*
     
   
	
   
	
    
    
     org.springframework.web.context.ContextLoaderListener
     
   
	
    
    
     org.springframework.web.util.IntrospectorCleanupListener
     
   
	
    
    
     SpringMVC
     
    
     org.springframework.web.servlet.DispatcherServlet
     
     
     
      contextConfigLocation
      
     
      classpath:spring-mvc.xml
      
     
    
     1
     
    
     true
     
   
	
    
    
     SpringMVC
     
    
     /
     
   	
	
    
    
     index.
     jsp
     
   
	

  
ÎļþÅäÖø㶨¡£

2 ½¨Á¢¿ØÖÆÆ÷Controller

/**
 * @×÷Õß yyp
 * @ÎļþÃû ChatController.java
 * @×÷Óà ´¦ÀíÁÄÌìÏûÏ¢
 * @Blog http://blog.csdn.net/gisredevelopment
 */
@Controller
public class ChatController {
	//´æ·ÅËùÓеÄÓû§ÇëÇó
	private final Map
  
   > chatRequests = new ConcurrentHashMap
   
    >(); //ʱ¼ä¸ñʽ»¯ private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * @×÷Õß yyp * @×÷Óà µÇ¼ * @param name Óû§Ãû * @param session »á»° * @return ÁÄÌìÊÒÒ³Ãæ */ @RequestMapping(value = "/login", method = RequestMethod.POST) public String login(@RequestParam String name, HttpSession session){ session.setAttribute("user", name); Message msg = new Message(); msg.setUser("ϵͳ"); msg.setDate(sdf.format(new Date())); msg.setContent(name + "ÒѼÓÈë"); //֪ͨËùÓÐÓû§ÓÐÈ˽øÈëÁÄÌìÊÒ processMessage(msg); return "room"; } /** * * @×÷Õß yyp * @×÷Óà ¶ÁÈ¡×îÐÂÏûÏ¢ * @param session »á»° * @return DeferredResult
    
      */ @RequestMapping(value = "/getMessages", method = RequestMethod.GET) @ResponseBody public DeferredResult
     
       getMessages(HttpSession session){ //È¡³öµ±Ç°µÇ¼Óû§ final String user = (String)session.getAttribute("user"); //´´½¨DeferredResult
      
        DeferredResult
       
         dr = new DeferredResult
        
         (); //ÈôÓû§²»´æÔÚÔòÖ±½Ó·µ»Ø£¬·ñÔò½«Æä·ÅÈëÓû§ÇëÇóÁбíÖÐÈ»ºó·µ»Ø if(null == user){ return dr; }else{ //µ±DeferredResult¶Ô¿Í»§¶ËÏìÓ¦ºó½«Æä´ÓÁбíÖÐÒÆ³ý dr.onCompletion(new Runnable() { @Override public void run() { // TODO ×Ô¶¯Éú³ÉµÄ·½·¨´æ¸ù chatRequests.remove(user); } }); chatRequests.put(user, dr); return dr; } } /** * @×÷Õß yyp * @×÷Óà ½ÓÊÕ¿Í»§¶ËÏûÏ¢ * @param session »á»° * @param content ÏûÏ¢ÄÚÈÝ * @return Map
         
           */ @RequestMapping(value = "/setMessage", method = RequestMethod.POST) @ResponseBody public Map
          
            setMessage(HttpSession session, @RequestParam String content){ Message msg = new Message(); msg.setContent(content); msg.setDate(sdf.format(new Date())); msg.setUser((String)session.getAttribute("user")); //·¢²¼ÏûÏ¢¸øËùÓÐÓû§ processMessage(msg); Map
           
             map = new HashMap
            
             (1); map.put("success", "true"); return map; } /** * @×÷Õß yyp * @×÷Óà Í˳öÁÄÌìÊÒ * @param session »á»° * @return Map
             
               */ @RequestMapping(value = "/logout", method = RequestMethod.GET) @ResponseBody public Map
              
                logout(HttpSession session){ Message msg = new Message(); String user = (String)session.getAttribute("user"); msg.setContent("ÒÑÀ뿪"); msg.setDate(sdf.format(new Date())); msg.setUser(user); chatRequests.remove(user); //֪ͨËùÓÐÓû§ÓÐÈËÀ뿪ÁÄÌìÊÒ processMessage(msg); Map
               
                 map = new HashMap
                
                 (1); map.put("success", "true"); return map; } /** * @×÷Õß yyp * @×÷Óà ½«ÏûÏ¢ÐÅÏ¢·¢²¼¸øËùÓÐÔÚÏßÓû§ * @para