Search code examples
delphicoldfusionwebsocketdelphi-2009coldfusion-10

HTML5 WebSockets not working. Server=ColdFusion, Client=Delphi


I am coding a message distribution system. Server is ColdFusion (CF) 10, using the new <cfwebsocket> feature set. Client will be written in Delphi 2009.

If I write the client in ColdFusion (using the <cfwebsocket> tag) things work fine: I can send messages between two clients. So the server side appears to be working.

No such luck with a Delphi client. I've tried two component libraries so far (Delphi on Rails and sgcWebSockets). Both seem to establish a connection to the CF server, but messages are not sent or received. I'm fairly certain Delphi is making a connection to the server as no exceptions are thrown if I specify the correct address, whereas I get an exception if I specify a different port or URI.

I think the missing link is in my understanding of "channels". It's easy in CF: you specify the channel to subscribe to or send messages to and it works. But the concept of "channels" doesn't seem to exist much outside of ColdFusion. I've searched w3.org, Google, etc, and don't see much about channels in the HTML5 WebSocket specs. Some references, but nothing clear, especially in the examples.

In summary, my questions:

  1. Are "channels" part of the standard WebSocket API, and if so,
  2. How do I subscribe to a channel using one of the Delphi WebSocket libraries I mentioned? Shouldn't it be as easy as ws://[server]:[port]/[channel]?
  3. How to debug WebSocket connections & traffic on the CF server?

Many thanks. This is my first post on StackOverflow; apologies if it's a tad long.


Solution

  • Thanks for the tips! I have done a bit of debugging since I posted the question and have discovered some answers.

    Are "channels" part of the standard WebSocket API?

    From what I can tell, no. Channels appear to be a ColdFusion-specific concept.

    How do I subscribe to a channel using one of the Delphi WebSocket libraries I mentioned? Shouldn't it be as easy as ws://[server]:[port]/[channel]?

    As mentioned in my original question, Delphi (using the TsgcWebSocketClient component) was connecting successfully to the ColdFusion (CF) WebSocket server. However, I found I was omitting a step: subscribing to the CF "channel". I discovered this by using Microsoft Network Monitor to compare the Delphi client traffic to the CF client traffic. The CF client was sending an additional string to the server after connecting:

    {"ns":"coldfusion.websocket.channels","type":"welcome","authKey":"6DD10C406710970271EDA2295C409D38","subscribeTo":"signals","appName":"Test"}
    

    This subscribes the client to the "signals" channel. So, I added the following line to my Delphi code after a connection was made:

    sgcWebSocketClient.WriteData( '{"ns":"Delphi","type":"welcome","authKey":"6DD10C406710970271EDA2295C409D38","subscribeTo":"signals","appName":"Test"}' );
    

    and the Delphi client was immediately able to receive messages from the CF "signals" channel.

    FYI, here is how to send a message ("Hello, World!" in this case) from the Delphi client to the "signals" channel:

    sgcWebSocketClient.WriteData( {"ns":"Delphi","type":"publish","channel":"signals","data":"Hello, World!","appName":"Test"}
    

    How to debug WebSocket connections & traffic on the CF server?

    I found Microsoft Network Monitor, with a port filter applied, was the easiest method. It does not currently support loopback/localhost monitoring, so I used another computer on our LAN to generate the traffic.