Coldfusion中的Instant Messenger

时间:2011-11-15 05:12:17

标签: coldfusion httpclient

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

因此,我无法理解该主题,从哪里开始以及要经历的技术是什么?

1 个答案:

答案 0 :(得分:4)

嗯,这可能是一个很大的话题,但是这里有一些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>