Skip to content
Browse files

update ptah_chat to latest api

  • Loading branch information...
1 parent 6156308 commit 8bf300902e1e8b8b5ce83c5bb51ae4aede1ce41b @fafhrd91 fafhrd91 committed Mar 27, 2012
View
11 ptah_chat/ptah_chat/app.py
@@ -3,7 +3,7 @@
from pyramid.session import UnencryptedCookieSessionFactoryConfig
import ptah
-from .chat import ChatSessionManager
+from .chat import ChatProtocol
session_factory = UnencryptedCookieSessionFactoryConfig('secret')
@@ -20,13 +20,12 @@ def main(global_config, **settings):
config.ptah_init_manage(managers = ['*'])
config.ptah_populate()
- # internal chat
- config.add_sockjs_route(
- 'chat', '/ws-chat',
- session_manager=ChatSessionManager('chat', config.registry))
+ # enable jca
+ config.register_jca_sm()
+ # internal chat
config.register_jca_component(
- 'chat', 'ex.Chat', 'ptah_chat:jca/')
+ 'chat', 'ptah_chat:jca/', protocol = ChatProtocol)
# we love them routes
config.add_route('root', '/')
View
59 ptah_chat/ptah_chat/chat.py
@@ -1,40 +1,36 @@
""" internal chat """
import ptah
from datetime import datetime
-from pyramid_jca import Session
-from pyramid_sockjs import SessionManager
+from pyramid_jca import Protocol
-class Session(Session):
+class ChatProtocol(Protocol):
def __init__(self, *args, **kw):
- super(Session, self).__init__(*args, **kw)
+ super(ChatProtocol, self).__init__(*args, **kw)
principal = ptah.auth_service.get_current_principal()
self.user_id = principal.__uri__.replace(':','_')
self.user_name = principal.name
def on_closed(self):
- self.manager.unassign(self)
+ super(ChatProtocol, self).on_closed()
found = False
- for uid, user in self.manager.list_users():
- if uid == self.user_id:
+ for user in self.instances.values():
+ if user.user_id == self.user_id:
found = True
break
if not found:
- for uid, user in self.manager.list_users():
- user.send('disconnected', {'uid': self.user_id})
+ self.broadcast('disconnected', {'uid': self.user_id})
def msg_init(self, data):
""" init message handler """
- self.manager.assign(self)
-
users = []
- for uid, user in self.manager.list_users():
- if uid != self.user_id:
- users.append({'uid': uid, 'name': user.user_name})
+ for user in self.instances.values():
+ if user.user_id != self.user_id:
+ users.append({'uid': user.user_id, 'name': user.user_name})
info = {'uid': self.user_id,
'name': self.user_name,
@@ -43,7 +39,7 @@ def msg_init(self, data):
msg = {'uid': self.user_id,
'name': self.user_name}
- self.manager.broadcast('joined', msg, tmpl='chat.user')
+ self.broadcast('joined', msg, tmpl='chat.user')
def msg_message(self, data):
""" 'message' message handler """
@@ -52,33 +48,6 @@ def msg_message(self, data):
'date': datetime.utcnow(),
'message': data['message']}
- for user in self.manager.get_user(data['uid']):
- user.send('message', msg)
-
-
-class ChatSessionManager(SessionManager):
-
- factory = Session
-
- def __init__(self, name, registry, **kw):
- super(ChatSessionManager, self).__init__(name, registry, **kw)
-
- self.users = {}
-
- def assign(self, session):
- data = self.users.setdefault(session.user_id, [])
- data.append(session)
-
- def unassign(self, session):
- data = self.users.get(session.user_id)
- if data and session in data:
- data.remove(session)
- if not data:
- del self.users[session.user_id]
-
- def get_user(self, uid):
- return self.users.get(uid)
-
- def list_users(self):
- for uid, data in self.users.items():
- yield uid, data[0]
+ for user in self.instances.values():
+ if user.user_id == data['uid']:
+ user.send('message', msg)
View
36 ptah_chat/ptah_chat/jca/chat.js
@@ -4,38 +4,22 @@ define(
function(jca, $) {
"use strict";
- var Chat = function(ca, container) {
- this.jca = ca
- this.container = container
- this.users = {}
- this.logger = jca.get_logger('Chat');
-
- this.conn = new jca.Connection(
- jca.gen_url('/ws-chat'), this,
- {autoconnect: true,
- logger: this.logger}
- )
- };
+ var Chat = jca.component('chat', {
- Chat.prototype = {
- toString: function() {
- return "Chat"
+ init: function() {
+ this.users = {}
+ this.logger = jca.get_logger('Chat');
},
-
+
on_connect: function() {
- var conn = this.conn;
this.container.append(this.templates.render('list'));
this.ws = $('#internal-chat');
- conn.send('init');
+ this.send('init');
this.ws.delegate('div.btn', 'click', this, this.on_user_click);
},
- on_disconnect: function() {
- },
-
- on_message: function(data, msg) {
- var type = msg.data['type'];
+ on_message: function(type, data, msg) {
if (type==='message') {
var win = this.users[data['uid']];
if (win) {
@@ -79,7 +63,7 @@ define(
that.users[uid].on_click();
};
}
- };
+ })
var ChatWindow = function(manager, uid, user) {
@@ -89,7 +73,7 @@ define(
this.uid = uid;
this.user = user;
this.data = {uid: uid, name: user};
- this.window;
+ this.window = null;
this.window_hidden = true;
this.manager.ws.append(this.templates.render('user', this.data));
@@ -171,7 +155,7 @@ define(
);
this.messages.animate({scrollTop: this.messages.height()});
- this.manager.conn.send('message', {uid: this.uid, message: msg})
+ this.manager.send('message', {uid: this.uid, message: msg})
el.val('');
},
View
4 ptah_chat/ptah_chat/templates/layout.pt
@@ -4,10 +4,10 @@
<title>Ptah Chat Example</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
+ ${view.include('curl')}
${view.include('bootstrap')}
${view.include('bootstrap-js')}
<tal:block content="structure view.render_includes()" />
- ${structure: request.render_jca_includes()}
</head>
<body>
<div class="navbar navbar-fixed-top">
@@ -49,5 +49,7 @@
${structure: wrapped_content}
</div>
</div>
+
+ ${structure: request.render_jca_includes('chat')}
</body>
</html>

0 comments on commit 8bf3009

Please sign in to comment.
Something went wrong with that request. Please try again.