gpt4 book ai didi

javascript - Coldfusion Websockets "User is typing..."功能

转载 作者:行者123 更新时间:2023-12-03 13:11:25 24 4
gpt4 key购买 nike

我正在使用 Coldfusion 10 的 websockets 并进行了简单的聊天以进行测试。我见过几个聊天,他们有“用户正在输入...”文本,当其他用户正在输入时显示。有谁知道如何有效地实现这一点?

最佳答案

再创建一个 channel “通知”,并在用户执行“keyDown”时发布“User is typing”,在“keyUp”时发布“emptystring”。

与您聊天时订阅此 channel 。接收方此 channel 的目标应是其内部 html 可以填充“用户正在键入”消息。

伪代码:

<cfwebsocket name="notificationSocket" 
onmessage="notifyHandler"
subscribeto="notificationChannel" >

<cfwebsocket name="ChatSocket"
onmessage="chatHandler"
subscribeto="chatChannel" >

<!-- Conversation displayer -->
<div id="messageBoard"></div>

<!-- your text message input area -->
<textarea onKeyDown="sayTyping()" onKeyUp="sayStopped()" id="chatInput"></textarea>
<div id="notifyArea"></div>
<input name="submit" onClick="publishMessage()"></textarea>

<script>

/*chat Functions*/
var publishMessage = function(){
var msg = document.getElementById('chatInput').value;
mycfwebsocketobject.publish("chatChannel", msg );
};

var chatHandler = function(msgObj){
document.getElementById('messageBoard').innerHTML += ColdFusion.JSON.encode(msgObj);
};


/*notifying Functions*/
var notifyHandler = function(noteObj){
document.getElementById('notifyArea').innerHTML = ColdFusion.JSON.encode(noteObj);
};

var sayTyping = function(){
mycfwebsocketobject.publish("notificationchannel","User is Typing..." );
};
var sayStopped = function(){
mycfwebsocketobject.publish("notificationchannel","" );
};

</script>

另一个改进是让一个 div 已经包含文本“用户正在输入”,并且您的 channel 将文本广播为“显示”和“不显示”。这基本上是给显示和隐藏它的类名。流量减少。

方法 2:使用相同的 channel

<cfwebsocket name="ChatSocket" 
onmessage="chatHandler"
subscribeto="chatChannel" >

<!-- Conversation displayer -->
<div id="messageBoard"></div>

<!-- your text message input area -->
<textarea onKeyDown="sayTyping()" onKeyUp="sayStopped()" id="chatInput"></textarea>
<div id="notifyArea" class="no">User is typing...</div>
<input name="submit" onClick="publishMessage()"></textarea>

<script>

/*chat Functions*/
var publishMessage = function(){
var msg = document.getElementById('chatInput').value;
mycfwebsocketobject.publish("chatChannel", msg );
};

var chatHandler = function(msgObj){
var msg = ColdFusion.JSON.encode(msgObj);
if (msg == '@userTyping@-yes'){
notifyHandler('yes');
}
else if (msg == '@userTyping@-no'){
notifyHandler('no');
}
else {
document.getElementById('messageBoard').innerHTML += msg;
}
};


/*notifying Functions*/
var notifyHandler = function(action){

document.getElementById('notifyArea').className = action;

/*call the notify handler with off to stop showing the user is typing message
after a certain interval of time. This is to avoid someone believing that
partner is still typing even when the connection is lost*/

if(action == 'on'){
setTimeout(function(){notifyHandler('no')},250);
}
};

var sayTyping = function(){
mycfwebsocketobject.publish("chatChannel","@userTyping@-yes" );
};
var sayStopped = function(){
mycfwebsocketobject.publish("chatChannel","@userTyping@-no" );
};

</script>
<style>
.yes { display:block;}
.no { display:none;}
</style>

人们总是可以通过将消息键入“@userTyping@-yes”或“@userTyping@-no”来欺骗此代码。但正如我所说,这只是一个 POC。此外,对于您提到的超时,keyUp 无论如何都会处理它。但您也可以通过 setTimeout() 调用 notifyHandler(),如上所示。

关于javascript - Coldfusion Websockets "User is typing..."功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16777849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com