.js Timer to prevent chatflood?

Settin’ up a school interactive chatroom, and wanted to prevent flooding of the RPC, how do I create in javascript a timer, that functions as a cooldown on your chat, e.g. having at least 1½ seconds between posts at best?

It depends on how the chat works. If you have an authoriative server the check has to be implemented only on the server but it can’t be removed (or bypassed). If you have a “normal” network setup, so each client send the rpc to all others itself you can implement it on the sending or receiving part. The easiest way is in the sending part but that could be bypassed. The other way would be that each client holds a timer for each other peer and refuses the receiving when it’s too frequent.

The usual way is to use a simple float variable that stores the timestamp of the last message. You can simply check when the last one has been sent / received and refuse sending / receiving when it’s faster than the allowed time.


// UnityScript:
public var cooldown : float = 1.5
private var lastTimeStamp : float = 0.0;

function SendMessage(text : String)
    if(Time.time > lastTimeStamp + cooldown)
        networkView.RPC(/*Send your message here*/);
        lastTimeStamp = Time.time;