Page 1 of 1

Re: Presentando Harbour websocket server

Posted: Sat Aug 10, 2024 9:41 am
by Otto
Dear Antonio,

It seems to me that the WSS is not forwarding the messages to the connected clients.
The server only responds to received messages and sends back to the original sender.

I wanted to make changes, but I'm missing files: hbmk2: Error: Referenced, missing, but unknown function(s): HB_SOCKETISOPEN(),
AP_FILENAME()

Code: Select all | Expand

   For Each hClient in aClients
                  if hb_socketIsOpen( hClient )
                     hb_socketSend( hClient, Mask( cRequest ) )
                  endif
               Next
Best regards,
Otto

Re: Presentando Harbour websocket server

Posted: Sat Aug 10, 2024 5:11 pm
by Antonio Linares
Dear Otto,

That functions belongs to Harbour, it may not be linked into mod_harbour

Re: Presentando Harbour websocket server

Posted: Sat Aug 10, 2024 6:56 pm
by Otto
Dear Antonio,

I think hb_socketSend() to all connected clients is missing in the websocketserver.
But I can't build myself a new exe.
Maybe you can provide a new exe.
Best regards,
Otto

Re: Presentando Harbour websocket server

Posted: Sun Aug 11, 2024 7:45 am
by Otto
Dear Antonio,

> That functions belongs to Harbour, it may not be linked into mod_harbour
It seems to me that this is missing in wsserver.prg.

Thank you in advance and best regards,
Otto


Image

Re: Presentando Harbour websocket server

Posted: Sun Aug 11, 2024 4:35 pm
by Antonio Linares
Dear Otto,

I have searched for hb_socketIsOpen() in all Harbour sources and it is not found. Maybe it was proposed by the AI ?

Harbour's hb_socketSend() is defined in src/rtl/hbsocket.c and hbsockhb.c ( high level HB_FUNC( HB_SOCKETSEND ) )

Code: Select all | Expand

long hb_socketSend( HB_SOCKET sd, const void * data, long len, int flags, HB_MAXINT timeout )
{
   long lSent = 0;

   hb_vmUnlock();

   if( timeout >= 0 )
   {
      lSent = hb_socketSelectWR( sd, timeout );
      if( lSent == 0 )
      {
         hb_socketSetError( HB_SOCKET_ERR_TIMEOUT );
         lSent = -1;
      }
   }
   if( lSent >= 0 )
   {
      int iError;

      flags = hb_socketTransFlags( flags );
      /* in POSIX systems writing data to broken connection stream causes
       * that system generates SIGPIPE which has to be caught by application
       * otherwise the default action for SIGPIPE is application termination.
       * we do not want to generate it so we are setting MSG_NOSIGNAL flag.
       */
#if defined( MSG_NOSIGNAL )
      flags |= MSG_NOSIGNAL;
#endif
      do
      {
         lSent = send( sd, ( const char * ) data, len, flags );
         iError = lSent > 0 ? 0 : HB_SOCK_GETERROR();
         hb_socketSetOsError( iError );
      }
      while( lSent == -1 && HB_SOCK_IS_EINTR( iError ) &&
             hb_vmRequestQuery() == 0 );
   }
   hb_vmLock();

   return lSent;
}
 
If you try to build wsserver.prg using hb_socketSend() it should be found, but hb_socketIsOpen() will be missing.

I am not sure if you meant that or I may have not properly understood you

Re: Presentando Harbour websocket server

Posted: Sun Aug 11, 2024 7:28 pm
by Otto
Dear Antonio,
thank you.

Now with this changes all the clients get the same info.
Best regards,
Otto

Code: Select all | Expand


function ServeClient( hSocket )

   local cRequest, cBuffer := Space( 4096 ), nLen, nOpcode
   local I := 0
   AAdd( aClients, hSocket )
   
   hb_socketRecv( hSocket, @cBuffer,,, 1024 )
  
   HandShaking( hSocket, RTrim( cBuffer ) )
 
   ? "new client connected"

   USE log SHARED

   while .T.
      cRequest = ""
      nLen = 1

      while nLen > 0
         cBuffer := Space( 4096 )
         if ( nLen := hb_socketRecv( hSocket, @cBuffer,,, TIMEOUT ) ) > 0  
            cRequest += Left( cBuffer, nLen )
         else
            if nLen == -1 .and. hb_socketGetError() == HB_SOCKET_ERR_TIMEOUT
               nLen = 0
            endif
         endif
      end
      
      if ! Empty( cRequest )
         cRequest:= UnMask( cRequest, @nOpcode )
         
         do case
            case cRequest == "exit"          // 1000 value in hex and bytes swapped 
               hb_socketSend( hSocket, Mask( I2Bin( 0xE803 ) + "exiting", OPC_CLOSE ) )   // close handShake
               
            case cRequest == I2Bin( 0xE803 ) + "exiting"                                  // client answered to close handShake
               exit
               
            otherwise
               ? " Antwort: "  +cRequest
         //      hb_socketSend( hSocket, Mask( "*" + cRequest ) )
 
          FOR I := 1 to len( aClients )
                   
                  hb_socketSend( aClients[I], Mask( cRequest ) )
              Next
         endcase
      endif
   end

   ? "close socket"

   hb_socketShutdown( hSocket )
   hb_socketClose( hSocket )

   USE

return nil