从 HTML 页面连接到coldfusion websocket [英] connect to coldfusion websocket from HTML page

查看:25
本文介绍了从 HTML 页面连接到coldfusion websocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个连接到 ColdFusion 2016 服务器的网络套接字,但我想从 HTML 页面(不是 cfm)打开它,所以我没有使用 cfwebsocket 标记的选项.我想要的是替代它..我试过下面的代码

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 replacement for it.. I have tried the following code

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方法没有运行

the problem is that I cant open the connection and the onOpen method does not run

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

another problem is that when I want to subscribe to any channel

chatSocket.subscribeTo('chat');

我不断收到以下错误

TypeError: chatSocket.subscribeTo is not a function

推荐答案

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

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

然后在web socket对象上写如下命令订阅任意频道

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

这篇关于从 HTML 页面连接到coldfusion websocket的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