2012-10-15 112 views
2

我的目標是在頁面加載訂閱聊天兩個用戶(教師和學生),他們可以將消息發送到每個人,所以這裏就是我所做的:PrimePush聊天不工作

1 BackingBean:

package com.myapp.beans; 

import org.primefaces.context.RequestContext; 
import org.primefaces.push.PushContext; 
import org.primefaces.push.PushContextFactory; 
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 

import com.ocpsoft.pretty.faces.annotation.URLMapping; 
import com.xeno.xenoTemplate.utils.constants.Pages; 

@Component("chatBean") 
@Scope("view") 
@URLMapping(id = Pages.CHAT, pattern = "/chat", viewId = "/faces/pages/users/chat.xhtml") 
public class ChatView { 

    private final PushContext pushContext = PushContextFactory.getDefault() 
      .getPushContext(); 

    private String privateMessage; 

    private String privateUser; 

    private final static String CHANNEL = "/chat/"; 

    private String student = "student"; 

    private String teacher = "teacher"; 

    private boolean pageLoaded; 

    private boolean studentLoggedIn; 

    private boolean teacherLoggedIn; 

    public void preRender() { 
     System.out.println("########### preRender CHAT BEAN #########"); 
     if (!pageLoaded) { // invoked first time page loaded 
      System.out.println("########### Invoking PreRender Code #########"); 
      if (!studentLoggedIn) { 
       System.out.println("########## STUDENT LOG IN ##########"); 
       RequestContext.getCurrentInstance().execute(
         "subscriber.connect('/" + student + "')"); 
       studentLoggedIn = true; 
      } 
      if (!teacherLoggedIn) { 
       System.out.println("########## TEACHER LOG IN ##########"); 
       RequestContext.getCurrentInstance().execute(
         "subscriber.connect('/" + teacher + "')"); 
       teacherLoggedIn = true; 
      } 
      pageLoaded = true; 
     } 
    } 

    public void sendPrivate() { 
     System.out.println("######### SEND PRIVATE ##########"); 
     pushContext.push(CHANNEL + privateUser, "[PM] " + "PRIVATE" + ": " 
       + privateMessage); 
     privateMessage = null; 
    } 

    public void setTeacher(String teacher) { 
     this.teacher = teacher; 
    } 

    public String getTeacher() { 
     return teacher; 
    } 

    public void setStudent(String student) { 
     this.student = student; 
    } 

    public String getStudent() { 
     return student; 
    } 

    public void setStudentLoggedIn(boolean studentLoggedIn) { 
     this.studentLoggedIn = studentLoggedIn; 
    } 

    public boolean isStudentLoggedIn() { 
     return studentLoggedIn; 
    } 

    public void setTeacherLoggedIn(boolean teacherLoggedIn) { 
     this.teacherLoggedIn = teacherLoggedIn; 
    } 

    public boolean isTeacherLoggedIn() { 
     return teacherLoggedIn; 
    } 

    public String getPrivateUser() { 
     return privateUser; 
    } 

    public void setPrivateUser(String privateUser) { 
     this.privateUser = privateUser; 
    } 

    public String getPrivateMessage() { 
     return privateMessage; 
    } 

    public void setPrivateMessage(String privateMessage) { 
     this.privateMessage = privateMessage; 
    } 

} 

,並在JSF頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ice="http://www.icesoft.com/icefaces/component" 
    xmlns:p="http://primefaces.org/ui" 
    xmlns:pretty="http://ocpsoft.com/prettyfaces" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:sec="http://www.springframework.org/security/tags" 
    xmlns:c="http://java.sun.com/jsp/jstl/core"> 

<h:head> 
    <title>Chat</title> 
</h:head> 

