Index: openacs-4/packages/xowiki/tcl/chat-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/tcl/chat-procs.tcl,v diff -u -r1.1 -r1.2 --- openacs-4/packages/xowiki/tcl/chat-procs.tcl 3 Feb 2006 22:17:41 -0000 1.1 +++ openacs-4/packages/xowiki/tcl/chat-procs.tcl 6 Feb 2006 11:45:09 -0000 1.2 @@ -8,27 +8,23 @@ namespace eval ::xowiki { Class Message -parameter {time user_id msg} Class Chat -superclass ::xo::OrderedComposite \ - -parameter {chat_id keep_nr_messages {encoder urlencode} {timewindow 600}} - Chat instproc context {user_id} { - if {![my exists now]} {my set now [clock clicks -milliseconds]} - if {$user_id == -1} {set user_id [ad_conn user_id]} - my set uid $user_id - my set array [self class]-[my set chat_id] - } + -parameter {chat_id user_id session_id {encoder urlencode} {timewindow 600}} Chat instproc init {} { - my instvar array uid now - my context 0 - if {![nsv_exists $array-seen newest]} { - nsv_set $array-seen newest $now - } + my instvar array + my set now [clock clicks -milliseconds] + if {![my exists user_id]} {my set user_id [ad_conn user_id]} + if {![my exists session_id]} {my set session_id [ad_conn session_id]} + set array [self class]-[my set chat_id] + if {![nsv_exists $array-seen newest]} {nsv_set $array-seen newest 0} } - Chat instproc add_msg {{-user_id -1} msg} { - my instvar array uid now - my context $user_id - set msg_id $now.$uid - nsv_set $array $msg_id [list $now [clock seconds] $uid $msg] + Chat instproc add_msg {{-get_new:boolean true} -uid msg} { + my instvar array now + set user_id [expr {[info exists uid] ? $uid : [my set user_id]}] + set msg_id $now.$user_id + nsv_set $array $msg_id [list $now [clock seconds] $user_id $msg] nsv_set $array-seen newest $now - my get_new -user_id $uid + nsv_set $array-last-activity $user_id $now + if {$get_new} {my get_new} } Chat instproc check_age {key ago} { my instvar array timewindow @@ -39,12 +35,11 @@ } return 1 } - Chat instproc get_new {{-user_id -1}} { - my instvar array uid now - my context $user_id - set last [expr {[nsv_exists $array-seen $uid] ? [nsv_get $array-seen $uid] : 0}] + Chat instproc get_new {} { + my instvar array now session_id + set last [expr {[nsv_exists $array-seen $session_id] ? [nsv_get $array-seen $session_id] : 0}] if {[nsv_get $array-seen newest]>$last} { - my log "--s must check $uid: [nsv_get $array-seen newest] > $last" + #my log "--c must check $session_id: [nsv_get $array-seen newest] > $last" foreach {key value} [nsv_array get $array] { foreach {timestamp secs user msg} $value break if {$timestamp > $last} { @@ -53,31 +48,41 @@ my check_age $key [expr {($now - $timestamp) / 1000}] } } - nsv_set $array-seen $uid $now + nsv_set $array-seen $session_id $now + #my log "--c setting session_id $session_id: $now" + } else { + #my log "--c nothing new for $session_id" } my render } - Chat instproc get_all {{-user_id -1}} { - my instvar array uid now - my context $user_id + Chat instproc get_all {} { + my instvar array now session_id foreach {key value} [nsv_array get $array] { foreach {timestamp secs user msg} $value break if {[my check_age $key [expr {($now - $timestamp) / 1000}]]} { my add [Message new -time $secs -user_id $user -msg $msg] } } - nsv_set $array-seen $uid $now + #my log "--c setting session_id $session_id: $now" + nsv_set $array-seen $session_id $now my render } - Chat instproc login {{-user_id -1}} { - my instvar array uid now - my context $user_id + Chat instproc login {} { + my instvar array user_id now # was the user already active? - if {![nsv_exists $array-seen $uid]} { - my add_msg -user_id $uid login + if {![nsv_exists $array-last-activity $user_id]} { + my add_msg -get_new false login } + foreach {user timestamp} [nsv_array get $array-last-activity] {;# sweeper + set ago [expr {($now - $timestamp) / 1000}] + if {$ago > 1200} { + my add_msg -get_new false -uid $user "auto logout" + nsv_unset $array-last-activity $user + } + } my encoder noencode - my get_all -user_id $uid + #my log "--c setting session_id [my set session_id]: $now" + my get_all } Chat instproc urlencode {string} {ns_urlencode $string} Chat instproc noencode {string} {set string} @@ -104,9 +109,43 @@ } return $result } + Chat proc login {-chat_id -package_id} { + auth::require_login + if {![info exists package_id]} {set package_id [ad_conn package_id] } + if {![info exists chat_id]} {set chat_id $package_id } + set context id=$chat_id&s=[ad_conn session_id].[clock seconds] + set path packages/xowiki/www/ajax/chat.js + if { ![file exists [acs_root_dir]/$path] } { + return -code error "File [acs_root_dir]/$path does not exist" + } + set file [open [acs_root_dir]/$path]; set js [read $file]; close $file + set location [util_current_location] + set path [site_node::get_url_from_object_id -object_id $package_id] + set login_url $path/ajax/chat?m=login&$context + set send_url $path/ajax/chat?m=add_msg&$context&msg= + set get_update "chatSendCmd(\"$path/ajax/chat?m=get_new&$context\",chatReceiver)" + set get_all "chatSendCmd(\"$path/ajax/chat?m=get_all&$context\",chatReceiver)" + return "\ + +
+ + +
+ " + #0 + #  [ad_conn session_id] - [ns_conn url] ?? [util_current_location] + + } + if {0} { - Chat c1 -chat_id 222 + Chat c1 -chat_id 222 -session_id 123 -user_id 456 set _ "" c1 add_msg "Hello World now" append _ [c1 get_new] Index: openacs-4/packages/xowiki/www/view.adp =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/www/Attic/view.adp,v diff -u -r1.6 -r1.7 --- openacs-4/packages/xowiki/www/view.adp 3 Feb 2006 22:17:41 -0000 1.6 +++ openacs-4/packages/xowiki/www/view.adp 6 Feb 2006 11:45:10 -0000 1.7 @@ -15,7 +15,7 @@ if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { - //http_request.overrideMimeType('text/xml'); + http_request.overrideMimeType('text/xml'); } } else if (window.ActiveXObject) { // IE try { @@ -33,6 +33,18 @@ return http_request; } +if (typeof DOMParser == "undefined") { + DOMParser = function () {} + + DOMParser.prototype.parseFromString = function (str, contentType) { + if (typeof ActiveXObject != "undefined") { + var d = new ActiveXObject("MSXML.DomDocument"); + d.loadXML(str); + return d; + } + } +} + var http = getHttpObject(); Index: openacs-4/packages/xowiki/www/admin/samples/ajax-chat.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/www/admin/samples/ajax-chat.tcl,v diff -u -r1.1 -r1.2 --- openacs-4/packages/xowiki/www/admin/samples/ajax-chat.tcl 3 Feb 2006 22:17:41 -0000 1.1 +++ openacs-4/packages/xowiki/www/admin/samples/ajax-chat.tcl 6 Feb 2006 11:45:10 -0000 1.2 @@ -4,40 +4,14 @@ -set lang en \ -set description {} \ -set text { -proc content {} { - auth::require_login - set chat_id 22 - set path packages/xowiki/www/ajax/chat.js - if { ![file exists [acs_root_dir]/$path] } { - return -code error "File [acs_root_dir]/$path does not exist" - } - set file [open [acs_root_dir]/$path]; set js [read $file]; close $file - set login_url /xowiki/ajax/chat?m=login&id=$chat_id - set send_url /xowiki/ajax/chat?m=add_msg&id=$chat_id&msg= - set get_update "chatSendCmd(\"/xowiki/ajax/chat?m=get_new&id=$chat_id\",chatReceiver)" - set get_all "chatSendCmd(\"/xowiki/ajax/chat?m=get_all&id=$chat_id\",chatReceiver)" - return " - -
- - - - 0 -   -" + proc content {} { + ::xowiki::Chat login -chat_id 22 + } + } \ + -set nls_language en_US \ + -set mime_type {text/html} \ + -set title en:ajax-chat } -} \ - -set nls_language en_US \ - -set mime_type {text/html} \ - -set title en:ajax-chat -} set page_title "Import XoWiki Pages" set context {} Index: openacs-4/packages/xowiki/www/ajax/chat.js =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/www/ajax/Attic/chat.js,v diff -u -r1.1 -r1.2 --- openacs-4/packages/xowiki/www/ajax/chat.js 3 Feb 2006 22:17:41 -0000 1.1 +++ openacs-4/packages/xowiki/www/ajax/chat.js 6 Feb 2006 11:45:10 -0000 1.2 @@ -13,62 +13,63 @@ if (http.status == 200) { appendToMessages(http.responseText); } else { + clearInterval(); alert('Something wrong in HTTP request, status code = ' + http.status); } } } function appendToMessages(content) { var xmlobject = (new DOMParser()).parseFromString(content, 'application/xhtml+xml'); - //var xmlobject = (new DOMParser()).parseFromString(content, 'text/html'); var items = xmlobject.getElementsByTagName('TR'); //alert('found ' + items.length + ' items'); - var counter = document.getElementById('chatCounter'); - counter.innerHTML = parseInt(counter.innerHTML) + 1; - //document.getElementById('chatResponse').innerHTML = content; + //var counter = document.getElementById('chatCounter'); + //counter.innerHTML = parseInt(counter.innerHTML) + 1; + //document.getElementById('chatResponse').innerHTML = 'items = ' + items.length + ' l=' + content.length + ' ' + escape(content); + //if (items.length > 0) {alert('appending ' + content);} + var doc = frames['ichat'].document; var tbody = frames['ichat'].document.getElementById('messages').tBodies[0]; + //var tbody = tbodies[tbodies.length -1]; //for (var i = 0 ; i < items.length ; i++) { // tbody.appendChild(frames['ichat'].document.importNode(items[i],true)); //} var tr, td, e, s; for (var i = 0 ; i < items.length ; i++) { - tr = document.createElement('tr'); + tr = doc.createElement('tr'); e = items[i].getElementsByTagName('TD'); - td = document.createElement('td'); + td = doc.createElement('td'); td.innerHTML = unescape(e[0].firstChild.nodeValue); td.className = 'timestamp'; tr.appendChild(td); - td = document.createElement('td'); + td = doc.createElement('td'); s = e[1].firstChild.nodeValue; td.innerHTML = unescape(e[1].firstChild.nodeValue.replace(/\+/g,' ')); - //td.appendChild(document.createTextNode(e[1].firstChild.nodeValue)); td.className = 'user'; tr.appendChild(td); - td = document.createElement('td'); + td = doc.createElement('td'); td.innerHTML = unescape(e[2].firstChild.nodeValue.replace(/\+/g,' ')); - //td.appendChild(document.createTextNode(e[2].firstChild.nodeValue)); td.className = 'message'; tr.appendChild(td); - //tbody.appendChild(items[i]); - //tbody.appendChild(items[i].cloneNode(true)); tbody.appendChild(tr); - } frames['ichat'].window.scrollTo(0,tbody.offsetHeight); } + function chatSendMsg(send_url,handler) { var msgField = document.getElementById('chatMsg'); chatSendCmd(send_url + escape(msgField.value),handler); msgField.value = ''; } + +var msgcount = 0; // hack to overcome IE function chatSendCmd(url,handler) { - http.open('GET', url, true); + http.open('GET', url + '&mc=' + msgcount++, true); http.onreadystatechange = handler; http.send(null); } Index: openacs-4/packages/xowiki/www/ajax/chat.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/www/ajax/Attic/chat.tcl,v diff -u -r1.1 -r1.2 --- openacs-4/packages/xowiki/www/ajax/chat.tcl 3 Feb 2006 22:17:41 -0000 1.1 +++ openacs-4/packages/xowiki/www/ajax/chat.tcl 6 Feb 2006 11:45:10 -0000 1.2 @@ -7,10 +7,12 @@ } -query { m id + s msg:optional } -::xowiki::Chat c1 -volatile -chat_id $id +#ns_log notice "--c m=$m session_id = $s [clock format [lindex [split $s .] 1] -format %H:%M:%S]" +::xowiki::Chat c1 -volatile -chat_id $id -session_id $s switch -- $m { add_msg { set _ [c1 $m $msg]