Jump to content
rabmo

Connect to wss://

Recommended Posts

Hi, can I connect to wss:// server with ICS? If yes is there a demo or example of code? I did not find anything in google.

Share this post


Link to post

I added a web socket server implementation to ICS last year, There is a new sample OverbyteIcsWebSocket and web page websocketclient.html that accesses the server. Note there are no plans for an ICS Websocket client component, the normal ICS server/client components can be used for sending data outside the browser environment.

 

Angus


 

Share this post


Link to post
On 8/19/2021 at 3:20 PM, Angus Robertson said:

I added a web socket server implementation to ICS last year, There is a new sample OverbyteIcsWebSocket and web page websocketclient.html that accesses the server. Note there are no plans for an ICS Websocket client component, the normal ICS server/client components can be used for sending data outside the browser environment.

 

Angus


 

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!

Share this post


Link to post
Posted (edited)

Sorry, other sample not in the main project group that is a victim of functions moving between units to ease linkage.

 

Please just add  OverbyteIcsUtils to the OverbyteIcsWebSockets uses clause.

 

Angus

 

 

 

Edited by Angus Robertson

Share this post


Link to post
9 minutes ago, Angus Robertson said:

Sorry, other sample not in the main project group that is a victim of functions moving between units to ease linkage.

 

Please just add  OverbyteIcsUtils to the OverbyteIcsWebSockets uses clause.

 

Angus

 

 

 

Excellent, that works. Thank you!

Share this post


Link to post
On 5/20/2022 at 10:00 PM, Martybartfast said:

Excellent, that works. Thank you!

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?

 

  • Like 1

Share this post


Link to post

Sorry, I did not write the WebSockets component, and only minimally tested it.   Perhaps someone who actually uses WebSockets will have a suggestion.

 

Angus

Share this post


Link to post
3 minutes ago, Angus Robertson said:

Sorry, I did not write the WebSockets component, and only minimally tested it.   Perhaps someone who actually uses WebSockets will have a suggestion.

 

Angus

Ah yes, I forgot. Thanks anyway.

Anyone else can help?

Share this post


Link to post

You know ICS is sending the data, so the issue here is your Javascript to receive the data.  You say you are using the ICS sample HTML page, why do you think it is designed to receive data from the server? 

 

Angus

 

Share this post


Link to post

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!

Screenshot 2022-05-24 at 10.33.26 PM.png

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×