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.4 -r1.5 --- openacs-4/packages/xowiki/tcl/chat-procs.tcl 31 Mar 2006 19:36:45 -0000 1.4 +++ openacs-4/packages/xowiki/tcl/chat-procs.tcl 8 Apr 2006 00:11:53 -0000 1.5 @@ -33,45 +33,75 @@ Chat proc initialize_nsvs {} {;} ;# noop - Chat proc login {-chat_id -package_id} { + Chat proc login {-chat_id -package_id -mode} { + my log "--" auth::require_login - ::xowiki::Page requireJS "/resources/xowiki/get-http-object.js" 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 path [site_node::get_url_from_object_id -object_id $package_id] + + if {![info exists mode]} { + set mode polling + set user_agent [string tolower [ns_set get [ns_conn headers] User-Agent]] + if {[regexp (firefox|msie) $user_agent] && [info command ::thread::mutex] ne ""} { + set mode streaming + } + my log "--mode $mode, user_agent=$user_agent" } - 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 "\ + + if {$mode eq "polling"} { + ::xowiki::Page requireJS "/resources/xowiki/get-http-object.js" + set jspath packages/xowiki/www/ajax/chat.js + set login_url ${path}ajax/chat?m=login&$context + set get_update "chatSendCmd(\"$path/ajax/chat?m=get_new&$context\",chatReceiver)" + set get_all "chatSendCmd(\"$path/ajax/chat?m=get_all&$context\",chatReceiver)" + } else { + set jspath packages/xowiki/www/ajax/streaming-chat.js + set subscribe_url ${path}ajax/chat?m=subscribe&$context + } + set send_url ${path}ajax/chat?m=add_msg&$context&msg= + + if { ![file exists [acs_root_dir]/$jspath] } { + return -code error "File [acs_root_dir]/$jspath does not exist" + } + set file [open [acs_root_dir]/$jspath]; set js [read $file]; close $file + + if {$mode eq "polling"} { + return "\ -
+
" + } else { + return "\ + +
+
+ +" + } } - - if {0} { - Chat c1 -chat_id 222 -session_id 123 -user_id 456 - set _ "" - c1 add_msg "Hello World now" - append _ [c1 get_new] - - ns_return 200 text/html $_ - } } + 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.3 -r1.4 --- openacs-4/packages/xowiki/www/ajax/chat.js 3 Mar 2006 10:17:25 -0000 1.3 +++ openacs-4/packages/xowiki/www/ajax/chat.js 8 Apr 2006 00:11:55 -0000 1.4 @@ -1,3 +1,7 @@ +// simple javascript support for polling ajax based chat interface +// $Id$ +// -gustaf neumann April 2006 + function receiver1() { if (http.readyState == 4) { // alert('status code =' + http.status); @@ -23,7 +27,6 @@ var xmlobject = (new DOMParser()).parseFromString(content, 'application/xhtml+xml'); 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 = 'items = ' + items.length + ' l=' + content.length + ' ' + escape(content); 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.2 -r1.3 --- openacs-4/packages/xowiki/www/ajax/chat.tcl 6 Feb 2006 11:45:10 -0000 1.2 +++ openacs-4/packages/xowiki/www/ajax/chat.tcl 8 Apr 2006 00:11:55 -0000 1.3 @@ -15,10 +15,12 @@ ::xowiki::Chat c1 -volatile -chat_id $id -session_id $s switch -- $m { add_msg { + #ns_log notice "--c call c1 $m '$msg'" set _ [c1 $m $msg] - ns_log notice "--c add_msg returns '$_'" + #ns_log notice "--c add_msg returns '$_'" } login - + subscribe - get_new - get_all {set _ [c1 $m]} default {ns_log error "--c unknown method $m called."} Index: openacs-4/packages/xowiki/www/ajax/streaming-chat.js =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/xowiki/www/ajax/Attic/streaming-chat.js,v diff -u --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ openacs-4/packages/xowiki/www/ajax/streaming-chat.js 8 Apr 2006 00:11:55 -0000 1.1 @@ -0,0 +1,98 @@ +// simple javascript support for streaming ajax based chat interface +// $Id: streaming-chat.js,v 1.1 2006/04/08 00:11:55 gustafn Exp $ +// -gustaf neumann April 2006 + +function getHttpObject() { + var http_request = false; + if (window.XMLHttpRequest) { // Mozilla, Safari,... + http_request = new XMLHttpRequest(); + } else if (window.ActiveXObject) { // IE + try { + http_request = new ActiveXObject('Msxml2.XMLHTTP'); + } catch (e) { + try { + http_request = new ActiveXObject('Microsoft.XMLHTTP'); + } catch (e) {} + } + } + + if (!http_request) { + alert('Cannot create and instance of XMLHTTP'); + } + return http_request; +} + +function getData() { + var response = http.responseText.substring(http.last); + // we recognize a complete message by a trailing }\n + if (response.match(/\}[\n ]+$/)) { + var messages = document.getElementById('messages'); + var data = eval('(' + response + ')'); + for (var i=0;i