从HTML页面连接到coldfusion websocket



我想打开一个web套接字到ColdFusion 2016服务器,但我想从HTML页面(不是cfm)打开它,所以我没有选择使用cfwebsocket标签。我想换一个新的。我尝试了以下代码

var webSocket_IP = '192.168.1.223';
        var chatSocket = new WebSocket("ws://"+webSocket_IP+":8579/cfusion/cfusion");                   
        chatSocket.onopen = function () {
            alert('OPEN');
        };
        chatSocket.onmessage = function () {
            alert('a message was recieved');
        };
        chatSocket.onError = function () {
            alert('Error');
        };

问题是我不能打开连接和onOpen方法不运行

另一个问题是当我想订阅任何频道

chatSocket.subscribeTo('chat');

我一直得到以下错误

TypeError: chatSocket.subscribeTo is not a function

对Hamzeh的回答进行澄清。

如何建立连接

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

如何订阅频道

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

如何发送数据

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"
    }
  } )
);

设置正常的web套接字回调

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 ); 
};

如果有人遇到同样的问题,我已经找到了解决方案首先连接到coldfusion web套接字路径

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

然后在web套接字对象上写入以下命令以订阅任何通道

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

,如果您想写消息,请使用以下命令:

{"ns":"coldfusion.websocket.channels","type":"publish","channel":"CHANNELNAME","data":"hi","appName":"APPNAME"}

相关内容

  • 没有找到相关文章

最新更新