Working on chat.

This commit is contained in:
marcins78@gmail.com
2011-09-12 16:40:20 +00:00
parent e86f8b5dc9
commit 1de10bf5fa
4 changed files with 104 additions and 4 deletions

View File

@@ -45,12 +45,12 @@ public class ServerResource {
_logger.debug("starting resource");
try {
_lotroServer = new LotroServer();
_lotroServer.startServer();
_chatServer = new ChatServer();
_chatServer.startServer();
_chatServer.createChatRoom("default");
_lotroServer = new LotroServer(_chatServer);
_lotroServer.startServer();
} catch (RuntimeException exp) {
_logger.error("Error while creating resource", exp);
exp.printStackTrace();

View File

@@ -38,12 +38,24 @@
$("#result").empty().append(data);
});
});
var comm = new GempLotrCommunication("/gemp-lotr/server",
function() {
alert("There was problem with communication with the server");
});
var chat = new ChatBoxUI("default", $("#chat"), comm);
chat.setSize(800, 300);
});
</script>
</head>
<body>
<body>
<div id="chat" style="width:800px;height:300px;">
</div>
<form action="/" id="createGameForm">
<b>Create game:</b><br/>
First player name: <input type="text" name="player1"/><br/>
@@ -52,5 +64,6 @@
</form>
<!-- the result of the search will be rendered inside this div -->
<div id="result"></div>
</body>
</html>

View File

@@ -0,0 +1,50 @@
var ChatBoxUI = Class.extend({
name: null,
div: null,
communication: null,
chatMessagesDiv: null,
chatTalkDiv: null,
init: function(name, div, communication) {
this.name = name;
this.div = div;
this.communication = communication;
this.chatMessagesDiv = $("<div class='chatMessages'></div>");
this.chatTalkDiv = $("<input class='chatTalk'>");
this.div.append(this.chatMessagesDiv);
this.div.append(this.chatTalkDiv);
var that = this;
this.communication.startChat(this.name, function(xml) {
that.processMessages(xml);
})
},
setSize: function(width, height) {
this.chatMessagesDiv.css({ position: "absolute", left: 0 + "px", top: 0 + "px", width: width, height: height - 30, overflow: "scroll" });
this.chatTalkDiv.css({ position: "absolute", left: 0 + "px", top: (height - 30) + "px", width: width, height: 30 });
},
appendMessage: function(message) {
this.chatMessagesDiv.append("<p>" + message + "</p>");
if ($("p", this.chatMessagesDiv).length > 50) {
$("p", this.chatMessagesDiv).first().remove();
}
},
processMessages: function(xml) {
var root = xml.documentElement;
if (root.tagName == 'chat') {
var messages = element.getElementsByTagName("message");
for (var i = 0; i < messages.length; i++) {
var message = messages[i];
var from = message.getAttribute("from");
var text = message.childNodes[0].nodeValue;
this.appendMessage("<b>" + from + ":</b>" + text);
}
}
}
});

View File

@@ -94,5 +94,42 @@ var GempLotrCommunication = Class.extend({
error: this.failure,
dataType: "xml"
});
},
startChat: function(room, callback) {
$.ajax({
type: "GET",
url: this.url + "/chat/" + room,
cache: false,
data: {
participantId: getUrlParam("participantId")},
success: callback,
error: this.failure,
dataType: "xml"
});
},
updateChat: function(room, callback) {
$.ajax({
type: "POST",
url: this.url + "/chat/" + room,
cache: false,
data: {
participantId: getUrlParam("participantId")},
success: callback,
error: this.failure,
dataType: "xml"
});
},
sendChatMessage: function(room, message, callback) {
$.ajax({
type: "POST",
url: this.url + "/chat/" + room,
cache: false,
data: {
participantId: getUrlParam("participantId"),
message: message},
success: callback,
error: this.failure,
dataType: "xml"
});
}
});