Created
September 7, 2012 03:34
-
-
Save robertz/3662849 to your computer and use it in GitHub Desktop.
Chat controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent> | |
<cfscript> | |
variables.instance = structNew(); | |
instance.chatHistory = arrayNew(1); | |
instance.clients = arrayNew(1); | |
instance.messageTotal = arrayLen(instance.chatHistory); | |
</cfscript> | |
<cffunction name="init" access="public" returntype="chat"> | |
<cfreturn this/> | |
</cffunction> | |
<cffunction name="postMessage" access="public" returntype="struct"> | |
<cfargument name="message" type="string" required="yes"/> | |
<cfargument name="userName" type="string" required="yes"/> | |
<cflock name="lockForWritingMessages" type="exclusive" timeout="5"> | |
<cfscript> | |
var result = structNew(); | |
var tempData = structNew(); | |
var utils = new com.kisdigital.utils(); | |
var msgTS = '<span style="color: blue;">[' & '#dateFormat(now(), "m/d/yy")#' & ' ' & '#timeFormat(now(), "h:mm tt")#' & '] </span> '; | |
var t = arrayNew(1); | |
var res = 0; | |
session.userName = arguments.userName; | |
result['svrStatus'] = "0"; | |
result['svrMessage'] = "OK"; | |
tempData['timeStamp'] = msgTs; | |
tempData['userid'] = utils.stripHTML(arguments.userName); | |
tempData['message'] = utils.stripHTML(arguments.message); | |
tempData['msgid'] = instance.messageTotal + 1; | |
instance.messageTotal++; | |
arrayAppend(instance.chatHistory, tempData); | |
if(arrayLen(instance.chatHistory) gt 100) arrayDeleteAt(instance.chatHistory, 1); | |
if(findNoCase("***", arguments.userName)){ | |
t = arguments.message.split(" "); | |
this.manageClients(t[1]); | |
} | |
else{ | |
this.manageClients(arguments.userName); | |
} | |
</cfscript> | |
</cflock> | |
<cfreturn result/> | |
</cffunction> | |
<cffunction name="getMessages" access="public" returntype="struct"> | |
<cfargument name="startID" type="numeric" required="no" default="0"/> | |
<cfscript> | |
var result = structNew(); | |
var i = 0; | |
var startIdx = 0; | |
var msgCnt = arrayLen(instance.chatHistory); | |
result['svrStatus'] = "0"; | |
result['svrMessage'] = "OK"; | |
result['lastid'] = instance.messageTotal; | |
if(arguments.startID eq 0){ | |
result['messages'] = instance.chatHistory; | |
} | |
else{ | |
result['messages'] = arrayNew(1); | |
for(i = 1; i lte msgCnt; i++){ | |
if(instance.chatHistory[i].msgid gt arguments.startID) | |
arrayAppend(result['messages'], instance.chatHistory[i]); | |
} | |
} | |
manageClients(session.userName); | |
return result; | |
</cfscript> | |
</cffunction> | |
<cffunction name="manageClients" access="private" returntype="boolean"> | |
<cfargument name="userName" type="string" required="true"/> | |
<cflock name="lockForManagingClients" type="exclusive" timeout="5"> | |
<cfscript> | |
var i = 0; | |
var found = 0; | |
var tmpStruct = structNew(); | |
if(len(trim(arguments.userName)) and not findNoCase("***", arguments.userName)){ | |
for(i=1; i lte arrayLen(instance.clients); i++){ | |
if(instance.clients[i].userName eq arguments.userName) found = 1; | |
} | |
if(found){ | |
instance.clients[found]['lastUpdated'] = now(); | |
} | |
else{ | |
tmpStruct['userName'] = arguments.userName; | |
tmpStruct['lastUpdated'] = now(); | |
arrayAppend(instance.clients, tmpStruct); | |
} | |
} | |
</cfscript> | |
</cflock> | |
<cfreturn true/> | |
</cffunction> | |
<cffunction name="pruneClients" access="private" returnType="void"> | |
<cflock name="lockForPruningClientList" type="exclusive" timeout="5"> | |
<cfscript> | |
var i = 0; | |
// Cleanup: | |
for(i = arrayLen(instance.clients); i gte 1; i--){ | |
if(abs(dateDiff("n", now(), instance.clients[i].lastUpdated)) gt 1){ | |
postMessage(instance.clients[i].userName & ' removed from chat for inactivity.', '*** '); | |
arrayDeleteAt(instance.clients, i); | |
} | |
} | |
</cfscript> | |
</cflock> | |
</cffunction> | |
<cffunction name="getClients" access="public" returnType="struct"> | |
<cfscript> | |
var result = structNew(); | |
pruneClients(); | |
result['svrStatus'] = "0"; | |
result['svrMessage'] = "OK"; | |
result['userInfo'] = instance.clients; | |
return result; | |
</cfscript> | |
</cffunction> | |
</cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment