2

私は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