The Operator Page
The operator, unlike the client, can carry on multiple conversations. So they can subscribe to many chat topics, all called /chat/demo/username for uniqueness. Each conversation will have a different Room widget in a different ContentPane
When a message arrives, it may be a new chat request or an existing conversation. We keep track of this in objects that correspond to conversations. First, the addOnLoad starts subscriptions running:
dojo.addOnLoad(function(){ dojo.parser.parse(dojo.body()); dojox.cometd.init("http://comet.sitepen.com:9000/cometd"); dojox.cometd.subscribe("/chat/demo",control,"_getAlert"); });
The control object is an overall controller for operator conversations. Here's a skeleton:
var control = { _chats: {}, _getAlert: function(e){ }, _privateChat: function(e){ } };
Coversation records are kept in the _chats object, which will be a hash of username keys with current conversations. Through our subscribe() above, _getAlert will get any messages over the public topic /chat/demo. It will then either create a new conversation and tabbed pane or simply return:
_getAlert: function(e){ // Ignore all control messages where we already have a conversation registered, // or messages bound for someone else. if (!this._chats[(e.data.user)] && (operator != e.data.user)){ dojox.cometd.subscribe("/chat/demo/"+e.data.joined,this,"_privateChat"); // Create a tab called chatWithUsername and insert it into the tabbed container var tabNode = document.createElement('div'); tabNode.id = "chatWith" + e.data.user; var chatNode = document.createElement('div'); chatNode.id = e.data.user + "Widget"; tabNode.appendChild(chatNode); var newTab = new dijit.layout.ContentPane({ title: e.data.user, closable: true },tabNode); dijit.byId('tabView').addChild(newTab); // Create an associated Room object var chat = new dijit.demos.chat.Room({ roomId: e.data.joined, registeredAs: operator },chatNode); chat.startup(); // And record this conversation this._chats[(e.data.user)]=true; } },
The subscription to /chat/demo/username passes received messages to the proper ContentPane:
_privateChat: function(e){ var thisChat = dijit.byId(e.data.user+"Widget") || false; if (thisChat) { thisChat._chat(e); } }
Laura calls over one of her team members and shows him the chat function. On separate PC's they play with it for about 15 minutes or so, trading war stories and jokes. It feels pretty nice to have a working system built this quickly and with so little code. Laura no longer misses C.
- Printer-friendly version
- Login or register to post comments
- Subscribe post