ColdFusion Websocket subscribe() JavaScript 在页面加载时调用时不工作
ColdFusion Websocket subscribe() JavaScript not working when called on page load
我有一个基本的 ColdFusion 页面,它使用 websockets 订阅一个频道,然后 display/publish 消息,即一个非常粗糙的聊天应用程序。
代码如下。
问题是:当我点击订阅按钮并调用 SubscribeToChannel()
时,用户成功订阅了频道并且一切正常。当我发送消息时,websocket 发布它就好了。 (如注释2所示)
然而,当我调用 SubscribeToChannel()
on document ready 时,该函数被调用,但用户似乎没有订阅,因为每次我发布消息时,它都没有出现。 (如注释1所示)
因此,SubscribeToChannel()
在通过单击按钮调用时有效,但在 $(function) document ready
过程中无效。为什么?
我希望用户在页面加载时订阅,而不必单击按钮来启动订阅。
(Please note, I am NOT looking to use the SubscribeTo
attribute of
the cfwebsocket tag, as it does not allow for custom parameters. )
<cfwebsocket
name="chatWS"
secure="true"
onMessage="messageHandler"/>
<doctype html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.1.0.min.js"
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
crossorigin="anonymous">
</script>
<script type="text/javascript">
function subscribeToChannel()
{
alert('subscribing');
chatWS.subscribe("chat", messageHandler);
}
messageHandler = function(aEvent,aToken) {
if (aEvent.data) {
$( "#myChatArea" ).append( aEvent.data + "<br />");
}
}
sendMessage = function() {
var msg2Send = $( "#myMessage" ).val();
if (msg2Send) {
chatWS.publish("chat", msg2Send);
}
}
$(function(){
<!--- 1) Subscribe DOES NOT WORK when called on page ready --->
subscribeToChannel();
});
</script>
</head>
<body>
<div id="myChatArea"><cfoutput>#Application.chatHistory#</cfoutput></div>
<input type="text" id="myMessage" /><input id="myButton" type="button" value="Send Message" onClick="sendMessage()" />
<input id="subscribe" type="button" value="Subscribe" onClick="subscribeToChannel()" />
<!--- 2) Subscribe WORKS when called via this button --->
</body>
</html>
来自评论
您是否尝试过使用 cfwebsocket
标签的 onOpen
属性?我认为 DOM 可能已加载,但 websocket 连接可能尚未建立。 onOpen
属性应该为您提供一种在 websocket 建立连接后 调用 JavaScript 函数的方法。
onOpen - Optional - The JavaScript function that is called when the WebSocket establishes a connection.
添加到您的代码示例中:
<cfwebsocket
name="chatWS"
secure="true"
onMessage="messageHandler"
onOpen="openHandler" />
<doctype html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.1.0.min.js"
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
crossorigin="anonymous">
</script>
<script type="text/javascript">
function subscribeToChannel()
{
alert('subscribing');
chatWS.subscribe("chat", messageHandler);
}
messageHandler = function(aEvent,aToken) {
if (aEvent.data) {
$( "#myChatArea" ).append( aEvent.data + "<br />");
}
}
openHandler = function() {
subscribeToChannel();
}
// the rest of your code here ...
我有一个基本的 ColdFusion 页面,它使用 websockets 订阅一个频道,然后 display/publish 消息,即一个非常粗糙的聊天应用程序。
代码如下。
问题是:当我点击订阅按钮并调用 SubscribeToChannel()
时,用户成功订阅了频道并且一切正常。当我发送消息时,websocket 发布它就好了。 (如注释2所示)
然而,当我调用 SubscribeToChannel()
on document ready 时,该函数被调用,但用户似乎没有订阅,因为每次我发布消息时,它都没有出现。 (如注释1所示)
因此,SubscribeToChannel()
在通过单击按钮调用时有效,但在 $(function) document ready
过程中无效。为什么?
我希望用户在页面加载时订阅,而不必单击按钮来启动订阅。
(Please note, I am NOT looking to use the
SubscribeTo
attribute of the cfwebsocket tag, as it does not allow for custom parameters. )
<cfwebsocket
name="chatWS"
secure="true"
onMessage="messageHandler"/>
<doctype html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.1.0.min.js"
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
crossorigin="anonymous">
</script>
<script type="text/javascript">
function subscribeToChannel()
{
alert('subscribing');
chatWS.subscribe("chat", messageHandler);
}
messageHandler = function(aEvent,aToken) {
if (aEvent.data) {
$( "#myChatArea" ).append( aEvent.data + "<br />");
}
}
sendMessage = function() {
var msg2Send = $( "#myMessage" ).val();
if (msg2Send) {
chatWS.publish("chat", msg2Send);
}
}
$(function(){
<!--- 1) Subscribe DOES NOT WORK when called on page ready --->
subscribeToChannel();
});
</script>
</head>
<body>
<div id="myChatArea"><cfoutput>#Application.chatHistory#</cfoutput></div>
<input type="text" id="myMessage" /><input id="myButton" type="button" value="Send Message" onClick="sendMessage()" />
<input id="subscribe" type="button" value="Subscribe" onClick="subscribeToChannel()" />
<!--- 2) Subscribe WORKS when called via this button --->
</body>
</html>
来自评论
您是否尝试过使用 cfwebsocket
标签的 onOpen
属性?我认为 DOM 可能已加载,但 websocket 连接可能尚未建立。 onOpen
属性应该为您提供一种在 websocket 建立连接后 调用 JavaScript 函数的方法。
onOpen - Optional - The JavaScript function that is called when the WebSocket establishes a connection.
添加到您的代码示例中:
<cfwebsocket
name="chatWS"
secure="true"
onMessage="messageHandler"
onOpen="openHandler" />
<doctype html>
<html>
<head>
<script
src="https://code.jquery.com/jquery-3.1.0.min.js"
integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="
crossorigin="anonymous">
</script>
<script type="text/javascript">
function subscribeToChannel()
{
alert('subscribing');
chatWS.subscribe("chat", messageHandler);
}
messageHandler = function(aEvent,aToken) {
if (aEvent.data) {
$( "#myChatArea" ).append( aEvent.data + "<br />");
}
}
openHandler = function() {
subscribeToChannel();
}
// the rest of your code here ...