<h:body> 

    <ui:composition template="/pages/template/commonLayout.xhtml"> 

     <ui:define name="content"> 
     <f:event type="preRenderView" listener="#{chatBean.preRender}" /> 

      <h:form id="form"> 

       <p:fieldset id="container" legend="PrimeChat" toggleable="true"> 

        <h:panelGroup> 

         <h:panelGrid columns="2" columnClasses="publicColumn,usersColumn" 
          style="width:400px;"> 

         <p:outputPanel style="height: 100px;" id="public" layout="block" 
           styleClass="ui-corner-all ui-widget-content chatlogs" /> 

         <h:panelGroup id="users" styleClass="usersList"> 

           <p:commandButton id="private_send" title="Chat" icon="ui-icon-comment" 
            oncomplete="pChat.show()" update=":form:privateChatContainer"> 
            <f:setPropertyActionListener value="#{chatBean.student}" 
             target="#{chatBean.privateUser}" /> 
           </p:commandButton> 
         #{chatBean.student} 
         </h:panelGroup> 

         </h:panelGrid> 



        </h:panelGroup> 


       </p:fieldset> 

       <!-- Private Chat Dialog --> 
       <p:dialog widgetVar="pChat" header="Private Chat" modal="true" 
        showEffect="fade" hideEffect="fade"> 
        <h:panelGrid id="privateChatContainer" columns="2" 
         columnClasses="vtop,vtop"> 
         <p:outputLabel for="pChatInput" 
          value="To: #{chatBean.privateUser}" /> 
         <p:inputTextarea id="pChatInput" 
          value="#{chatBean.privateMessage}" rows="5" cols="30" /> 

         <p:spacer /> 
         <p:commandButton value="Send" 
          actionListener="#{chatBean.sendPrivate}" 
          oncomplete="pChat.hide()" /> 
        </h:panelGrid> 
       </p:dialog> 

      </h:form> 

      <p:socket onMessage="handleMessage" channel="/chat" 
       autoConnect="false" widgetVar="subscriber" /> 

      <script type="text/javascript"> 
       function handleMessage(data) { 
        alert('handleMessage'); 
        var chatContent = $(PrimeFaces 
          .escapeClientId('form:public')); 
        chatContent.append(data + '<br />'); 
        chatContent.scrollTop(chatContent.height()); 
       } 
      </script> 



     </ui:define> 

    </ui:composition> 

