diff --git a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java index a9d3bc05c..6d866ace8 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java +++ b/gemp-lotr/gemp-lotr-web/src/main/java/com/gempukku/lotro/server/ServerResource.java @@ -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(); diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html b/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html index a3ff0e4a2..b79079cdb 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/index.html @@ -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); }); - + +
+ +
+
Create game:
First player name:
@@ -52,5 +64,6 @@
+ diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/chat.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/chat.js new file mode 100644 index 000000000..aecac7d03 --- /dev/null +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/chat.js @@ -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 = $("
"); + this.chatTalkDiv = $(""); + + 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("

" + message + "

"); + 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("" + from + ":" + text); + } + } + } +}); \ No newline at end of file diff --git a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js index f50487940..d6df2a7bd 100644 --- a/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js +++ b/gemp-lotr/gemp-lotr-web/src/main/webapp/js/communication.js @@ -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" + }); } });