Martybartfast
Members-
Content Count
7 -
Joined
-
Last visited
Everything posted by Martybartfast
-
I'm using ICS Web Sockets, and everything is working good so far - browsers connect to my server, and the ICS "echo" sample works fine. However, I need to send an unsolicited string from the server to all connected clients. Anyone know how to do this? I've tried and tried, and can't find any way to access the web socket clients. If I just do "Webserver.client[n].sendstr(s);" then the browser just disconnects. What I'm trying to do this something like this: WebSocketServer: TWSocketServer; ... procedure THttpGUI.Start; begin ... if not(assigned(WebSocketServer)) then // Don't do if already created begin WebSocketServer := TWSocketServer.Create(nil); // Create the WebSocket server WebSocketServer.Proto := 'tcp'; // TCP/IP protocol WebSocketServer.ClientClass := TWebSockSrvClient; // Web socket WebSocketServer.Banner := ''; // Remove banner because we handle websocket handshake WebSocketServer.OnBgException := WebSocketServer_BgException; WebSocketServer.OnClientConnect := WebSocketServer_ClientConnect; WebSocketServer.OnClientDisconnect := WebSocketServer_ClientDisconnect; end; end; // Connection stuff all works fine, not shown here ... // Send a string to all connected clients (browsers). procedure THttpGUI.WebSocket_SendString(s : string); var n : integer; begin if assigned(WebSocketServer) then begin for n := 0 to WebSocketServer.ClientCount-1 do begin WebSocketClient[n].sendString(s); // Something like this!!! end; end; end;
-
Here's the full unit XTEC_HttpSrv_GUI.pas.zip
-
I don't think anything is wrong with the Javascript. Here's why: I'm using the code from the ICS Web Sockets sample: function init(){ var host = "ws://192.168.1.9:42071/xcs"; try { socket = new WebSocket(host); log('WebSocket - status ' + socket.readyState); socket.onopen = function(msg){ log("Welcome - status " + this.readyState); }; socket.onmessage = function(msg){ log("Received (" + msg.data.length + " bytes): " + (msg.data.length < 5000 ? msg.data : (msg.data.substr(0, 30) + '...'))); }; socket.onclose = function(msg){ log("Disconnected - status " + this.readyState); }; } catch(ex) { log(ex); } } So, for testing I made a global IWebSocketConnection variable, then in the Connected callback I copy the 'conn' IWebSocketConnection to my array. Then I can use that to successfully send to the browsers. So it seems the Javascript is fine. I just can't work out how to access the IWebSocketConnection for each client. I do NOT want to have my own array of connections... this must be possible another way? This is my send code with my work-around (s = "Hello from Martin"): var WebSocketConn : array of IWebSocketConnection; ... procedure THttpGUI.WebSocketServer_Connected(Sender: TObject; con: IWebSocketConnection); var i: Integer; msg: String; begin if Assigned(con) then begin ... setLength(WebSocketConn, WebSocketServer.ClientCount); WebSocketConn[WebSocketServer.ClientCount - 1] := con; // Store connection - testing!!! ... end; end; // Send a string to all connected clients (browsers). procedure THttpGUI.WebSocket_SendString(s : string); var n : integer; begin if assigned(WebSocketServer) then begin for n := 0 to WebSocketServer.ClientCount-1 do begin WebSocketConn[n].sendString(s); // This works, but is NOT the right way to do this!!! end; end; end; As you see, I get my "Hello from Martin" string fine on the browser. Hummmm.... much appreciate any help!
-
Ah yes, I forgot. Thanks anyway. Anyone else can help?
-
Hi again Angus. So I've got Web Sockets working in my application, everything connects as it should, and I can send a welcome message in the Connected event. Problem now is that once the connection is established, if I then send a string to the client(s), the browser immediately disconnects from the Web Socket connection. Delphi: s : string; n : integer; b : integer; ... s := 'Hello'; for n := 0 to WebSocketServer.ClientCount-1 do begin b := WebSocketServer.Client[n].SendStr(s); end; b does indeed show the correct number of characters have been sent. The HTML is taken directly from your sample: function init(){ var host = "ws://127.0.0.1:42071/xcs"; try { socket = new WebSocket(host); log('WebSocket - status '+socket.readyState); socket.onopen = function(msg){ log("Welcome - status "+this.readyState); }; socket.onmessage = function(msg){ log("Received ("+msg.data.length+" bytes): " + (msg.data.length < 5000 ? msg.data : (msg.data.substr(0, 30) + '…'))); }; socket.onclose = function(msg){ log("Disconnected - status "+this.readyState); }; } catch(ex) { log(ex); } } Any idea what I'm doing wrong?
-
Excellent, that works. Thank you!
-
Hi Angus. I'm trying to run the ICS "OverbyteIcsWebSocketSrv.dproj" sample in Delphi 11.1, but I get errors when I build: [dcc32 Error] OverbyteIcsWebSockets.pas(695): E2003 Undeclared identifier: 'Base64Encode'. [dcc32 Error] OverbyteIcsWebSockets.pas(811): E2003 Undeclared identifier: 'Base64Encode' Delphi can't find the "Base64Encode" function. Any ideas? Am I being totally stupid? :) I've looked all over for a solution... Hope you can assist. Thanks mate!