FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index mod_harbour Presentando Harbour websocket server
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Presentando Harbour websocket server
Posted: Sat Apr 18, 2020 09:49 AM

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

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

  2. Escribe exit para indicarle al servidor que termine una sesi贸n.

Disfr煤talo!

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 297
Joined: Fri Apr 14, 2006 05:52 PM
Re: Presentando Harbour websocket server
Posted: Sun Jun 05, 2022 08:01 AM

Buenos d铆as Antonio:

Podr铆as indicarme los pasos exactos para construirlo.

Muchas gracias y un saludo,

Posts: 297
Joined: Fri Apr 14, 2006 05:52 PM
Re: Presentando Harbour websocket server
Posted: Sun Jun 05, 2022 09:45 AM

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,

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Presentando Harbour websocket server
Posted: Mon Jun 06, 2022 06:18 AM

Muy bien Mike!

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Presentando Harbour websocket server
Posted: Fri Jun 10, 2022 07:14 AM

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
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Presentando Harbour websocket server
Posted: Sat Aug 10, 2024 09:41 AM
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 (fw): Select all Collapse
聽 聽For Each hClient in aClients
聽 聽 聽 聽 聽 聽 聽 聽 聽 if hb_socketIsOpen( hClient )
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽hb_socketSend( hClient, Mask( cRequest ) )
聽 聽 聽 聽 聽 聽 聽 聽 聽 endif
聽 聽 聽 聽 聽 聽 聽 聽Next
Best regards,
Otto
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Presentando Harbour websocket server
Posted: Sat Aug 10, 2024 05:11 PM

Dear Otto,

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

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Presentando Harbour websocket server
Posted: Sat Aug 10, 2024 06:56 PM

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

Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Presentando Harbour websocket server
Posted: Sun Aug 11, 2024 07:45 AM
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


Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Presentando Harbour websocket server
Posted: Sun Aug 11, 2024 04:35 PM
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 (fw): Select all Collapse
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
Posts: 6983
Joined: Fri Oct 07, 2005 07:07 PM
Re: Presentando Harbour websocket server
Posted: Sun Aug 11, 2024 07:28 PM
Dear Antonio,
thank you.

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

Code (fw): Select all Collapse
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

Continue the discussion