2

我写了两个文件。应用程序.cfc

component output="false"{
 this.name = "WebSocket";
 this.wschannels = [{name:"news"}];
}

索引.cfm

<html>
<head>
    <script>
        function myfun(){
            myWSDemo.publish("news","This is a test message.");
        }

        function myfun1(){
            myWSDemo.subscribe("news");
        }

        var messageHandler = function(message) {
            console.log("Message Handler is called.");
            console.log(message);
            if(message.type == "data"){
                 var txt=document.getElementById("message"); 
                 txt.innerHTML+= message.data + "<br/>";
            }
        }   
    </script>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" />
</head>
<body>
    <cfwebsocket name="myWSDemo" onmessage="messageHandler">    
    <input type="button" id="sub" onclick="myfun1()" value="Subscribe"/>
    <input type="button" id="pub" onclick="myfun()" value="Publish"/>
    <div id="message"></div>
</body>
</html>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js" />当页面中不包含此代码时,此代码运行良好。

当我添加 jquery cdn 链接时,它没有初始化 web-socket 对象。有人对这个问题有任何想法吗?

4

0 回答 0