Presentando Harbour websocket server

mod_harbour es un módulo para Apache que permite correr tus PRGs directamente en la web!!!
Post Reply
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Presentando Harbour websocket server

Post by Antonio Linares »

Aqui teneis el primer prototipo del Harbour websocket server:

https://github.com/FiveTechSoft/wsserver

Basicamente este server permite comunicarte entre un navegador web y una aplicación Harbour (sin importar donde esté!) usando websockets.

Actualmente funciona bien para mensajes <= 125 chars. Esperamos completarlo para todas las longitudes de mensajes muy pronto. Se agradece la ayuda :-)

En esta versión el websocket server implementa un servicio de echo, para probar que funciona bien. Te envía de vuelta lo que le envies. Puedes facilmente cambiar el código fuente para implementar cualquier otra conversación que se te ocurra. Usa el puerto 9000 pero podeis usar cualquier otro, tan solo recordad cambiarlo tanto en wsserver.prg como en client.html

Cómo usarlo:

1. Construye wsserver.exe usando hbmk2 wsserver.prg -mt. Usa el flag -mt de hbmk2 para construirlo con multihilo!

2. Ejecuta wsserver.exe. Mostrará todos los mensajes que reciba. "loop" aparece en la pantalla. Pulsa esc en cualquier momento para finalizarlo.

3. Abre esta página HTML desde tu navegador:

https://fivetechsoft.github.io//wsserver/client.html

4. Lo que envies desde la página web te llegará de vuelta (un recordatorio del karma :-)

5. Escribe exit para indicarle al servidor que termine una sesión.

Disfrútalo!
regards, saludos

Antonio Linares
www.fivetechsoft.com
Mike Serra
Posts: 297
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Re: Presentando Harbour websocket server

Post by Mike Serra »

Buenos días Antonio:

Podrías indicarme los pasos exactos para construirlo.

Muchas gracias y un saludo,
Mike Serra
Posts: 297
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Re: Presentando Harbour websocket server

Post by Mike Serra »

Conseguido!!

He usado la utilidad vmh.exe de los samples y al fichero go.bat que genera, le he modificado unas rutas que estaban "a pelo" y además le he añadido al final de la llamada a hbmk2 -mt para el soporte multihilo.

Voy a realizar pruebas para enviarle desde el cliente un fichero json y procesarlo y devolver respuesta.

Un SAludo,
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: Presentando Harbour websocket server

Post by Antonio Linares »

Muy bien Mike!

Si necesitas cualquier cosa, aqui estamos. Hay que sacarles el jugo a los websockets :-)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: Presentando Harbour websocket server

Post by Antonio Linares »

Mike,

Tenemos preparado un ejemplo de uso de websockets desde FWH para FWH 22.06

Hemos implementado una nueva clase TWebSocketServer que simplifica muchisimo el uso de los websockets :-)

Te lo digo por simplificar lo que necesites hacer
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 6396
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 8 times
Been thanked: 1 time
Contact:

Re: Presentando Harbour websocket server

Post 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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: Presentando Harbour websocket server

Post by Antonio Linares »

Dear Otto,

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

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 6396
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 8 times
Been thanked: 1 time
Contact:

Re: Presentando Harbour websocket server

Post 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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
Posts: 6396
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 8 times
Been thanked: 1 time
Contact:

Re: Presentando Harbour websocket server

Post 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
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Antonio Linares
Site Admin
Posts: 42393
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Has thanked: 9 times
Been thanked: 41 times
Contact:

Re: Presentando Harbour websocket server

Post 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
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Otto
Posts: 6396
Joined: Fri Oct 07, 2005 7:07 pm
Has thanked: 8 times
Been thanked: 1 time
Contact:

Re: Presentando Harbour websocket server

Post 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
 
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
Post Reply