Recent

Author Topic: How to do Warfley simple websocket server sample on a form not in console  (Read 1972 times)

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Anyone know how to do this? as i know got sample only on console mode.
1. got freezing on server start.
2. how to do registering handler on visual form

Thank you
« Last Edit: February 14, 2023, 01:47:00 am by Mongkey »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2088
  • Fifty shades of code.
    • Delphi & FreePascal
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #1 on: February 14, 2023, 01:54:47 am »
What is "Warfley simple websocket server sample" ?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

PierceNg

  • Sr. Member
  • ****
  • Posts: 374
    • SamadhiWeb
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #2 on: February 14, 2023, 04:11:58 am »
1. got freezing on server start.

Start the server in its own thread, not in the main GUI thread.

2. how to do registering handler on visual form

Don't understand the question. You mean like having a text label on a form and updating the label with data that the socket server receives? For that PostMessage / SendMessage could do the job, or you may need to implement producer/consumer queues with synchronized multithreaded access by GUI and socket server.

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #3 on: February 14, 2023, 04:48:13 am »
What is "Warfley simple websocket server sample" ?

Simple hello world on a controlled form ws server. Thank you.


Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #4 on: February 14, 2023, 04:49:47 am »
1. got freezing on server start.

Start the server in its own thread, not in the main GUI thread.

2. how to do registering handler on visual form

Don't understand the question. You mean like having a text label on a form and updating the label with data that the socket server receives? For that PostMessage / SendMessage could do the job, or you may need to implement producer/consumer queues with synchronized multithreaded access by GUI and socket server.

Yes, my friend , simple mean may only has 1-5 wsclients. Sendmessages Only to display counter. Thank you.
« Last Edit: February 14, 2023, 04:54:14 am by Mongkey »

TRon

  • Hero Member
  • *****
  • Posts: 2538
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #5 on: February 14, 2023, 06:45:13 am »
What is "Warfley simple websocket server sample" ?
Uhm... when I initially read the topic I got a very strange thought in my head that was about some form/appearance of user Warfley as a webserver socket  %)  :)

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #6 on: February 14, 2023, 10:48:03 am »
What is "Warfley simple websocket server sample" ?
Uhm... when I initially read the topic I got a very strange thought in my head that was about some form/appearance of user Warfley as a webserver socket  %)  :)

 :D :D

PierceNg

  • Sr. Member
  • ****
  • Posts: 374
    • SamadhiWeb
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #7 on: February 14, 2023, 01:05:45 pm »
Attached is a quick and dirty port of (half of) Warfley's console chat server example to LCL. Half because the console chat server does readln in a loop and sends what it reads from console to the connected client, but this LCL port only receives and never sends.

Built with Lazarus 2.2.0 with FPC 3.2.2 on Ubuntu 20.04. Tested with the console and web browser chat clients. It works on my computer.TM

Note: I changed the server port to 8080. Either change it back to 9080, or modify the clients' source to match.

I also made slight change to TWebSocketServer constructors to set socket to SO_REUSEADDR and SO_REUSEPORT. Let me know if you want the patch.

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #8 on: February 14, 2023, 03:00:47 pm »
Attached is a quick and dirty port of (half of) Warfley's console chat server example to LCL. Half because the console chat server does readln in a loop and sends what it reads from console to the connected client, but this LCL port only receives and never sends.

Built with Lazarus 2.2.0 with FPC 3.2.2 on Ubuntu 20.04. Tested with the console and web browser chat clients. It works on my computer.TM

Note: I changed the server port to 8080. Either change it back to 9080, or modify the clients' source to match.

I also made slight change to TWebSocketServer constructors to set socket to SO_REUSEADDR and SO_REUSEPORT. Let me know if you want the patch.

Thank you! pierceNg  8), this should be inside warfley websocket github sample.
« Last Edit: February 14, 2023, 03:03:58 pm by Mongkey »

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #9 on: February 14, 2023, 07:27:27 pm »
As my name is in the titel, I feel as if I should also answer to this thread.

The thing about the examples is, they are not production ready code, not in the slightest. They are as minimal as possible to just show the basic functionality, but for creating a real server there is much more to it.

