Permalink
Browse files

Finished chat

  • Loading branch information...
1 parent 98bfaff commit 03a6774aaa3dc6dee5b4306def436bbe6651a914 @pksunkara committed Sep 18, 2011
Showing with 82 additions and 48 deletions.
  1. +34 −1 app.js
  2. +0 −26 chat.js
  3. +0 −16 chat_client.html
  4. +43 −0 public/javascripts/collab.js
  5. +5 −5 views/code.jade
View
35 app.js
@@ -6,6 +6,8 @@ var express = require('express');
var app = module.exports = express.createServer();
+var io = require('socket.io').listen(app);
+
// Configuration
app.configure(function(){
@@ -51,8 +53,39 @@ app.get('/code/:id', function(req, res){
});
});
+// socket I/O functionality
+
+var sessions = [];
+
+io.sockets.on('connection', function(socket){
+ socket.emit('nickname?', {});
+ socket.on('nickname', function(data){
+ console.log(data, 'joined the session');
+ socket.emit('members', sessions);
+ io.sockets.emit('join', {name: data, msg: data+' has joined the session'});
+ socket.set('nickname', data);
+ sessions.push(data);
+ });
+ socket.on('chat', function(data){
+ socket.get('nickname', function(err, nickname){
+ console.log("msg from", nickname, ":", data);
+ io.sockets.emit('chat', nickname+': '+data);
+ });
+ });
+ socket.on('disconnect', function(){
+ socket.get('nickname', function(err, nickname){
+ console.log(nickname, 'quit the session');
+ io.sockets.emit('quit', {name: nickname, msg: nickname+' has quit the session'});
+ sessions.splice(sessions.indexOf(nickname), 1);
+ });
+ });
+})
+
+// Start listening to server
+
app.listen(3000);
-console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
+
+// Some useful functions
function randomString() {
var text = "";
View
26 chat.js
@@ -1,26 +0,0 @@
-
-var CHAT_PORT = 3000
-
-var
-io = require('socket.io').listen(CHAT_PORT),
-
-io.sockets.on('connection', socketHandler)
-
-function socketHandler(socket){
- socket.emit('asking name', {msg:'What\'s your name?'});
-
- socket.on('giving name', function(data){
- io.sockets.emit('message', {name:'GOD', msg:data.name + ' has joined the session'});
- socket.set('name', data.name)
- });
-
- socket.on('message', function(data){
- console.log("recieved a message from", data.from, "saying", data.msg);
- io.sockets.emit('message', {name:socket.get('name'), msg:data.msg});
- });
-
- socket.on('disconnect', function() {
- io.sockets.emit('message', {name:'GOD', msg:socket.get('name')+' has left the editing session'});
- });
-}
-
View
@@ -1,16 +0,0 @@
-<script src="/socket.io/socket.io.js"></script>
-<script>
-
-var socket = io.connect("http://localhost");
-socket.on('asking name', function(data){
- console.log(data);
- var nickname = prompt('Your nick ?');
- socket.emit('giving name', {name:nickname});
-});
-
-socket.on('message', function(data){
- // Also add the code to display the chat to the user in the chat window in this case
- console.log(data.name + ' : ' + data.msg);
-});
-
-</script>
@@ -0,0 +1,43 @@
+var socket = io.connect(window.location.origin);
+var members = [];
+
+updateMembers = function() {
+ var e = document.getElementsByClassName('members')[0];
+ var r = members[0];
+ for(i=1;i<members.length;i++)
+ r += "\n"+members[i];
+ e.innerHTML = r;
+}
+
+putChat = function(msg) {
+ var e = document.getElementsByClassName('chatbox')[0];
+ e.innerHTML = e.innerHTML+msg+"\n";
+}
+
+sendChat = function() {
+ var e = document.getElementsByClassName('chat')[0];
+ socket.emit('chat', e.value);
+ e.value = '';
+}
+
+socket.on('nickname?', function(data){
+ var nickname = prompt('Your nickname?');
+ socket.emit('nickname', nickname);
+ socket.on('members', function(data){
+ members = data;
+ updateMembers();
+ });
+ socket.on('join', function(data){
+ members.push(data.name);
+ updateMembers();
+ putChat(data.msg);
+ });
+ socket.on('quit', function(data){
+ members.slice(members.indexOf(data.name)+1);
+ updateMembers();
+ putChat(data.msg);
+ });
+ socket.on('chat', function(data){
+ putChat(data);
+ });
+});
View
@@ -1,6 +1,8 @@
script(src='/javascripts/ace/ace.js')
script(src='/javascripts/ace/theme-twilight.js')
script(src='/javascripts/ace/mode-javascript.js')
+script(src='/socket.io/socket.io.js')
+script(src='/javascripts/collab.js')
script(type='text/javascript')
window.onload = function() {
var editor = ace.edit("editor");
@@ -65,11 +67,9 @@ script(type='text/javascript')
| });
|
| app.listen(3000);
- | console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
.span3
pre.members
- | pksunkara
- | gsiddardha
- | nihar
pre.chatbox
- input.chat
+ form(action='',onsubmit='sendChat(); return false;')
+ input.chat
+ input(type='submit',style='display:none;')

0 comments on commit 03a6774

Please sign in to comment.