我写了两个文件。 的Application.cfc
component output="false"{
this.name = "WebSocket";
this.wschannels = [{name:"news"}];
}
Index.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对象。 谁对这个问题有任何想法?