Class ::xo::MessageRelay (public)
::nx::Class ::xo::MessageRelay
Defined in packages/xotcl-core/tcl/message-relay-procs.tcl
Generic Message Relay class
- Testcases:
- No testcase defined.
Source code: :public method subscribe {key {-initmsg ""} {-mode default}} { # # Subscribe to a service identified by a key. # # @param key unique id for a (potentially new communication hub) # @param initmsg optional message to be sent, when subscription happens # @param mode optional mode # } :public method send_to_subscriber {key msg} { # # Send a message to a service identified by the key # (communication hub). # # @param key id for an existing communication hub # @param msg message to be sent # } :public method sweep {key} { # # Check existing subscriptions and clean stale ones. # # @param key key of the communication hub # } :public method can_be_used {} { # # Check, if a message relay can be used in the current # configuration. # return 1 } :method start_of_page {mode} { # # Compose reply header. # if {$mode eq "scripted"} { # # Scripted streaming relies on an infinitely long request, # providing chunks of javascript code conveying the # message. Widely supported e.g. also by Internet Explorer, # but not recommended on modern browsers. # set content_type "text/html;charset=utf-8" set encoding "Cache-Control: no-cache\r\nTransfer-Encoding: chunked\r\n" set body "<html><body>[string repeat { } 1024]\r\n" set body [format %x [string length $body]]\r\n$body\r\n return "HTTP/1.1 200 OK\r\nContent-type: $content_type\r\n$encoding\r\n$body" } else { # # Other modes will use Server Sent Events. # return [append _ "HTTP/1.1 200 OK\r\n" "Cache-Control: no-cache\r\n" "X-Accel-Buffering': no\r\n" "Content-type: text/event-stream\r\n" "\r\n"] } } :public method encode_message {mode msg} { # # Provide different "encoding" depending on the mode. Notice # that for one chat, multiple clients might have difference # modes, since the modes are determined at also by the # capabilities of the client browser. So the incoming message # has to be recoded multiple times. # set msg [encoding convertto utf-8 $msg] if {$mode eq "scripted"} { #::sec_handler_reset #ns_log notice "SEND data <$msg> encoded <$emsg>" set jsMsg [subst { <script nonce='[::security::csp::nonce]'> window.parent.postMessage($msg); </script> }] set msg [format %x [string length $jsMsg]]\r\n$jsMsg\r\n } else { set msg [string cat {data: } $msg \n\n] } #ns_log notice "#### [self] encode_message <$mode> returns <$msg>" return $msg }XQL Not present: Generic, PostgreSQL, Oracle