</h:body> 
</html> 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     version="3.0"> 

    <display-name>MyAPP</display-name> 

    <listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
    </listener> 

    <listener> 
    <listener-class> 
     org.springframework.web.context.request.RequestContextListener 
    </listener-class> 
    </listener> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
     classpath:META-INF/spring/applicationContext.xml 
     classpath:META-INF/spring/applicationSecurity.xml 
     </param-value> 

    </context-param> 

    <context-param> 
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name> 
    <param-value> 
    /WEB-INF/springsecurity.taglib.xml;/WEB-INF/utils.taglib.xml 
    </param-value> 
    </context-param> 


    <context-param> 
    <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name> 
    <param-value>false</param-value> 
    </context-param> 





    <!-- --> 
    <context-param> 
    <param-name>javax.faces.PROJECT_STAGE</param-name> 
    <param-value>Development</param-value> 
    </context-param> 

    <welcome-file-list> 
    <welcome-file>/</welcome-file> 
    </welcome-file-list> 

    <context-param> 
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>server</param-value> 
    </context-param> 

    <context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>resources.application</param-value> 
    </context-param> 


    <filter> 
     <filter-name>hibernateFilter</filter-name> 
     <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> 
     <init-param> 
     <param-name>sessionFactoryBeanName</param-name> 
     <param-value>sessionFactory</param-value>   
     </init-param>  
    </filter> 

    <filter-mapping> 
     <filter-name>hibernateFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>FORWARD</dispatcher> 
     <dispatcher>REQUEST</dispatcher> 
     <dispatcher>ERROR</dispatcher> 
    </filter-mapping> 

    <!-- Spring Security --> 
    <filter> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <filter-class> 
      org.springframework.web.filter.DelegatingFilterProxy 
     </filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>springSecurityFilterChain</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>FORWARD</dispatcher> 
     <dispatcher>REQUEST</dispatcher> 
     <dispatcher>ERROR</dispatcher> 
    </filter-mapping> 

    <!-- 
    <filter> 
     <filter-name>Pretty Filter</filter-name> 
     <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class> 
     <init-param> 
      <param-name>logLevel</param-name> 
      <param-value>ERROR</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
    <filter-name>Pretty Filter</filter-name> 
     <url-pattern>/*</url-pattern> 
     <dispatcher>FORWARD</dispatcher> 
     <dispatcher>REQUEST</dispatcher> 
     <dispatcher>ERROR</dispatcher> 
    </filter-mapping> 
    --> 

    <session-config> 
     <session-timeout>30</session-timeout> 
    </session-config> 



    <servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 

    <error-page> 
    <exception-type>java.lang.Throwable</exception-type> 
    <location>/error</location> 
    </error-page> 

    <error-page> 
    <error-code>400</error-code> 
    <location>/error</location> 
    </error-page> 

    <error-page> 
    <error-code>404</error-code> 
    <location>/pageNotFound</location> 
    </error-page> 

    <!-- PrimePush Servlet --> 

    <servlet> 
     <servlet-name>Push Servlet</servlet-name> 
     <servlet-class>org.primefaces.push.PushServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
     <init-param> 
      <param-name>org.atmosphere.cpr.broadcasterCacheClass</param-name> 
      <param-value>org.atmosphere.cache.HeaderBroadcasterCache</param-value> 
     </init-param> 
     <init-param> 
      <param-name>org.atmosphere.cpr.broadcasterClass</param-name> 
      <param-value>org.atmosphere.cpr.DefaultBroadcaster</param-value> 
     </init-param> 
     <init-param> 
      <param-name>org.atmosphere.cpr.broadcastFilterClasses</param-name> 
      <param-value>org.atmosphere.client.TrackMessageSizeFilter</param-value> 
     </init-param>  
     <init-param> 
      <param-name>org.atmosphere.cpr.sessionSupport</param-name> 
      <param-value>true</param-value> 
     </init-param> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Push Servlet</servlet-name> 
     <url-pattern>/primepush/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

我使用以下罐子

  1. JSF 2.1.10
  2. PrimeFaces 4.0-SNAPSHOT
  3. 的Tomcat 7.0.32
  4. 氣氛-runtime 1.0.1

HEADERS

Request URL:http://localhost:8080/MyAPP/chat 
Request Method:POST 
Status Code:200 OK 
Request Headersview source 
Accept:application/xml, text/xml, */*; q=0.01 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:282 
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
Cookie:JSESSIONID=2BB4298A4E291CCCBB80EE3A4A5624BC 
Faces-Request:partial/ajax 
Host:localhost:8080 
Origin:http://localhost:8080 
Referer:http://localhost:8080/PrimeTemplate/chat 
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11 
X-Requested-With:XMLHttpRequest 
Form Dataview URL encoded 
javax.faces.partial.ajax:true 
javax.faces.source:form:j_idt31 
javax.faces.partial.execute:@all 
form:j_idt31:form:j_idt31 
form:form 
form:j_idt24: 
form:container_collapsed:false 
form:pChatInput:aaaaaaaaaaaa 
javax.faces.ViewState:5079906166114901626:-433970771510989466 
Response Headersview source 
Cache-Control:no-cache 
Content-Length:190 
Content-Type:text/xml;charset=UTF-8 
Date:Thu, 18 Oct 2012 10:40:04 GMT 
Server:Apache-Coyote/1.1 
X-Powered-By:JSF/2.0 

迴應:

<?xml version='1.0' encoding='UTF-8'?> 
<partial-response><changes><update id="javax.faces.ViewState"><![CDATA[5079906166114901626:-433970771510989466]]></update></changes></partial-response> 

問題:

我可以打開聊天窗口,爲學生和輸入聊天信息,然後按ENTER鍵,代碼獲取在豆的方法sendPrivate,但JS handleMessage函數不稱爲一個因此,私人消息不會顯示在JSF頁面中,我在瀏覽器控制檯中不會出現錯誤。

請告知如何解決這個問題。

+0

downvote的原因是什麼? – perissf

+0

爲什麼你使用這樣一個過時的PF版本?然後還有一個快照? – Thor

+0

@Thor,它是primefaces的最新版本? –

回答

1

嘗試以下罐子添加到您的lib文件夾(如果您還沒有已經這樣做了):

大氣COMPAT-tomcat的-1.0.1.jar

大氣COMPAT-tomcat7-1.0.1 .jar

另外請記住,如果您作爲STUDENT連接到「學生」頻道,並且如果您將消息推送到「老師」頻道,則只有教師頻道會收到留言。

要測試您的聊天,您需要在一個瀏覽器中以學生身份登錄,並在另一個瀏覽器中以老師身份登錄。在這種情況下,當您在一個瀏覽器中將消息作爲學生推送給教師時,另一個瀏覽器中的教師將收到消息。