andremussche / DelphiWebsockets

Websockets and Socket.io for Delphi
251 stars 129 forks source link

broadcast message to all connected clients ? #10

Open DRAMA22 opened 8 years ago

DRAMA22 commented 8 years ago

i am using that way to get some text on server then i try to broadcast back to all connected clients so i try

procedure TForm1.ServerMessageTextReceived(const AContext: TIdServerWSContext;
  const aText: string);
var
  strm: TStringStream;
begin
  ShowMessageInMainthread(aText);
  strm := TStringStream.Create('SERVER: ' + aText);
  //AContext.IOHandler.WriteLn('goi', IndyTextEncoding_UTF8);
  broadcastmessage('goi');
end;

procedure TForm1.broadcastmessage(const text: string);
var
 List: TList;
 i : integer;
begin
List := Server.Contexts.LockList;
try

for I := 0 to List.Count - 1 do
begin
SendCommand(text);
end;

finally
Server.Contexts.UnlockList;
end;

end;

procedure TForm1.SendCommand(const textprm: String);
begin

//what to do here ?
end;

or better yet can i setup tconnection to each connected client ?as we do in normal tcpserver as example 

  **TConnection = class(TIdServerContext)
var to define connections as name etc..**
andremussche commented 8 years ago

it is much easier to do this :) server.SocketIO.EmitEventToAll(C_SERVER_EVENT, SO(['data', 'pushed from server']) );

see for a demo:

https://github.com/andremussche/DelphiWebsockets/blob/master/Demo/Unit1.pas

2016-09-09 7:26 GMT+02:00 DRAMA22 notifications@github.com:

i am using that way to get some text on server then i try to broadcast back to all connected clients so i try

procedure TForm1.ServerMessageTextReceived(const AContext: TIdServerWSContext; const aText: string); var strm: TStringStream; begin ShowMessageInMainthread(aText); strm := TStringStream.Create('SERVER: ' + aText); //AContext.IOHandler.WriteLn('goi', IndyTextEncoding_UTF8); broadcastmessage('goi'); end;

procedure TForm1.broadcastmessage(const text: string); var List: TList; i : integer; begin List := Server.Contexts.LockList; try

for I := 0 to List.Count - 1 do begin SendCommand(text); end;

finally Server.Contexts.UnlockList; end;

end;

procedure TForm1.SendCommand(const textprm: String); begin

//what to do here ? end;

or better yet can i setup tconnection to each connected client ?as we do in normal tcpserver as example

TConnection = class(TIdServerContext) var to define connections as name etc..

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/andremussche/DelphiWebsockets/issues/10, or mute the thread https://github.com/notifications/unsubscribe-auth/AC5IjgZFUBPPoj2EON-KoDD7qcs85YX1ks5qoO3ogaJpZM4J4tiG .

DRAMA22 commented 8 years ago

the problem is i need to define connection who is entered to my server with username and id etc.. so i send to each one who matched some condition

andremussche commented 8 years ago

Can you please send me a small demo project1 so I can reproduce and debug the problem?

2016-09-13 9:48 GMT+02:00 DRAMA22 notifications@github.com:

the problem is i need to define connection who is entered to my server with username and id etc.. so i send to each one who matched some condition

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/andremussche/DelphiWebsockets/issues/10#issuecomment-246602524, or mute the thread https://github.com/notifications/unsubscribe-auth/AC5IjsWyXGvehthMbemmf45jxwzebkl2ks5qplUxgaJpZM4J4tiG .