Also, the library was made to be as simple as possible in terms of architecture and it's own code (I originally tried to keep it under 1k lines of code, now it's already twice that because I keep adding features, but still quite slim), on the other hand this means that you need to do a lot yourself.
One of those things you need to handle yourself is thread synchronization and LCL integration.
The threading implementation is extremely basic, meaning if you choose to use the threaded handler (which I originally did not even intend to include, as everyone should make their own architecture), it will be executed in it's own thread, but any synchronization, e.g. with the LCL through syncronize or TThread.Queue must be done by the programmer themselves.

I intend on building a non blocking single threaded variant one day (which could then be for example used with a TTimer in an LCL application), but it's just one thing on an ever growing lists of ideas for the future

PierceNg

  • Sr. Member
  • ****
  • Posts: 374
    • SamadhiWeb
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #10 on: February 15, 2023, 02:12:40 am »
As my name is in the titel, I feel as if I should also answer to this thread.

Hi Warfley, thank you for making your software available and chiming in.

A comment/question: Your console chat server example runs a "while comm.open do stuff" loop. To emulate that, for LCL, my code  does "while comm.open waitFor(INFINITE)". It feels unaesthetic to do a while loop (that waits indefinitely) in an event-driven GUI cum network server app. Is there a better way currently in the library to do the same thing?

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #11 on: February 15, 2023, 09:15:34 am »
When the client is connected, the handler is created and "HandleConnection" is called. The Threaded Handler then starts a thread which does the following:
Code: Pascal  [Select][+][-]
  1.   try
  2.     Recv := CreateReceiverThread(arg.Communicator, Arg.Pooling);
  3.     try
  4.       Arg.Communicator.SetCustomReceiveMessageThread(Recv);
  5.       Arg.Handler.PrepareCommunication(arg.Communicator);
  6.       Arg.Handler.DoHandleCommunication(arg.Communicator);
  7.     finally
  8.       Recv.Stop;
  9.     end;
  10.     Sleep(20);
  11.   finally
  12.     Arg.Handler.FinalizeCommunication(arg.Communicator);
  13.   end;
So what happens here is that a separate receiver thread is started, and then the handling loop is executed. Once the handling loop finished, the Receiver Thread is stopped, and afterwards the communicator is Finalized.

If you don't want to have that loop you need to create your own TWebsocketHandler, which creates it's own receiver thread start/stop mechanism.

I may later today create a simple example for an event driven handler

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #12 on: February 15, 2023, 02:51:59 pm »
Sorry guys, once more question:
How to terminate created wssthread?

1.wssthread.terminate;
   wssthread.free;
2. Killthread(threadId);
->threadid = wssthread.threadid. directly after created.

ws streaming does not stop.

Thank you.
« Last Edit: February 15, 2023, 03:12:51 pm by Mongkey »

Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #13 on: February 15, 2023, 07:10:35 pm »
As promised a new example: https://github.com/Warfley/LazWebsockets/tree/master/examples/lcl_example/server

Is a bit lazy on some points, but I think this shows clearly how to build such a thing event driven and how to handle the events that occured on different threads on the mainthread (through TThread.QueueAsyncCall).

But I think this could be done easier with some minor modifications to the library (basically the event calling functions must be overridable so one can simply inherit isntead of creating a whole new wrapper.
There are some other things I want to change in the future, so maybe I am going to update the library soon™
« Last Edit: February 15, 2023, 07:12:23 pm by Warfley »

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: How to do Warfley simple websocket server sample on a form not in console
« Reply #14 on: February 15, 2023, 10:10:03 pm »
As promised a new example: https://github.com/Warfley/LazWebsockets/tree/master/examples/lcl_example/server

Is a bit lazy on some points, but I think this shows clearly how to build such a thing event driven and how to handle the events that occured on different threads on the mainthread (through TThread.QueueAsyncCall).

But I think this could be done easier with some minor modifications to the library (basically the event calling functions must be overridable so one can simply inherit isntead of creating a whole new wrapper.
There are some other things I want to change in the future, so maybe I am going to update the library soon™

Thank you bro, your LCL sample is more than enough.  :)

 

TinyPortal © 2005-2018