add(new Behavior() {
private static final long serialVersionUID = -2205036360048419129L;
@Override
public void renderHead(Component component, IHeaderResponse response) {
ChatDao dao = Application.getBean(ChatDao.class);
try {
StringBuilder sb = new StringBuilder();
for (ChatMessage m : dao.get(0, Integer.MAX_VALUE)) {
sb.append("addChatMessage(").append(getMessage(m).toString()).append(");");
}
if (sb.length() > 0) {
response.render(OnDomReadyHeaderItem.forScript(sb.toString()));
}
} catch (JSONException e) {
}
super.renderHead(component, response);
}
});
add(new WebMarkupContainer("messages").setMarkupId("messageArea"));
final Form<Void> f = new Form<Void>("sendForm");
f.add(new TextArea<String>("message", new PropertyModel<String>(ChatPanel.this, "message")).setOutputMarkupId(true));
f.add(new Button("send").add(new AjaxFormSubmitBehavior("onclick"){
private static final long serialVersionUID = -3746739738826501331L;
protected void onSubmit(AjaxRequestTarget target) {
ChatDao dao = Application.getBean(ChatDao.class);
ChatMessage m = new ChatMessage();
m.setMessage(message);
m.setSent(new Date());
m.setFromUser(Application.getBean(UsersDao.class).get(WebSession.getUserId()));
dao.update(m);
IWebSocketConnectionRegistry reg = IWebSocketSettings.Holder.get(getApplication()).getConnectionRegistry();
for (IWebSocketConnection c : reg.getConnections(getApplication())) {
try {
c.sendMessage(getMessage(m).toString());
} catch(Exception e) {