@@ -25,20 +25,17 @@ function curl_socket($socket, $url, $message='') {
2525 return $ reply ;
2626}
2727
28- // $endpoint: the name of the endpoint to publish to
29- // $message: the message to publish (if an array, it will be converted to a JSON string)
28+ // $endpoint: the name of the endpoint to publish to (string)
29+ // $message: the message to publish (string)
3030// $len: the length of the buffer (default 1)
3131// $abort: if true, the script will exit if the endpoint is without subscribers on the next publish attempt after $abortTime seconds (default true)
3232// $abortTime: the time in seconds to wait before exiting the script if the endpoint is without subscribers (default 30)
33- function publish ($ endpoint , $ message , $ len =1 , $ abort =true , $ abortTime =30 ) {
33+ function publish ($ endpoint , $ message , $ len =1 , $ abort =false , $ abortTime =30 ) {
3434 static $ abortStart = [], $ com = [], $ lens = [];
3535
3636 if ( is_file ("/tmp/publishPaused " ) )
3737 return false ;
3838
39- if ( is_array ($ message ) ) {
40- $ message = json_encode ($ message );
41- }
4239
4340 // Check for the unlikely case of a buffer length change
4441 if ( (($ lens [$ endpoint ] ?? 1 ) !== $ len ) && isset ($ com [$ endpoint ]) ) {
@@ -85,9 +82,8 @@ function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
8582 if ( ! ($ abortStart [$ endpoint ]??false ) )
8683 $ abortStart [$ endpoint ] = time ();
8784 if ( (time () - $ abortStart [$ endpoint ]) > $ abortTime ) {
88- my_logger ("$ endpoint timed out after $ abortTime seconds. Exiting. " , 'publish ' );
89-
90- removeNChanScript ();
85+ $ script = removeNChanScript ();
86+ my_logger ("$ script timed out after $ abortTime seconds. Exiting. " , 'publish ' );
9187 exit ();
9288 }
9389 $ reply = false ; // if no subscribers, force return value to false
@@ -100,7 +96,7 @@ function publish($endpoint, $message, $len=1, $abort=true, $abortTime=30) {
10096}
10197
10298// Function to not continually republish the same message if it hasn't changed since the last publish
103- function publish_noDupe ($ endpoint , $ message , $ len = 1 , $ abort = true , $ abortTime =30 ) {
99+ function publish_noDupe ($ endpoint , $ message , $ noListenerAbort = false , $ abortTime =30 ) {
104100 static $ msg_old = [];
105101 static $ msg_time = [];
106102 static $ listener = [];
@@ -109,12 +105,8 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30)
109105 $ listener [$ endpoint ] = false ;
110106 }
111107
112- if ( is_array ($ message ) ) {
113- $ message = json_encode ($ message );
114- }
115-
116108 // if abort is set, republish the message even if it hasn't changed after $timeout seconds to check for subscribers and exit accordingly
117- if ( $ abort ) {
109+ if ( $ noListenerAbort ) {
118110 if ( (time () - ($ msg_time [$ endpoint ]??0 )) > $ abortTime ) {
119111 $ msg_old [$ endpoint ] = null ;
120112 }
@@ -124,7 +116,7 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30)
124116 $ msg_old [$ endpoint ] = $ message ;
125117 $ msg_time [$ endpoint ] = time ();
126118
127- $ return = publish ($ endpoint , $ message , $ len , $ abort , $ abortTime );
119+ $ return = publish ($ endpoint , $ message ,1 , $ noListenerAbort );
128120
129121 // if no listener, keep publishing whether or not its the same message.
130122 $ listener [$ endpoint ] = $ return ? true : false ;
@@ -133,6 +125,10 @@ function publish_noDupe($endpoint, $message, $len=1, $abort=true, $abortTime=30)
133125 }
134126}
135127
128+ // Wrapper to publish a ping message to the endpoint with occasional republishing and checking if anyone is listening
129+ function ping ($ endpoint ) {
130+ publish_noDupe ($ endpoint ,"ping " ,true );
131+ }
136132
137133// Removes the script calling this function from nchan.pid
138134function removeNChanScript () {
@@ -148,5 +144,6 @@ function removeNChanScript() {
148144 } else {
149145 @unlink ("/var/run/nchan.pid " );
150146 }
147+ return $ script ;
151148}
152149?>
0 commit comments