📄 privatechat.asc
字号:
try { var dummy = FCPrivateChat; } catch ( e ) { // #ifndef FCPrivateChat
load( "components/component.asc" );
/**
* Private Chat component class
*/
FCPrivateChat = function(name) {
this.init(name);
// Get a non persistent shared object for sending broadcasts
this.message_so = SharedObject.get( this.prefix + "message", false );
// If persist is true, then get the history back
this.history_so = SharedObject.get( this.prefix + "history", this.persist );
this.history = this.history_so.getProperty( "history" );
if ( this.history == null )
this.history = new Array;
// Get a non persistent shared object for the list of users
this.users_so = SharedObject.get( this.prefix + "users", false );
this.clientsByID = new Object();
}
// All named instances are held in instances
FCPrivateChat.prototype = new FCComponent("FCPrivateChat",FCPrivateChat);
FCPrivateChat.prototype.histlen = 99999999999999999999999; // Maximum history length
FCPrivateChat.prototype.persist = true; // Whether to save history
FCPrivateChat.prototype.allowClear = true; // Allow clients to clear history
FCPrivateChat.prototype.history = new Array; // History
FCPrivateChat.prototype.message_so = null; // Message broadcasts
FCPrivateChat.prototype.history_so = null; // History persistence
FCPrivateChat.prototype.users_so = null; // Users SharedObject
FCPrivateChat.prototype.badWords = ["fuck", "shit", "etc"];
///////////////////////////////////////////////////////////////////////////
// onXXX events that are called at appropriate times
// Methods that a client side component calls explicitly
FCPrivateChat.prototype.stripBadWords = function(str){
for(var i=0;i<this.badWords.length;i++){
str = str.split(this.badWords[i]).join(new Array(this.badWords[i].length+1).join("*"));
}
return str;
}
// This is called when a client disconnects
FCPrivateChat.prototype.onDisconnect = function( client ) {
this.close(client);
var clocal = this.getClientLocalStorage(client);
this.users_so.setProperty(clocal.id, null);
this.clientsByID[clocal.id] = null;
// Need to call this at the end since we are overriding
// the base onDisconnect
this.releaseLocalStorage(client);
}
// This is called when the application about to stop
FCPrivateChat.prototype.onAppStop = function() {
if ( this.persist && this.history_so != null ) {
this.history_so.setProperty( "history", this.history );
this.history_so.flush();
}
}
///////////////////////////////////////////////////////////////////////////
// Methods that a client side component calls explicitly
// The first method called by a client component
FCPrivateChat.prototype.connect = function( client ) {
var cglobal = this.getClientGlobalStorage(client);
var clocal = this.getClientLocalStorage(client);
if (!cglobal.usercolor) {
cglobal.usercolor = "0x000000";
}
client.call( this.callPrefix + "receiveHistory", null, this.history );
client.call( this.callPrefix + "setUsername", null, cglobal.username);
var now = new Date();
this.startTime = now.getTime();
var str = now.getHours() + ":";
var mins = now.getMinutes();
str += (mins>=10) ? mins : "0"+mins
this.statusMessage(client, str+", "+cglobal.username+" has entered the room");
clocal.id = "u" + this.getClientID(client);
this.clientsByID[clocal.id] = client;
this.users_so.setProperty(clocal.id, cglobal.username == null ? " fc_lurker" : cglobal.username);
}
// The last method called by a client component
FCPrivateChat.prototype.close = function( client ) {
var cglobal = this.getClientGlobalStorage(client);
var now = new Date();
var diff = Math.round((now.getTime()-this.startTime)/1000);
var formattedTime = this.formatTime(diff);
var str = now.getHours() + ":";
var mins = now.getMinutes();
str += (mins>=10) ? mins : "0"+mins
this.statusMessage(client, str+", "+cglobal.username + " ("+client.ip+") has left the room "+formattedTime);
}
FCPrivateChat.prototype.formatTime = function(totalSecs) {
//trace("formatTime:"+totalSecs);
var minutes = Math.floor(totalSecs / 60) % 60;
var seconds = totalSecs % 60;
var hours = Math.floor(totalSecs / 3600) % 3600;
var str = "";
if (totalSecs >= 3600) {
str += hours + " hour";
str += (hours != 1) ? "s, " : ", ";
}
if (totalSecs >= 60) {
str += minutes + " minute";
str += (minutes != 1) ? "s and " : " and ";
}
str += seconds + " second";
str += (seconds != 1) ? "s" : "";
return str;
}
FCPrivateChat.prototype.statusMessage = function( client, mesg ) {
var cglobal = this.getClientGlobalStorage(client);
//mesg = this.hiliteURLs(mesg);
mesg = "<font color=\"#ffcc00\"><i>" + mesg + "</i></font><br>\n";
this.history.push( mesg );
while ( this.history.length > this.histlen )
this.history.shift();
this.message_so.send( "message", mesg );
}
// send a message to all others participating in the chat session
FCPrivateChat.prototype.sendMessage = function( client, mesg, toWho ) {
var cglobal = this.getClientGlobalStorage(client);
if ((mesg == "")||(mesg == null))
return;
if (mesg == "/toofast") {
trace("toofast called!");
var allips = "<font color=\"#AAAAAA\"><b>Hey, Slow Down!!! You are typing too fast </b>";
//for (var i=0; i<application.clients.length; i++) {
//var name = this.getClientGlobalStorage(application.clients[i]).username;
//allips += "<br><b>"+name+"</b>: "+application.clients[i].ip;
//}
allips += "</font><br>\n";
client.call( this.callPrefix + "receiveMessage", null, allips);
return;
}
if (mesg == "/ips") {
trace("ips called!");
var allips = "<font color=\"#AAAAAA\"><b>People's IPs:</b>";
for (var i=0; i<application.clients.length; i++) {
var name = this.getClientGlobalStorage(application.clients[i]).username;
allips += "<br><b>"+name+"</b>: "+application.clients[i].ip;
}
allips += "</font><br>\n";
client.call( this.callPrefix + "receiveMessage", null, allips);
return;
}
if (mesg == "/clear") {
trace("clear called!");
this.clearHistory(null);
return;
}
if (mesg.substr(0,5) == "/kick") {
trace("kick called!");
ipToKick = mesg.substr(mesg.indexOf(" ")+1);
trace("ipToKick:"+ipToKick);
for (var i=0; i<application.clients.length; i++) {
if (application.clients[i].ip == ipToKick) {
trace("disconnecting "+application.clients[i]);
var name = this.getClientGlobalStorage(application.clients[i]).username;
this.message_so.send("message", "<font color=\"#AAAAAA\"><i>"+name+" ("+ipToKick+") was kicked</i></font>");
application.disconnect(application.clients[i]);
break;
}
}
return;
}
if (mesg == "/help") {
trace("help called!");
var allips = "<font color=\"#AAAAAA\"><b>Available Commands:</b>";
allips += "<br> /clear";
allips += "<br> /help";
allips += "<br> /ips";
allips += "<br> /kick ip";
allips += "</font><br>\n";
client.call( this.callPrefix + "receiveMessage", null, allips);
return;
}
//mesg = this.hiliteURLs(mesg);
var hexColor = "#"+cglobal.usercolor.substring(2, cglobal.usercolor.length);
if (toWho == "everyone") {
mesg = "<font color=\"" + hexColor + "\"><b>" + cglobal.username + ": </b>" + mesg + "</font>";
this.history.push( mesg );
while ( this.history.length > this.histlen )
this.history.shift();
this.message_so.send( "message", mesg );
} else {
//Private message
this.clientsByID[toWho].call( this.callPrefix + "receiveMessage", null, "<font color=\"#3333FF\"><b>"+cglobal.username+" {privately}: </b>"+mesg+"</font>");
}
}
// Clear the history
FCPrivateChat.prototype.clearHistory = function( client ) {
// If this is client request check if it is allowed
if ( client != null && !this.allowClear )
return false;
this.history_so.setProperty( "history", null );
this.history_so.flush();
delete this.history;
this.history = new Array;
// Broadcast a clearHistory command to all clients
this.message_so.send( "clearHistory" );
return true;
}
//Called by client
FCPrivateChat.prototype.changeName = function(client) {
var cglobal = this.getClientGlobalStorage(client);
var clocal = this.getClientLocalStorage(client);
this.users_so.setProperty(clocal.id, cglobal.username == null ? " fc_lurker" : cglobal.username);
};
///////////////////////////////////////////////////////////////////////////
// Helper and utility methods
// Save the history
FCPrivateChat.prototype.saveHistory = function() {
this.history_so.setProperty( "history", this.history );
this.history_so.flush();
}
// Hilight the urls in a message
FCPrivateChat.prototype.hiliteURLs = function(msg)
{
//+
//escape all <
//-
var escaped = "";
var ltPos = msg.indexOf("<");
while (ltPos != -1) {
escaped = msg.substring(0, ltPos) + "<" + msg.substring(ltPos+1,msg.length);
//trace ("escaped: "+escaped);
msg = escaped;
ltPos = msg.indexOf("<");
}
//+
//escape all >
//-
var escaped = "";
var ltPos = msg.indexOf(">");
while (ltPos != -1) {
escaped = msg.substring(0, ltPos) + ">" + msg.substring(ltPos+1,msg.length);
//trace ("escaped: "+escaped);
msg = escaped;
ltPos = msg.indexOf(">");
}
//+
//highlight urls
//-
var url_begin = msg.indexOf("http:");
if ( url_begin == -1 )
url_begin = msg.indexOf("www.");
if ( url_begin == -1 )
return msg;
var hilited = msg.substring(0, url_begin);
var url_end = msg.indexOf( " ", url_begin );
var urlstr = "";
if ( url_end == -1 )
urlstr = msg.substring(url_begin);
else
urlstr = msg.substring(url_begin, url_end);
var urlref = urlstr;
if ( urlstr.indexOf("www.") == 0 )
urlref = "http://" + urlstr;
var trailer = "";
if ( url_end != -1 )
trailer = hiliteURLs( msg.substring(url_end) );
hilited += "<font color=\"#0000FF\"><u><a href=\"" + urlref + "\" target=\"_blank\">" + urlstr + "</a></u></font>" + trailer;
//hilited += "<font color=\"#0000FF\"><u><a href=\"" + urlstr + "\">" + urlstr + "</a></u></font>" + trailer;
return hilited;
}
trace( "Chat loaded successfully." );
} // #endi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -