Browse files

hook up socket.io on the client

  • Loading branch information...
1 parent 9de35a8 commit 61a02a5a7495fd0534555d16e1ae8d25c614f179 @dmcaulay committed May 15, 2012
Showing with 11 additions and 0 deletions.
  1. +10 −0 public/client.js
  2. +1 −0 public/index.html
View
10 public/client.js
@@ -5,6 +5,7 @@ jQuery(function( $ ) {
this.ENTER_KEY = 13;
this.cacheElements();
this.bindEvents();
+ this.setupSocketIo();
this.$username.focus();
},
cacheElements: function() {
@@ -17,6 +18,13 @@ jQuery(function( $ ) {
this.$username.on('keyup', this.joinChat);
this.$newMessage.on('keyup', this.sendMessage );
},
+ setupSocketIo: function()
+ {
+ this.socket = io.connect();
+ this.socket.on('joined', this.userJoined);
+ this.socket.on('message', this.messageReceived);
+ this.socket.on('left', this.userLeft);
+ },
getVal: function(e, cb) {
var $el = $(e.target)
, val = $.trim($el.val());
@@ -31,6 +39,7 @@ jQuery(function( $ ) {
App.$newMessage.removeClass('hidden');
App.$newMessage.focus();
App.username = val;
+ App.socket.emit('joined', {username:val});
});
},
sendMessage: function(e) {
@@ -41,6 +50,7 @@ jQuery(function( $ ) {
};
$el.val('');
App.messageReceived(message);
+ App.socket.emit('message', message);
});
},
userJoined: function(message) {
View
1 public/index.html
@@ -7,6 +7,7 @@
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
+ <script src="/socket.io/socket.io.js"></script>
<script src="./client.js"></script>
<div id="header">Chat</div>

0 comments on commit 61a02a5

Please sign in to comment.