Skip to main content
Inspiring
October 28, 2019
Question

How to call WebSockets directly via Java?

  • October 28, 2019
  • 1 reply
  • 623 views

In order to enable WebSockets, the following example is given:

 

application.cfc

 

this.name = "WebSocketDemo"; this.wschannels = [{name="stocks"},{name="chat"}];

 

When application variables are dumped, this.name looks like it gets translated into application.applicationname.  However, wschannels is nowhere to be found.  How would one be able to enable websockets via a call in application.cfm or any cfm page?

 

I tried this but neither worked:

 

<cfset application.wschannels = [{name="stocks"},{name="chat"}]>

<cfset application.this.wschannels = [{name="stocks"},{name="chat"}]>

This topic has been closed for replies.

1 reply

BKBK
Community Expert
Community Expert
October 29, 2019

// The application name (this setting is not for the websocket).. 

this.name = "WebSocketDemo";

// You must include the listener (CFC) of a websocket. 

this.wschannels = [{name="stocks",cfclistener="stockListener"},{name="chat",cfclistener="chatListener"}];

 

Example: ChatListener.cfc

component extends="CFIDE.websocket.ChannelListener"
{
  public boolean function allowSubscribe(Struct subscriberInfo)
   {
     if(structKeyExists(subscriberInfo,"utype"))
       {
        //Validating if a user has publish right & adding a new key to ConnectionInfo 
          if(subscriberInfo.utype eq "Moderator" or subscriberInfo.utype eq "Member" ){
              subscriberInfo.connectioninfo.havepublishright=true;
              return true;
          } else {
              subscriberInfo.connectioninfo.havepublishright=false;
              return false;
          }
       
       }
   }
  
   public boolean function allowPublish(Struct publisherInfo)
   {
       if(structKeyExists(publisherInfo.connectioninfo,"havepublishright")){
         if(publisherInfo.connectioninfo.havepublishright){
            return true;
         } else {
            return false;
         }
       }
   }
 
}
Tom WooAuthor
Inspiring
October 29, 2019

Thanks -- but I don't see how this would work in, say, an application.cfm file.  "this.wschannel" seems to only exist inside application.cfc -- or am I mistaken?

BKBK
Community Expert
Community Expert
October 30, 2019

I would advise you to use Application.cfc. Application.cfm is outdated.

But, to answer your question, Application.cfm may use something like

 

<cfapplication name="WebSocketDemo"
wschannels=[{name="stocks",cfclistener="stockListener"},{name="chat",cfclistener="chatListener"}]
clientmanagement="No"
sessionmanagement="Yes"
sessionTimeout=createTimespan(0,0,30,0)>