LazWebsocket How to identify IP adress during connect lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- hgbk · 2021-04-20 13:47 I have implemented a game as client server solution using LazWebsockets. It may happen that during the game the connection from a client breaks. In order to implement the possibility for this specific client to reconnect I need to identify the IP address during accept and compare it with ACommunication.SocketStream.RemoteAddress.Address of this client  which I have stored before failure. In accept I have TRequestData as parameter which delivers Host (Domain but not IP address). Any idea how to solve this? ---------------------------------------------------------------------- Warfley · 2021-04-21 01:02 There is currently no way implemented to identify the client during the handshake. This is defenetly something I will consider adding to the library. But, I should point out that identifying clients based on their IPs is bad style, as due to the use of NAT networks, multiple devices usually share an external IP. While in Europe and the US it is usually something like one per household, for some (cheaper) internet providers or providers in other countries that haven't been so lucky with getting large shares of the IP address space, multiple homes or even larger parts like city blocks can share the same IP address. So there could be potentially hundreds of people with the same IP. Therefore you should give the client a unique ID to reconnect. Like when a game starts, you could generate a game ID, which every client gets. If a client looses it's connection it can reconnect by sending this game ID to the server and the server can recognize this and put it back into the right game ---------------------------------------------------------------------- hgbk · 2021-04-21 11:17 Understand - I will implement the "reconnect" according to your proposal. Thanks a lot for the quick and helpful answer! ---------------------------------------------------------------------- r.lukasiak · 2021-09-28 18:38 Quote from: Warfley on April 21, 2021, 03:02:55 amThere is currently no way implemented to identify the client during the handshake. This is defenetly something I will consider adding to the library. But, I should point out that identifying clients based on their IPs is bad style, as due to the use of NAT networks, multiple devices usually share an external IP. While in Europe and the US it is usually something like one per household, for some (cheaper) internet providers or providers in other countries that haven't been so lucky with getting large shares of the IP address space, multiple homes or even larger parts like city blocks can share the same IP address. So there could be potentially hundreds of people with the same IP. Therefore you should give the client a unique ID to reconnect. Like when a game starts, you could generate a game ID, which every client gets. If a client looses it's connection it can reconnect by sending this game ID to the server and the server can recognize this and put it back into the right game Hi everyone! I've been trying to figure out how to use LazWebSocket for some 3 days. I'm analyzing the example  server and client files. I managed to figure out how the client works (at least some basic functions), I created some basic GUI and applied the "client" code to it, I was able to run it iin StartReceiveMessageThread mode. My app is able to sent and receive the message (OnReceiveMessage). But when I run more than 1 instance, messages from the server are sent randomly. I even tried to start my client app from different IPs, local 192.168.x.x and through HamachiLogMeIn 25.x.x.x. Both instances can send messages to the server but the server answers randomly. I understand that it may happen when there are more than 1 client with the same IP but if 2 clients have a different IP address? How can I make the server answer to a specific client? Quoteyou could generate a game ID, which every client gets how could I use a client ID to make the server contact the specific client by his ID, not IP? thanks for any hint. ---------------------------------------------------------------------- Warfley · 2021-09-29 12:27 In LazWebsockets the connection is handled by the TWebsocketCommunicator instance given as Parameter to the handler. If you want to handle multiple connections, you need to identify these. The (threaded) base handler already provides a list of all connections, you could use the index to identify these: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   procedure TSocketHandler.IdentifyClients;  var    ConnectionList: TConnectionList;    i: Integer;  begin    ConnectionList := Connections.Lock;    try      for i := 0 to ConnectionList.Count - 1 do        ConnectionList[i].WriteStringMessage('You are client number ' + i.ToString);    finally      Connections.Unlock;    end;  end; The problem here is, when a client disconnects, it changes the indices of all clients connected after that one (as all move by one). So you could rather than that generate an ID (e.g. by simply having a counter counting up) and use a TDictionary or TFPGMap or similar to manage the connections. You might also need a reverse lookup to map the communicator to it's ID (some extendibility option for the communicator might be also useful here, to store such data right in the communicator). Another solution is, each handler call gets executed in their own Thread (assuming you use a threaded handler). The best way is to have all the communication happen within that handler and you interact though messages in a message queue. When you receive something you can send a message to the GUI to handle the data. When the GUI wants to send something it sends a message to the handler. But: no matter what approach you choose, you must always make sure to use sufficient thread locks. All the default  datastructures (arrays, Lists, Maps, Dictionaries, etc.) are not thread safe, so when dealing with the threaded websockets, you should always use locks ---------------------------------------------------------------------- gucao · 2021-10-06 10:46 The accepted socket is the SocketStream.handle? can get the ip and port from the accepted  socket The code need test Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses sockets var  VRtInt,VL:Integer;  VAddr:TSockAddr;  VSocketErr: cint; VIP:string;  VPort:integer; begin  VL:=SizeOf(VAddr);    VRtInt:=fpgetpeername(ASocket,@VAddr,@VL);  if VRtInt=0 then begin     VIp:=NetAddrToStr(VAddr.sin_addr);     VPort:=NToHs(VAddr.sin_port);  end else begin     Result:='faild`````';     Exit;  end;  end; ---------------------------------------------------------------------- r.lukasiak · 2021-10-14 08:35 Hi there! I'm sorry for the delay in answering. Warfley, thanks for the explanation, it seems pretty clear, however I'd have several questions: 1. Quote"The (threaded) base handler" socket.AcceptingMethod:=samThreaded? 2. Does a new client always get the highest number in ConnectionList? If there are 5 connected clients, I suppose they are numbered 0..4 so the new one will get 5, right? Clients have their user names so basing on ConnectionList, I could have some list linking ConnectionList's client number to the user name but... 3. Quote"when a client disconnects, it changes the indices of all clients connected" Is there any event going on in this situation? how can I know that someone disconnected to update my list? gucao, I haven't tested it it but quickly analyzing the code, I understand that it's extracting IP and port from the handler? ---------------------------------------------------------------------- Warfley · 2021-10-14 12:47 Quote from: r.lukasiak on October 14, 2021, 10:35:19 amWarfley, thanks for the explanation, it seems pretty clear, however I'd have several questions: 1. Quote"The (threaded) base handler" socket.AcceptingMethod:=samThreaded? Nope, that makes the accepting handshake threaded (basically before establishing a connection a HTTP request is made, with samThreaded or samThreadPool this will be threaded). What I meant is when implementing the handler you create a class inheriting from TWebsocketHandler. If you inherit from  TThreadedWebsocketHandler you make use of the threaded handler. In the ChatServer example this would be this line: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   TSocketHandler = class(TThreadedWebsocketHandler) // inherit from TThreadedWebsocketHandler Quote from: r.lukasiak on October 14, 2021, 10:35:19 am2. Does a new client always get the highest number in ConnectionList? If there are 5 connected clients, I suppose they are numbered 0..4 so the new one will get 5, right? Clients have their user names so basing on ConnectionList, I could have some list linking ConnectionList's client number to the user name but... Yes it is a simple TFPGList used. When a client connects it is added to the list via .Add at the end of the list and if it disconnects it is removed via .Delete. Quote from: r.lukasiak on October 14, 2021, 10:35:19 am3. Quote"when a client disconnects, it changes the indices of all clients connected" Is there any event going on in this situation? how can I know that someone disconnected to update my list? The list is automatically updated. To be notified on a disconnect you can register an event handler. In the ChatServer example this is done in: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     ACommunication.OnClose := @ConnectionClosed; ---------------------------------------------------------------------- r.lukasiak · 2021-10-14 22:30 QuoteTSocketHandler = class(TThreadedWebsocketHandler) ok, got it. QuoteThe list is automatically updated. To be notified on a disconnect you can register an event handler. In the ChatServer example this is done in: of course... :| I don't know what I was thinking of when asking this question. I even use that event to do some stuff. What I really wanted to know is how can I figure out which client disconnected, is there any way get its number in ConnectionsList? And there is another question about OnConnectionClosed. I testing the server using my gui and the HTML example. The server notices when the HTML client disconnects, whether I use the disconnect button, refresh the page or even kill the browser process, the server always triggers the OnConnectionClosed event but it doesn't happen with my GUI, whether I close it or just kill the process, the server doesn't notice it. How could I notify the server I disconnect? ---------------------------------------------------------------------- Warfley · 2021-10-15 15:21 Quote from: r.lukasiak on October 15, 2021, 12:30:26 amof course... :| I don't know what I was thinking of when asking this question. I even use that event to do some stuff. What I really wanted to know is how can I figure out which client disconnected, is there any way get its number in ConnectionsList? Currently there is no nice way of getting it, you basically need to iterate that list and search for it Quote from: r.lukasiak on October 15, 2021, 12:30:26 amAnd there is another question about OnConnectionClosed. I testing the server using my gui and the HTML example. The server notices when the HTML client disconnects, whether I use the disconnect button, refresh the page or even kill the browser process, the server always triggers the OnConnectionClosed event but it doesn't happen with my GUI, whether I close it or just kill the process, the server doesn't notice it. How could I notify the server I disconnect? Thats because you don't perform the closing handshake of websockets, but rather break the TCP connection (which will result in an exception). To close the connection gracefully just call "TWebsocketCommunicator.Close" and wait until TWebsocketCommunicator.Open is false ---------------------------------------------------------------------- r.lukasiak · 2021-10-20 16:18 QuoteTo close the connection gracefully just call "TWebsocketCommunicator.Close" and wait until TWebsocketCommunicator.Open is falseworked perfectly! QuoteCurrently there is no nice way of getting it, you basically need to iterate that list and search for itI see... anyway I managed to somehow solve it.  I dug through your code to find out how you implemented ConnectionList and what info it exactly stores. I created something like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ConnectedUser = record  UserName: string;  Handler: TWebsocketCommunicator;end;...varConnectedUserList = array of ConnectedUser; OnConnect I add the client to the list and the client after connecting automatically sends some kind of "hello message" with its username so the server adds it to the list. So far so good, thanks a lot! I tried to use these libs with LAMW for Android and I came across a problem with: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FCommunicat or.OnClose:=@StreamClosed;FCommunicator.OnReceiveMessage:=@ReceiveMess age; The compiler (for aarch64) is giving me: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit1.pas(138,26) Error: Variable identifier expectedchanging it to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i FCommunicat or.OnClose:=StreamClosed;FCommunicator.OnReceiveMessage:=ReceiveMessag e; helps to compile but it's obviously causing some problems. I ran the app on my phone and it connected to the server, server showed info about the connected client but the app crashed immediately. Have you tried it with LAMW? any hint? ---------------------------------------------------------------------- r.lukasiak · 2021-10-25 05:15 This solution seems to work, it stores the handlers and the usernames. However I'm stuck at sending messages from one client to another. All clients can communicate with the server and server can communicate with the clients, but the problem appears when the server receives a message and tries to send it to another clients. What I did is: 1. SERVER: Server starts 2. SERVER: Client connects - server stores the handler in ConnectionList and I added my ConnectedUsersList(record of TWebsocketCommunicator and string), it add the handler to my array in TSocketHandler.DoHandleCommunication 3. CLIENT: right after chat.FCommunicator.StartReceiveMessageThread, if chat.FCommunicator.Open is true, it sends a message to the server providing the client's username (each client has its unique username). 4. SERVER: Server receives this message (TSocketHandler.MessageReceived) and updates my array adding the username to the handler. 5. Steps 2-4 for every client (I've tested with just 3) At this point all clients are able to talk to the server and receive messages from the server. But if a client sends a message that's supposed to be send to another client, I come across a problem. Some client (apparently the last one) can send messages and these are delivered to another client with no problem but others can't. How I did it: 1. Client A sends a message that's supposed to get to Client B. 2. Server receives the message (TSocketHandler.MessageReceived). The message contains the Client B's username . 3. Server looks up my array to find the B's username: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i for i:=0 to length(ConnectedUsersList)-1 do if ConnectedUsersList[i].UserID=ClientB then ConnectedUsersList[i].Handler.WriteStringMessage.....4. Client B receives the message. I use locks whenever I  use the connection list or send messages. I performed some tests: 1# -Client A connects -Client B connects -Client A sends a message to the Server (the message doesn't reach the server) -Client B sends a message to the Server (the message goes through) -Server sends a message to Client A (the message goes through) -Server sends a message to Client B (the message goes through) at this point Server can send messages to both clients but only Client B can send messages to the Server -Client B sends a message to Client A (it goes through) -Client A sends a message to Client B (the message doesn't even reach the Server) at this point Server can still send message to both Clients but only Client B can send to the Server. What's even more disturbing, Client B's GUI closes properly, Client A's GUI doesn't want to close, I need to kill the process. 2# -3 clients connect -Server can send messages to all 3 clients -Only Client C can send message to the server I've tried to do the same using my ConnectedUserList and the original ConnectionList. Another solution was adding UserID: string variable to TWebsocketCommunicator class (wsstream), since it's threaded, each thread keeps it's UserID. No matter which solution I use, the result is the same. any idea what's wrong? ---------------------------------------------------------------------- Warfley · 2021-11-04 19:15 This sounds like a deadlock, have you checked when a thread acquires a lock and when it is released (e.g. by adding simple writeln with debug output)? ---------------------------------------------------------------------- r.lukasiak · 2021-11-05 15:21 I was taking care of the locks, as far as I understand how to use them. Anyway the code was kinda messy, I was implementing several ideas and then changing them if they didn't work so I decided to start from 0. I took the example client and server and implemented just 2 simple things but I have already faced the same problem, not all clients can send messages. First off, I added UserID: string to TWebsocketCommunicator class so the handler stores the user name: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i TWebsocketCommunincator = class  private ...  public    UserID: string;... end; The client just connects and sends its user name right after connecting: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Ttestchat.FormCreate(Sender: TObject);begin  if paramCount()>0 then MyUserID:=ParamStr(1) else MyUserID:='Client';     writeln(paramCount().ToString+' MyUserID='+MyUserID);   client := TWebsocketClient.Create(MyServerIP, MyServerPort);  try    chat := TSimpleChat.Create(client.Connect(TSocketHandler.Create));    finally    client.Free;  end;     chat.FCommunicator.StartReceiveMessageThread;     while not chat.FCommunicator.Open do              begin                writeln('not open, sleeping...');                Sleep(100);            end;     if chat.FCommunicator.Open then      begin        Writeln('chat.FCommunicator.Open=true');        chat.FCommunicator.WriteStringMessage(mtHello+MyUserID);        Writeln('Hello message sent');      end      else Writeln('chat.FCommunicator.Open=false');end; on the server side, when the client connects and sends the "hello" message: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TSocketHandler.MessageRecieved(Sender: TObject);  var    Messages: TWebsocketMessageOwnerList;    m: TWebsocketMessage;    Comm: TWebsocketCommunincator;    CL: TConnectionList;    i: integer;  begin    Comm := TWebsocketCommunincator(Sender);    Messages := TWebsocketMessageOwnerList.Create(True);    try      Comm.GetUnprocessedMessages(Messages);      for m in Messages do      if m is TWebsocketStringMessage then        begin          WriteLn('Message from ', Comm.SocketStream.RemoteAddress.Address,          ': ', TWebsocketStringMessage(m).Data);           //Hello message          if TWebsocketStringMessage(m).Data[1]=mtHello then        begin            writeln('Hello message received');            write('(MessageReceived) LOCK... ');            CL:=Connections.Lock;            writeln('...locked');            try            for i:=0 to CL.Count-1 do              begin                if CL[i]=Comm then                begin                    CL[i].UserID:=copy(TWebsocketStr ingMessage(m).Data,2,length(TWebsocketStringMessage(m).Data)-1);                  writeln('CL['+i.ToString+'].UserID:='+QuotedStr(CL[i].UserID));                  Break;                  end;              end;            finally              write('(MessageReceived) UNLOCK... ');            Connections.Unlock;              writeln('...unlocked');            end;          end;        end;    finally      Messages.Free;    end;  end;                        I also implemented some command so I can peek on ConnectionList: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TSocketHandler.DoHandleCommunication(    ACommunication: TWebsocketCommunincator);  var    str: string;    i: integer;    CL: TConnectionList;  begin    WriteLn('Connected to ', ACommunication.SocketStream.RemoteAddress.Address);    ACommunication.OnRecieveMessage := @MessageRecieved;    ACommunication.OnClose := @ConnectionClosed;    while ACommunication.Open do    begin      ReadLn(str);       if str='cl' then      begin        Write('(DoHandleCommunication) LOCK... ');      CL:=Connections.Lock;        Writeln('...locked');        try      begin          Writeln('Count='+CL.Count.ToString);          if CL.Count>0 then             for i:=0 to CL.Count-1 do               writeln(i.ToString+' '+CL[i].UserID);        end;        finally        Write('(DoHandleCommunication) UNLOCK... ');          Connections.Unlock;          Writeln('...unlocked');        end;      end;       if not ACommunication.Open then        Break;    end;    socket.Stop(True);  end;                   Server output: Quote$ ./chatServer Server is starting Connected to 192.168.1.1                       <- Client A connects Message from 192.168.1.1: ClientA          <- and everything is ok, it's able to send the Hello message Hello message received (MessageReceived) LOCK... ...locked CL[0].UserID:='ClientA' (MessageReceived) UNLOCK... ...unlocked Connected to 192.168.1.3                        <-The same with Client B, everything is ok. It connects from another PC Message from 192.168.1.3: ClientB Hello message received (MessageReceived) LOCK... ...locked CL[1].UserID:='ClientB' (MessageReceived) UNLOCK... ...unlocked                                                                      <-here I fired Client C but it didn't reach the server. I ran it on the same PC that ClientA cl                                                                   <-I typed 'cl' on the server to see what's in ConnectionList (DoHandleCommunication) LOCK... ...locked Count=2 0 ClientA 1 ClientB                                                          <-and there is no info about Client C (DoHandleCommunication) UNLOCK... ...unlocked Client A and Client B close without a problem but Client C doesn't, I need to kill the process. In case of ClientC, according to logs, it connected and sent the message: Quote$ ./TestChat ClientC 1 MyUserID=ClientC chat.FCommunicator.Open=true Hello message sent Another scenario, I ran just 2 clients, from the same PC: Quote$ ./chatServer Server is starting Connected to 192.168.1.3       <-Client1 connects and sends the Hello message but Server doesn't receive it Connected to 192.168.1.3       <-Client2 connects Message from 192.168.1.3: Client2   <-and Server receives the Hello message Hello message received (MessageReceived) LOCK... ...locked CL[1].UserID:='Client2'                              <-it's added to the handler (MessageReceived) UNLOCK... ...unlocked cl                                                                 <-I fire 'cl' command and.... (DoHandleCommunication) LOCK... ...locked Count=2 0                                                                   <-...both clients are connected but the Hello message from Client1 never reached Server 1 Client2                                                            so there is no user name. (DoHandleCommunication) UNLOCK... ...unlocked I performed several tests and random clients weren't able to connect or send the first message. It looks like I'm doing something wrong right in the beginning but I can't figure it out. Any clue?