Nachdem ich den Chat von Primefaces (PrimeFaces - ShowCase) zum laufen gebracht habe (das überhaupt mal etwas angezeigt wird) bekomme ich einfach keine Anzeige der Nachrichten die ich versende.. nein ich werd nicht einmal eingeloggt!
Wichtig: damit dieser Chat funktioniert müsst ihr Primefaces 3.4 verwenden!
Vllt wisst ihr eine andere JSF Alternative zu diesem Chat - wäre euch sehr Dankbar!
.Domii
Wichtig: damit dieser Chat funktioniert müsst ihr Primefaces 3.4 verwenden!
HTML:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
template="../templates/ui.xhtml">
<ui:define name="head">
<style type="text/css">
.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>
<script type="text/javascript">
//<![CDATA[
function handleMessage(data) {
var chatContent = $(PrimeFaces.escapeClientId('form:public'));
chatContent.append(data + '<br />');
//keep scroll
chatContent.scrollTop(chatContent.height());
}
//]]>
</script>
</ui:define>
<ui:define name="content">
<h1 class="title ui-widget-header ui-corner-all">PrimePush - Chat</h1>
<div class="entry">
<p>Chat is a simple push based application created with PrimePush.</p>
<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="#{chatUsers.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">
<p:ajax event="message" update=":form:users" />
</p:socket>
</div>
</ui:define>
</ui:composition>
Java:
/*
* Copyright 2009-2012 Prime Teknoloji.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package view;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
public class ChatUsers {
private List<String> users;
@PostConstruct
public void init() {
this.users = new ArrayList<String>();
}
public List<String> getUsers() {
return users;
}
public void setUsers(List<String> users) {
this.users = users;
}
public void addUser(String user) {
this.users.add(user);
}
public void removeUser(String user) {
this.users.remove(user);
}
public boolean contains(String user) {
return this.users.contains(user);
}
}
Java:
/*
* Copyright 2009-2012 Prime Teknoloji.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* [url]http://www.apache.org/licenses/LICENSE-2.0[/url]
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package view;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.context.RequestContext;
import org.primefaces.push.PushContext;
import org.primefaces.push.PushContextFactory;
public class ChatView {
private final PushContext pushContext = PushContextFactory.getDefault().getPushContext();
private ChatUsers users;
private String privateMessage;
private String globalMessage;
private String username;
private boolean loggedIn;
private String privateUser;
private final static String CHANNEL = "/chat/";
public void setUsers(ChatUsers 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, "[PM] " + 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.addUser(username);
pushContext.push(CHANNEL + "*", username + " joined the channel.");
requestContext.execute("subscriber.connect('/" + username + "')");
loggedIn = true;
}
}
public void disconnect() {
//remove user and update ui
users.removeUser(username);
RequestContext.getCurrentInstance().update("form:users");
//push leave information
pushContext.push(CHANNEL + "*", username + " left the channel.");
//reset state
loggedIn = false;
username = null;
}
}
Vllt wisst ihr eine andere JSF Alternative zu diesem Chat - wäre euch sehr Dankbar!
.Domii