Skip to content
Browse files

primo commit con 2 channel

  • Loading branch information...
1 parent 3a5373a commit ccb2d1ad16aa71668c2f9d467e9ca974262a9f16 @mikosh75 committed Oct 12, 2011
Showing with 307 additions and 1 deletion.
  1. +11 −1 demo/chat.js
  2. +49 −0 demo/web/index2.html
  3. +247 −0 demo/web/js/client2.js
View
12 demo/chat.js
@@ -12,7 +12,17 @@ chatServer.listen(8001);
chatServer.addChannel({
basePath: "/chat"
}).addListener("msg", function(msg) {
- sys.puts("<" + msg.nick + "> " + msg.text);
+ sys.puts("1 <" + msg.nick + "> " + msg.text);
+}).addListener("join", function(msg) {
+ sys.puts(msg.nick + " join");
+}).addListener("part", function(msg) {
+ sys.puts(msg.nick + " part");
+});
+
+chatServer.addChannel({
+ basePath: "/chat2"
+}).addListener("msg", function(msg) {
+ sys.puts("2 <" + msg.nick + "> " + msg.text);
}).addListener("join", function(msg) {
sys.puts(msg.nick + " join");
}).addListener("part", function(msg) {
View
49 demo/web/index2.html
@@ -0,0 +1,49 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>NodeChat</title>
+
+ <link rel="stylesheet" type="text/css" href="css/layout.css">
+ <script src="js/jquery-1.4.2.js"></script>
+ <script src="js/nodechat.js"></script>
+ <script src="js/client2.js"></script>
+</head>
+<body class="login">
+ <header><img src="images/node-chat.png" width="71" height="10" alt="Node Chat" /></header>
+
+ <form id="login">
+ <h1>Welcome to Node-<strong>Chat</strong></h1>
+ <p>
+ <label for="nick">Please choose a nickname:</label>
+ <input type="text" id="nick" maxlength="50" autocomplete="off" />
+ </p>
+ </form>
+
+ <section id="channel">
+
+ <section id="frame">
+ <div id="chat-log">
+ </div>
+ </section>
+
+ <ul id="users" class="button_list">
+ </ul>
+
+ <form id="entry">
+ <fieldset>
+ <p><input type="text" name="message" value="" autocomplete="off" id="message" /></p>
+ <input type="submit" value="Send" id="Send" />
+ </fieldset>
+ </form>
+
+ <footer>
+ <p class="credits">
+ Node.js + jQuery Chat Demo by <a href="http://scottgonzalez.com">Scott González</a> <span class="pipe">|</span>
+ User Interface by <a href="http://dougneiner.com">Doug Neiner</a>
+ </p>
+ </footer>
+
+ </section>
+</body>
+</html>
View
247 demo/web/js/client2.js
@@ -0,0 +1,247 @@
+(function($) {
+
+var title = document.title,
+ colors = ["green", "orange", "yellow", "red", "fuschia", "blue"],
+ channel = nodeChat.connect("/chat2"),
+ log,
+ message;
+
+// TODO: handle connectionerror
+
+$(function() {
+ log = $("#chat-log");
+ message = $("#message");
+
+ // Add a button that can be easily styled
+ $("<a></a>", {
+ id: "submit",
+ text: "Send",
+ href: "#",
+ click: function(event) {
+ event.preventDefault();
+ $(this).closest("form").submit();
+ }
+ })
+ .appendTo("#entry fieldset");
+
+ // Add a message indicator when a nickname is clicked
+ $("#users").delegate("li", "click", function() {
+ message
+ .val($(this).text() + ": " + message.val())
+ .focus();
+ });
+});
+
+// new message posted to channel
+// - add to the chat log
+$(channel).bind("msg", function(event, message) {
+ var time = formatTime(message.timestamp),
+ row = $("<div></div>")
+ .addClass("chat-msg");
+
+ $("<span></span>")
+ .addClass("chat-time")
+ .text(time)
+ .appendTo(row);
+
+ $("<span></span>")
+ .addClass("chat-nick")
+ .text(message.nick)
+ .appendTo(row);
+
+ $("<span></span>")
+ .addClass("chat-text")
+ .text(message.text)
+ .appendTo(row);
+
+ row.appendTo(log);
+})
+// another user joined the channel
+// - add to the chat log
+.bind("join", function(event, message) {
+ var time = formatTime(message.timestamp),
+ row = $("<div></div>")
+ .addClass("chat-msg chat-system-msg");
+
+ $("<span></span>")
+ .addClass("chat-time")
+ .text(time)
+ .appendTo(row);
+
+ $("<span></span>")
+ .addClass("chat-nick")
+ .text(message.nick)
+ .appendTo(row);
+
+ $("<span></span>")
+ .addClass("chat-text")
+ .text("joined the room")
+ .appendTo(row);
+
+ row.appendTo(log);
+})
+// another user joined the channel
+// - add to the user list
+.bind("join", function(event, message) {
+ var added = false,
+ nick = $("<li></li>", {
+ "class": colors[0],
+ text: message.nick
+ });
+ colors.push(colors.shift());
+ $("#users > li").each(function() {
+ if (message.nick == this.innerHTML) {
+ added = true;
+ return false;
+ }
+ if (message.nick < this.innerHTML) {
+ added = true;
+ nick.insertBefore(this);
+ return false;
+ }
+ });
+ if (!added) {
+ $("#users").append(nick);
+ }
+})
+// another user left the channel
+// - add to the chat log
+.bind("part", function(event, message) {
+ var time = formatTime(message.timestamp),
+ row = $("<div></div>")
+ .addClass("chat-msg chat-system-msg");
+
+ $("<span></span>")
+ .addClass("chat-time")
+ .text(time)
+ .appendTo(row);
+
+ $("<span></span>")
+ .addClass("chat-nick")
+ .text(message.nick)
+ .appendTo(row);
+
+ $("<span></span>")
+ .addClass("chat-text")
+ .text("left the room")
+ .appendTo(row);
+
+ row.appendTo(log);
+})
+// another user left the channel
+// - remove from the user list
+.bind("part", function(event, message) {
+ $("#users > li").each(function() {
+ if (this.innerHTML == message.nick) {
+ $(this).remove();
+ return false;
+ }
+ });
+})
+
+// Auto scroll list to bottom
+.bind("join part msg", function() {
+ // auto scroll if we're within 50 pixels of the bottom
+ if (log.scrollTop() + 50 >= log[0].scrollHeight - log.height()) {
+ window.setTimeout(function() {
+ log.scrollTop(log[0].scrollHeight);
+ }, 10);
+ }
+});
+
+// handle login (choosing a nick)
+$(function() {
+ function loginError(error) {
+ login
+ .addClass("error")
+ .find("label")
+ .text(error + " Please choose another:")
+ .end()
+ .find("input")
+ .focus();
+ }
+
+ var login = $("#login");
+ login.submit(function() {
+ var nick = $.trim($("#nick").val());
+
+ // TODO: move the check into nodechat.js
+ if (!nick.length || !/^[a-zA-Z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+$/.test(nick)) {
+ loginError("Invalid Nickname.");
+ return false;
+ }
+
+ channel.join(nick, {
+ success: function() {
+ $("body")
+ .removeClass("login")
+ .addClass("channel");
+ message.focus();
+ },
+ error: function() {
+ loginError("Nickname in use.");
+ }
+ });
+
+ return false;
+ });
+ login.find("input").focus();
+});
+
+// handle sending a message
+$(function() {
+ $("#channel form").submit(function() {
+ channel.send(message.val());
+ message.val("").focus();
+
+ return false;
+ });
+});
+
+// update the page title to show if there are unread messages
+$(function() {
+ var focused = true,
+ unread = 0;
+
+ $(window)
+ .blur(function() {
+ focused = false;
+ })
+ .focus(function() {
+ focused = true;
+ unread = 0;
+ document.title = title;
+ });
+
+ $(channel).bind("msg", function(event, message) {
+ if (!focused) {
+ unread++;
+ document.title = "(" + unread + ") " + title;
+ }
+ });
+});
+
+// notify the chat server that we're leaving if we close the window
+$(window).unload(function() {
+ channel.part();
+});
+
+function formatTime(timestamp) {
+ var date = new Date(timestamp),
+ hours = date.getHours(),
+ minutes = date.getMinutes(),
+ ampm = "AM";
+
+ if (hours > 12) {
+ hours -= 12;
+ ampm = "PM";
+ }
+
+ if (minutes < 10) {
+ minutes = "0" + minutes;
+ }
+
+ return hours + ":" + minutes + " " + ampm;
+}
+
+})(jQuery);

0 comments on commit ccb2d1a

Please sign in to comment.
Something went wrong with that request. Please try again.