Permalink
Browse files

First import

  • Loading branch information...
1 parent 283b9ac commit f6e03d1db7d872a47905c33c70eeeb91d01cdecc Olivier BONNAURE committed Jul 30, 2010
View
@@ -0,0 +1,6 @@
+[submodule "lib/socket.io.client"]
+ path = lib/socket.io.client
+ url = git://github.com/LearnBoost/Socket.IO.git
+[submodule "lib/socket-io-node"]
+ path = lib/socket-io-node
+ url = git://github.com/solisoft/Socket.IO-node.git
View
Binary file not shown.
View
Binary file not shown.
View
@@ -0,0 +1,103 @@
+body {
+ background: #22252A;
+ font-family: "monospace", Helvetica, Arial, sans-serif;
+}
+#room {
+ position: absolute;
+ top: 150px;
+}
+#room, #users {
+ line-height: 30px;
+ color: #9F9686;
+}
+.chat {
+ position: absolute;
+ bottom: 80px;
+ left: 10px;
+ top: 190px;
+ right: 290px;
+ overflow-y: auto;
+ color: white;
+ padding: 10px;
+ background: #1B1D20;
+ border: 5px solid #000;
+ word-wrap: break-word;
+}
+.chat .from {
+ font-weight: bold;
+ color: #7F8794;
+ border-bottom: 1px solid #7F8794;
+ margin-top: 5px;
+}
+.chat .from .date {
+ float: right;
+ font-size: 0,9em;
+ font-weight: normal;
+}
+
+.room form input[type=text] {
+ position: absolute;
+ bottom: 40px;
+ left: 10px;
+ right: 290px;
+ font-size: 20px;
+ line-height: 25px;
+ background: #fff;
+ width: 65%;
+}
+
+.nicks {
+ position: absolute;
+ top: 190px;
+ bottom: 0;
+ right: 0;
+ width: 250px;
+ color: white;
+}
+input[type=submit] {
+ display: none;
+}
+#rooms {
+ position: absolute;
+ bottom: 0;
+ left: 0;
+ right: 0;
+ height: 30px;
+}
+#rooms ul {
+ margin: 0;
+ padding: 0;
+ color: white;
+}
+#rooms ul li {
+ float: left;
+ list-style: none;
+ margin: 0 3px;
+ padding: 0px 20px;
+ line-height: 30px;
+ background: #3F3B32;
+ cursor: pointer;
+}
+#rooms ul li.active {
+ background: #9F998D;
+}
+.help {
+ background: #000;
+ padding: 15px;
+ color: #CD5;
+}
+#rooms ul li.pm {
+ background: darkblue;
+}
+#rooms ul li.pm.active {
+ background: blue;
+}
+.nicks {
+ color: lightSteelBlue;
+}
+.nicks a {
+ color: white;
+}
+.notice {
+ color: #aaa;
+}
View
@@ -0,0 +1,27 @@
+<!doctype html>
+<html>
+ <head>
+ <meta http-equiv="X-UA-Compatible" content="chrome=1">
+ <title>Demo</title>
+ <script src="/public/js/jquery-1.4.2.min.js"></script>
+ <script src="/public/js/raphael-min.js"></script>
+ <script src="/public/js/underscore-min.js"></script>
+ <script src="/lib/socket.io.client/socket.io.js"></script>
+ <script src="/client-io.js"></script>
+ <link rel="stylesheet" type="text/css" href="/client-io.css">
+ </head>
+ <body>
+ <div class="help">
+ This is a Multi Rooms Chat Demo using node.js and socket.io
+ <br / >Usage :
+ <br / >/nick your_nick
+ <br / >/join a_new_room
+ <br / >/part
+ </div>
+
+ <audio id="audio_new_pm" src="audio/new_pm.wav" preload="auto"></audio>
+ <audio id="audio_msg" src="audio/sound.wav" preload="auto"></audio>
+
+ <div id="rooms"><ul></ul></div>
+ </body>
+</html>
View
@@ -0,0 +1,256 @@
+var conn_id = null;
+var debug = true;
+var nick = "";
+var room = "demo";
+var rooms = {};
+
+io.setPath('/lib/socket.io.client/');
+var socket = new io.Socket(null, {rememberTransport: false, port: 8090});
+socket.connect();
+
+socket.on('message', function(message){
+ console.info(message);
+ message = JSON.parse(message);
+ var data = message.msg.split(" ");
+
+ var msg_room = "";
+ if(message.room) msg_room = message.room;
+
+ switch(data[0]) {
+ case "/hello":
+ conn_id = data[1];
+ nick = "Guest " + conn_id;
+ if(nick != "") socket.send("/nick " + nick);
+ break;
+
+ case "/msg":
+ var text = data.slice(1).join(" ");
+ if(message.room == "/pm") { // Private Message
+ var id = message.to == undefined ? message.from : message.to;
+ if(text != "" || message.from == conn_id) {
+ if(rooms[id] == undefined) {
+ rooms[id] = {};
+ rooms[id]["last_user"] = "";
+ rooms[id]["nb"] = 0;
+ rooms[id]["type"] = "pm";
+ rooms[id]["to"] = message.name;
+ try { $("#audio_new_pm")[0].play(); } catch(e) {}
+ $('#rooms ul:first-child').append("<li class='pm' id='r_" + id + "'>@" + message.name + "</li>");
+ addNewRoom(id);
+ room = id;
+ }
+ if(text != "") {
+ try { $("#audio_msg")[0].play(); } catch(e) {}
+ if(rooms[id]["last_user"] != message.toname) {
+ var date = new Date(),
+ hour = date.getHours(),
+ min = date.getMinutes();
+ if(min < 10) min = "0" + min;
+ rooms[id]["last_user"] = message.toname;
+ $('#chat_' + id).append("<div class='from'><div class='date'>"+hour+":"+min+"</div>" + message.toname + "</div>");
+ }
+
+ $('#chat_' + id).append("<div>" + HTMLEncode(text) + "</div>");
+ scrollChat();
+ if(room != id) {
+ rooms[id]["nb"]++;
+ $("#r_" + id).html("@" + message.name + " (" + rooms[id]["nb"] + ")");
+ } else {
+ $("#r_" + id).html("@" + message.name);
+ }
+ }
+ }
+ }
+ else { // Normal message
+ try { $("#audio_msg")[0].play(); } catch(e) {}
+ if(rooms[message.room]["last_user"] != message.from) {
+ var date = new Date(),
+ hour = date.getHours(),
+ min = date.getMinutes();
+ if(min < 10) min = "0" + min;
+
+ rooms[message.room]["last_user"] = message.from;
+ $('#chat_' + message.room).append("<div class='from'><div class='date'>"+hour+":"+min+"</div>" + message.from + "</div>");
+ }
+ $('#chat_' + message.room).append("<div>" + HTMLEncode(data.slice(1).join(" ")) + "</div>");
+ scrollChat();
+ if(room != message.room) {
+ rooms[message.room]["nb"]++;
+ $("#r_" + message.room).html(message.room + " (" + rooms[message.room]["nb"] + ")");
+ } else {
+ $("#r_" + message.room).html(message.room);
+ }
+ }
+ break;
+
+ case "/your_nick":
+ nick = data.slice(1).join(" ");
+ if(rooms[room] == undefined) {
+ socket.send("/join " + room);
+ } else {
+ refreshList(room);
+ }
+ break;
+
+ case "/join":
+ rooms[room] = {};
+ rooms[room]["last_user"] = "";
+ rooms[room]["nb"] = 0;
+ if(nick == message.from) {
+ $('#room').html("Connected on room #" + room);
+ $('#rooms ul:first-child').append("<li id='r_" + room + "'>" + room + "</li>");
+ addNewRoom(room);
+ $('#chat_' + room).append("<div class='notice'>:: You are connected as " + nick + "</div>");
+ } else {
+ $('#chat_' + room).append("<div class='notice'>:: " + message.from + " joined this room</div>");
+ }
+ refreshList(room);
+
+ break;
+
+ case "/list":
+ _.each(data.slice(1).join(" ").split(","), function(data) {
+ n = data.split(":");
+ var nh = "#n_" + room + "_" + n[0];
+ if($(nh).html() == null) {
+ var value = n[1];
+ if(n[1] == "undefined" || n[1] == undefined) value = n[0];
+ if(n[0] == conn_id) $("#n_" + message.room).append("<div id='n_" + room + "_" +n[0]+"'>" + value +"</div>");
+ else $("#n_" + message.room).append("<div id='n_" + room + "_" +n[0]+"'><a href='#' onclick='socket.send(\"/pm "+ n[0]+"\")'>" + value +"</a></div>");
+ } else {
+ $(nh).html(value);
+ }
+ });
+ break;
+
+ case "/quit":
+ $("#n_" + msg_room + "_" + data[1]).detach();
+ $('#chat_' + msg_room).append("<div class='notice'>:: " + message.from + " left the room</div>");
+ break;
+
+ case "/part":
+ $("#n_" + msg_room + "_" + data[1]).detach();
+ $('#chat_' + msg_room).append("<div class='notice'>:: " + message.from + " left the room</div>");
+ break;
+
+ case "/writing":
+ $("#n_" + msg_room + "_" + data[1]).addClass("writing");
+ break;
+
+ case "/notice":
+ $('#chat_' +room).append("<div class='notice'>:: " + data.slice(1).join(" ") + " </div>");
+ break;
+
+ default:
+ break;
+ }
+});
+
+socket.on('connect', function(){
+ socket.send("/whoami");
+});
+
+function send(msg) {
+ var r = room;
+ if(msg[0] == "/") {
+ var can_send = true;
+ var data = msg.split(" ");
+ switch(data[0]) {
+ case "/join":
+ room = data[1].replace(/\W/g, "");
+ msg = "/join " + room;
+ can_send = nick == "" ? false : true;
+ break;
+ case "/part":
+ msg = msg + " " + room;
+ rooms[room] = undefined;
+ $('#r_' + room).detach();
+ $('#n_' + room).detach();
+ $("#chat_" + room).detach();
+ for(var r in rooms) {
+ if(rooms[r] != undefined) {
+ room = r;
+ displayRoom("r_" + room);
+ }
+ break;
+ }
+ break;
+
+ break;
+ }
+ if(can_send) socket.send(msg);
+ }
+ else {
+ if(nick != "") {
+ var txt = '/msg ' + room + " " + msg;
+ if(rooms[room].type == "pm") {
+ txt = "/pm " + room + " " + msg;
+ }
+ socket.send(txt);
+ }
+ }
+ $('#t_' + r).val("");
+}
+
+function scrollChat() {
+ var log = $("#chat_" + room);
+ log.scrollTop(log[0].scrollHeight + log.height());
+}
+
+function querySt(ji) {
+ hu = window.location.search.substring(1);
+ gy = hu.split("&");
+ for (i=0;i<gy.length;i++) {
+ ft = gy[i].split("=");
+ if (ft[0] == ji) {
+ return ft[1];
+ }
+ }
+}
+
+function HTMLEncode(wText){
+ if(typeof(wText)!="string"){
+ wText=wText.toString();
+ };
+ wText=wText.replace(/</g, "&lt;") ;
+ wText=wText.replace(/>/g, "&gt;") ;
+ return wText;
+};
+
+function displayRoom(r) {
+ r = r.split("_")[1];
+ room = r;
+ $(".room").css("display", "none");
+ $("#rooms ul li").removeClass("active");
+ $('#r_' + r).addClass("active");
+ if(rooms[r]["type"] == "pm") {
+ $('#r_' + r).html("@" + rooms[r]["to"]);
+ } else {
+ $('#r_' + r).html(r);
+ }
+ $('#n_' + r).html("");
+
+ socket.send("/list " + r);
+ $('#room_' + r).css("display", "block");
+ rooms[r]["nb"] = 0;
+ scrollChat();
+}
+
+function refreshList(r) {
+ $('#n_' + r).html("");
+ socket.send("/list " + r);
+}
+
+function addNewRoom(r) {
+ $('body').append("<div class='room' id='room_" + r + "'></div>");
+ $('#room_' + r).append("<div class='chat' id='chat_" + r + "'></div>");
+ $('#room_' + r).append('<form id="f_'+ r +'" action="send" method="post" onsubmit="send($(\'#t_' + r + '\').val()); return false;"></form>');
+ $('#room_' + r).append("<div class='nicks' id='n_"+ r +"'></div>");
+ $('#f_' + r).append('<input type="text" id="t_' + r + '" name="t" value="" width="50" />');
+ $('#f_' + r).append('<input type="submit" value="send" />');
+ $('.room').css("display","none");
+ $('#r_' + r).click(function() { displayRoom(this.id) });
+ $("#rooms ul li").removeClass("active");
+ $('#r_' + r).addClass("active");
+ $('#room_' + r).css("display", "block");
+}
Submodule socket-io-node added at f4c19c
Oops, something went wrong.

0 comments on commit f6e03d1

Please sign in to comment.