Permalink
Browse files

prove con logging solo da server.js

  • Loading branch information...
1 parent 4d7038a commit ff0ff41977d052705bd1f3364a30712550a3ed44 @mikosh75 committed Oct 12, 2011
Showing with 17 additions and 298 deletions.
  1. +0 −49 demo/web/index2.html
  2. +12 −0 demo/web/js/client.js
  3. +0 −247 demo/web/js/client2.js
  4. +1 −1 lib/channel.js
  5. +4 −1 lib/server.js
View
@@ -1,49 +0,0 @@
-<!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
@@ -174,6 +174,16 @@ $(function() {
.focus();
}
+ function randomString(len, charSet) {
+ charSet = charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
+ var randomString = '';
+ for (var i = 0; i < len; i++) {
+ var randomPoz = Math.floor(Math.random() * charSet.length);
+ randomString += charSet.substring(randomPoz,randomPoz+1);
+ }
+ return randomString;
+ }
+
var login = $("#login");
login.submit(function() {
var nick = $.trim($("#nick").val());
@@ -184,6 +194,8 @@ $(function() {
return false;
}
+// var nick = "user_" + randomString(5);
+
channel.join(nick, {
success: function() {
$("body")
View
@@ -1,247 +0,0 @@
-(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);
View
@@ -11,7 +11,7 @@ function Channel(options) {
this.basePath = options.basePath;
this.messageBacklog = parseInt(options.messageBacklog) || 200;
- this.sessionTimeout = (parseInt(options.sessionTimeout) || 60) * 1000;
+ this.sessionTimeout = (parseInt(options.sessionTimeout) || 180) * 1000;
this.nextMessageId = 0;
this.messages = [];
View
@@ -97,8 +97,12 @@ var handlers = [
channel.query(since, function(messages) {
session.poke();
response.simpleJSON(200, { messages: messages });
+ if (messages[0]['type'] == "msg") {
+ sys.puts(session.id + " " + messages[0]['nick'] + " " + messages[0]['type'] + " " + messages[0]['text']);
+ }
});
+
} },
{ path: "/send", handler: function(channel, request, response) {
var query = qs.parse(url.parse(request.url).query),
@@ -118,7 +122,6 @@ var handlers = [
session.poke();
var id = channel.appendMessage(session.nick, "msg", text);
response.simpleJSON(200, { id: id });
- sys.puts(session.id + " " + text);
} }
];

0 comments on commit ff0ff41

Please sign in to comment.