【问题标题】:Instant Messenger in ColdfusionColdfusion 中的即时通讯工具
【发布时间】:2011-11-15 05:12:17
【问题描述】:

我是 ColdFusion 的新手,我想在其中创建一个与 Google 和 Facebook 的聊天客户端非常相似的即时通讯工具?

那么,由于我无法理解该主题,那么从哪里开始以及要通过哪些技术?

【问题讨论】:

标签: coldfusion httpclient


【解决方案1】:

嗯,这可能是一个巨大的话题,但这里有一些 jquery+cfm+ajax 聊天代码,我写了一段时间,可能会为你带来一些想法:

ajax_chat_client.cfm

<div id="chatlog">

</div>

<input type="text" name="message" id="chat_message"> <input type="button" value="post" onclick="sendMessage()">

<br>Last HTTP Request made:
<span id="last_request_time"></span>

<style>
#chatlog {

        width: 500px;
        height: 300px;
        overflow: auto;
        border: thin black solid;

}
</style>

<script src="jquery-1.3.2.min.js" /></script>
<script>

var lastMessage = 0;

function sendMessage(){
        $.get("ajax_chat_post.cfm?message=" + escape($("#chat_message").val()));
        $("#chat_message").val("");
}

function checkNewPosts()
{
        $("#last_request_time").html(new Date().getTime());
        $.getJSON('ajax_chat_get.cfm?lastchat=' + lastMessage, function(data){

                while(data.length > lastMessage)
                {
                        $("#chatlog").prepend((lastMessage+1) + ') ' + data[lastMessage] + '<br>');

                        lastMessage++;

                }


                setTimeout(checkNewPosts(), 500);

        });

}

 $(document).ready(function(){


        checkNewPosts();


 });

</script>

ajax_chat_get.cfm

<cfapplication name="chatter">

<cfset threadLife = 30000><!--- thirty seconds --->


<cfset threadStart = getTickCount()>
<cfparam name="lastchat" default="0">

<cfif not IsDefined("application.chatlog")>
        <cfset application.chatlog = ArrayNew(1)>
</cfif>

<cfloop condition="threadLife+threadStart GT getTickCount()">

        <cfoutput>

                <cfif ArrayLen(application.chatlog) GT lastchat>
                                #SerializeJSON(application.chatlog)#
                        <cfabort>
                </cfif>

                <cfthread action="sleep" duration="500" />

        </cfoutput>

</cfloop>
<cfoutput>#SerializeJSON(ArrayNew(1))#</cfoutput>

ajax_chat_post.cfm

<cfapplication name="chatter">

<cfif not IsDefined("application.chatlog")>
        <cfset application.chatlog = ArrayNew(1)>
</cfif>

<cfparam name="message" default="">

<cfif len(message)>
        <cfset arrayAppend(application.chatlog, message)>
</cfif>

【讨论】:

  • 请注意 jquery 的包含 - 版本无关紧要。
  • 感谢您的精彩信息。我想再问一件事,是否可以保留聊天记录、笑脸支持、与多人一对一聊天。
  • 当然,你可以用很多不同的方式来扩展它——天空是极限!在我的代码中,我只有一个大的聊天历史池 (application.chatlog),每个人都共享 - 您可以轻松地为各个用户之间的聊天历史构建单独的池。笑脸和类似的东西只是 jquery 方面的更多显示逻辑。
  • 您可以通过使用数据库层的持久性来非常轻松地保留聊天历史记录,而不是像我在这里所做的那样仅在应用程序范围内填充消息。
  • 感谢@Jake Feasel 提供的信息真的很棒。非常感谢
猜你喜欢
  • 2011-05-17
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多