connect to coldfusion websocket from HTML page

后端 未结 2 636
执念已碎
执念已碎 2021-01-14 21:34

I want to open a web-socket to a ColdFusion 2016 server, but I want to open it from HTML page (not cfm) so I don\'t have the option to use cfwebsocket tag. what I want is a

相关标签:
2条回答
  • 2021-01-14 21:50

    in case someone stumbled with the same issue , I have found the solution first connect to coldfusion web socket path

    var chatSocket = new WebSocket("ws://"+webSocket_IP+":8579/cfusion/cfusion");  
    

    then write the following command on the web socket object to subscribe to any channel

    {"ns":"coldfusion.websocket.channels","type":"welcome","subscribeTo":"CHANNELNAME","appName":"APPNAME"}
    

    and in case you want to write a message use the following:

    {"ns":"coldfusion.websocket.channels","type":"publish","channel":"CHANNELNAME","data":"hi","appName":"APPNAME"}
    
    0 讨论(0)
  • 2021-01-14 21:52

    For clarification on Hamzeh's Answer.

    How to establish a connection

    var chatSocket = new WebSocket("ws://"+webSocket_IP+":8579/cfusion/cfusion");  
    

    How to subscribe to a channel

    chatSocket.send(
      JSON.stringify( {
        appName: "customoptionexample1", //App Name
        authKey: "739CAAF6CA8CA73DCCDB9305225F7D48",
        ns: "coldfusion.websocket.channels",
        subscribeTo: "bidchannel", //Channel subscribing to
        type: "welcome"
      } )
    );
    

    How to Send Data

    chatSocket.send(
      JSON.stringify( {
        "ns": "coldfusion.websocket.channels",
        "type": "publish",
        "channel": "bidchannel", // Channel Name
        "appName": "customoptionexample1", //App Name
        "data": "Bid placed by adfadfadf Amount 66",
        "customOptions": {
          "value": "66"
        }
      } )
    );
    

    Setting up normal web socket callbacks

    chatSocket.onopen = function() {
      console.log( 'opened' );
    };
    chatSocket.onclose = function() {
      console.log( 'onclose' );
    };
    chatSocket.onerror = function() {
      console.log( 'onerror' );
    };
    chatSocket.onmessage = function( event ) {
      //This parses the data and just prints the data and not the meta data.
      console.log( 'onmessage', JSON.parse(event.data).data ); 
    };
    
    0 讨论(0)
提交回复
热议问题