Permalink
Browse files

Added socket.io wrapper class and rewrote use of socket in controller

  • Loading branch information...
1 parent be4eb0b commit 2b17b473f029db23a8d44bf7c6200077f93902a8 Nils Dehl committed Dec 14, 2010
Showing with 147 additions and 46 deletions.
  1. +7 −0 .rockstarapps
  2. +106 −22 js/build/app.all.js
  3. +1 −1 js/build/app.all.js.min.js
  4. +1 −1 js/build/app.all.js.ycomp.js
  5. +32 −22 js/conrollers/Controller.Chat.js
View
@@ -11,6 +11,12 @@
+
+
+
+
+
+
<file name="js/build/app.all.js">
<min>true</min>
<comp>true</comp>
@@ -27,6 +33,7 @@
<file>js/lib/ext.util.md5.js</file>
<file>js/lib/socket.io.js</file>
<file>js/index.js</file>
+ <file>js/lib/App.util.Socketio.js</file>
<file>js/conrollers/Controller.Viewport.js</file>
<file>js/conrollers/Controller.Chat.js</file>
<file>js/conrollers/Controller.Config.js</file>
View
@@ -2109,6 +2109,80 @@ Ext.regApplication('App', {
*/
/*
+ * START OF FILE - /ST-Websocket-Mobile-Chat/js/lib/App.util.Socketio.js
+ */
+Ext.ns('App.util');
+/**
+ * Socket.io wrapper class
+ * @class App.util.Socketio
+ * @extends Ext.util.Observable
+ */
+App.util.Socketio = Ext.extend(Ext.util.Observable, {
+
+ constructor: function(host, options){
+
+ options = options || {};
+
+
+ App.util.Socketio.superclass.constructor.call(
+ this
+ );
+
+ this.socket = new io.Socket(host, options);
+ var that = this;
+
+ this.socket.on('connect', function(){
+ that.onConnect();
+ });
+ this.socket.on('message', function(data){
+ that.onMessage(data);
+ });
+ this.socket.on('close', function(){
+ that.onClose();
+ });
+ this.socket.on('disconnect', function(){
+ that.onDisconnect();
+ });
+
+ },
+
+ /**
+ * connect
+ */
+ connect: function() {
+ this.socket.connect();
+ },
+
+ disconnect: function(){
+ this.socket.disconnect();
+ },
+
+ send: function(message) {
+ this.socket.send(message);
+ },
+
+ onConnect: function() {
+ this.fireEvent('connect');
+ },
+
+ onDisconnect: function() {
+ this.fireEvent('disconnect');
+ },
+
+ onClose: function() {
+ this.fireEvent('close');
+ },
+
+ onMessage: function(message) {
+ this.fireEvent('message', message);
+ }
+});
+Ext.reg('App.util.Socketio', App.util.Socketio);
+/*
+ * END OF FILE - /ST-Websocket-Mobile-Chat/js/lib/App.util.Socketio.js
+ */
+
+/*
* START OF FILE - /ST-Websocket-Mobile-Chat/js/conrollers/Controller.Viewport.js
*/
/**
@@ -2205,40 +2279,50 @@ Ext.regController('Chat', {
*
*/
initSocketConnection: function() {
- var chatStore = new App.Store.Chat();
- var configStore = Ext.StoreMgr.get('ConfigStore');
- var settings = configStore.getAt(0);
- var user = {
- nickname: settings.get('nickname'),
- gravatar: settings.get('gravatar')
- };
+ this.chatStore = new App.Store.Chat();
+ this.configStore = Ext.StoreMgr.get('ConfigStore');
+ var settings = this.configStore.getAt(0);
- var socket = new io.Socket(settings.get('server'), {port: 4000});
- this.socket = socket;
+ this.socket = new App.util.Socketio(settings.get('server'), {port: 4000});
+ this.socket.connect();
- socket.connect();
-
- socket.on('connect',
- function() {
- socket.send(user);
- },
+ // Event Listener
+ this.socket.on(
+ 'connect',
+ this.registerUser,
this
);
-
- socket.on('message', function(message) {
- chatStore.add(message);
- });
+ this.socket.on(
+ 'message',
+ this.addMessageToChatStore,
+ this
+ );
App.on(
'newMsg',
- function(msg){
- this.socket.send(msg);
- },
+ this.sendMessageToServer,
this
);
},
+ sendMessageToServer: function(msg){
+ this.socket.send(msg);
+ },
+
+ addMessageToChatStore: function(message) {
+ this.chatStore.add(message);
+ },
+
+ registerUser: function() {
+ var settings = this.configStore.getAt(0);
+ var user = {
+ nickname: settings.get('nickname'),
+ gravatar: settings.get('gravatar')
+ };
+ this.socket.send(user);
+ },
+
/**
* Show chat view
*/

Large diffs are not rendered by default.

Oops, something went wrong.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -24,40 +24,50 @@ Ext.regController('Chat', {
*
*/
initSocketConnection: function() {
- var chatStore = new App.Store.Chat();
- var configStore = Ext.StoreMgr.get('ConfigStore');
- var settings = configStore.getAt(0);
- var user = {
- nickname: settings.get('nickname'),
- gravatar: settings.get('gravatar')
- };
-
- var socket = new io.Socket(settings.get('server'), {port: 4000});
- this.socket = socket;
+ this.chatStore = new App.Store.Chat();
+ this.configStore = Ext.StoreMgr.get('ConfigStore');
+ var settings = this.configStore.getAt(0);
- socket.connect();
+ this.socket = new App.util.Socketio(settings.get('server'), {port: 4000});
+ this.socket.connect();
- socket.on('connect',
- function() {
- socket.send(user);
- },
+ // Event Listener
+ this.socket.on(
+ 'connect',
+ this.registerUser,
this
);
-
- socket.on('message', function(message) {
- chatStore.add(message);
- });
+ this.socket.on(
+ 'message',
+ this.addMessageToChatStore,
+ this
+ );
App.on(
'newMsg',
- function(msg){
- this.socket.send(msg);
- },
+ this.sendMessageToServer,
this
);
},
+ sendMessageToServer: function(msg){
+ this.socket.send(msg);
+ },
+
+ addMessageToChatStore: function(message) {
+ this.chatStore.add(message);
+ },
+
+ registerUser: function() {
+ var settings = this.configStore.getAt(0);
+ var user = {
+ nickname: settings.get('nickname'),
+ gravatar: settings.get('gravatar')
+ };
+ this.socket.send(user);
+ },
+
/**
* Show chat view
*/

0 comments on commit 2b17b47

Please sign in to comment.