andremussche / DelphiWebsockets

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

Push data to browser clients #24

Open nkaku opened 7 years ago

nkaku commented 7 years ago

Hi Andre,

I have started my server as follows:

procedure TForm1.btnCreateWSServerClick(Sender: TObject); begin myServer := TIdWebsocketServer.Create(Self); myServer.DefaultPort := 12345; myServer.Active := True; end;

On the html page, I have established connection as follows:

<script type='text/javascript'>

var exampleSocket = new WebSocket('ws://localhost:12345');

exampleSocket.onopen = function(){
    console.log('Connection open!');
}

exampleSocket.onclose = function(){
    console.log('Connection close!');
}

exampleSocket.onmessage = function(e){
    var server_message = e.data;
    console.log(server_message);
}

...

When I connect, the browser logs Connection Open and when I close my Delphi application, browser logs Connection closed. So far so good.

Now I am trying to send a message from the server to the browser client using this

procedure TForm1.btn2Click(Sender: TObject); begin myServer.SocketIO.SendToAll('{"data":"hello"}'); end;

but nothing happens on the browser.

what am I doing wrong?

Nirav