Pessoal depois de muita luta consegui fazer funcionar o chat do show case do primefaces, mas tem outro problema
o chat só funciona local ou na rede local da empresa se eu colocar em um servidor as mensagens não são enviadas, e não aparece erro nenhum.
O código:
index.xhtml:
[code]<?xml version='1.0' encoding='UTF-8' ?>
Facelet Title .messageInput { width:400px; } .publicColumn {
width:80%;
}
.usersColumn {
width:20%;
}
.vtop {
vertical-align: top;
}
.chatlogs {
height:200px;
overflow:auto;
padding: 0.5em 1em 0.5em 0.5em;
}
.usersList {
height:200px;
overflow:auto;
}
.usersList ul {
list-style-type: none;
padding-left:10px;
}
.usersList ul li {
margin-bottom: 2px;
}
.usersList .ui-button-text {
padding:0;
}
</style>
</h:head>
<h:body>
<f:view>
<p:growl id="growl" showDetail="true" />
<h:form id="form">
<p:fieldset id="container" legend="PrimeChat" toggleable="true">
<h:panelGroup rendered="#{chatView.loggedIn}">
<h:panelGrid columns="2" columnClasses="publicColumn,usersColumn" style="width:100%">
<p:outputPanel id="public" layout="block" styleClass="ui-corner-all ui-widget-content chatlogs"/>
<p:dataList id="users" var="user" value="#{chatView.users}" styleClass="usersList">
<f:facet name="header">
Users
</f:facet>
<p:commandButton title="Chat" icon="ui-icon-comment" oncomplete="pChat.show()" update=":form:privateChatContainer">
<f:setPropertyActionListener value="#{user}" target="#{chatView.privateUser}" />
</p:commandButton>
#{user}
</p:dataList>
</h:panelGrid>
<p:separator />
<p:inputText value="#{chatView.globalMessage}" styleClass="messageInput" />
<p:spacer width="5" />
<p:commandButton value="Send" actionListener="#{chatView.sendGlobal}" oncomplete="$('.messageInput').val('').focus()"/>
<p:spacer width="5" />
<p:commandButton value="Disconnect" actionListener="#{chatView.disconnect}" global="false" update="container" />
</h:panelGroup>
<h:panelGroup rendered="#{not chatView.loggedIn}" >
Username: <p:inputText value="#{chatView.username}" />
<p:spacer width="5" />
<p:commandButton value="Login" actionListener="#{chatView.login}" update="container"
icon="ui-icon-person" />
</h:panelGroup>
</p:fieldset>
<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: #{chatView.privateUser}" />
<p:inputTextarea id="pChatInput" value="#{chatView.privateMessage}" rows="5" cols="30" />
<p:spacer />
<p:commandButton value="Send" actionListener="#{chatView.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) {
var chatContent = $(PrimeFaces.escapeClientId('form:public'));
chatContent.append(data + '<br />');
//keep scroll
chatContent.scrollTop(chatContent.height());
}
</script>
</f:view>
</h:body>
[/code]
O bean:
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;
@ManagedBean
@SessionScoped
public class ChatView {
private final PushContext pushContext = PushContextFactory.getDefault().getPushContext();
private static List<String> users;
private String privateMessage;
private String globalMessage;
private String username;
private boolean loggedIn;
private String privateUser;
private final static String CHANNEL = "/chat/";
static{
users = new ArrayList<String>();
}
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> users) {
this.users = users;
}
public String getPrivateUser() {
return privateUser;
}
public void setPrivateUser(String privateUser) {
this.privateUser = privateUser;
}
public String getGlobalMessage() {
return globalMessage;
}
public void setGlobalMessage(String globalMessage) {
this.globalMessage = globalMessage;
}
public String getPrivateMessage() {
return privateMessage;
}
public void setPrivateMessage(String privateMessage) {
this.privateMessage = privateMessage;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public boolean isLoggedIn() {
return loggedIn;
}
public void setLoggedIn(boolean loggedIn) {
this.loggedIn = loggedIn;
}
public void sendGlobal() {
pushContext.push(CHANNEL + "*", username + ": " + globalMessage);
globalMessage = null;
}
public void sendPrivate() {
pushContext.push(CHANNEL + privateUser, "[MP] " + username + ": " + privateMessage);
privateMessage = null;
}
public void login() {
RequestContext requestContext = RequestContext.getCurrentInstance();
if(users.contains(username)) {
loggedIn = false;
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username taken", "Try with another username."));
requestContext.update("growl");
}
else {
users.add(username);
pushContext.push(CHANNEL, username + " entrou no chat.");
requestContext.execute("subscriber.connect('/" + username + "')");
loggedIn = true;
}
}
public void disconnect() {
//remove user and update ui
users.remove(username);
RequestContext.getCurrentInstance().update("form:users");
//push leave information
pushContext.push(CHANNEL, username + " saiu do chat.");
//reset state
loggedIn = false;
username = null;
}
}
O web.xml:
[code]<?xml version="1.0" encoding="UTF-8"?>
javax.faces.PROJECT_STAGE
Development
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
/faces/
30
faces/index.xhtml
Push Servlet
org.primefaces.push.PushServlet
Push Servlet
/primepush/
[/code]
Será que alguém sabe qual o problema?