Http://blog.csdn.net/yc7369/article/details/35567105
Recently, mobile game customers have asked for a chat feature. In fact, I personally think this chat feature is quite weak. This part has almost been monopolized by tx. How can we deal with it, there are not many mobile game chats.
The main reason is very simple: the server load will increase a lot, the maintenance is more troublesome, and the cost is burning, and there will not be too many people using this function, there will also be various things under the control of the server.
The chat system can determine whether the chat content contains illegal characters.
Use smartfoxserver Server Extension, because SFS by default it is UTF-8 encoding, in addition to English sent characters are garbled, so I thought about the following method for your reference one day. If there is something wrong, please leave a message!
Try {
String [] keyword = {"", "fuck", "", "cao "};
String content = json. getString ("content"). trim ();
Pattern p = Pattern. compile ("\ s * | \ t | \ r | \ n"); // Regular Expression
Matcher m = p. matcher (content );
String after = m. replaceAll (""); // replace all the above expressions with an empty String.
After = MsgTool. gbkToutf8 (after); // convert all the characters used to the UTF-8 encoding so that no encoding problem occurs and cannot be compared
Boolean flag = false; // determines whether a blocked character is included.
For (int I = 0; I <keyword. length; I ++ ){
String kw = MsgTool. gbkToutf8 (keyword [I]);
If (after. indexOf (kw )! =-1) {// returns if any
Flag = true;
Break;
}
}
MsgJSONObject msj = MsgJSONObject. instance ();
If (flag ){
Log.info ("invalid characters are sent when a user is chatting. the user is:" + user. getName (); // the user is sent to the client to indicate that the user's chat content has invalid characters.
Msj. put ("cmd", "chatUnlawful ");
SendMessage (user, msj );
} Else {// send normally if no
Log.info ("user chat, user:" + user. getName ());
Log.info ("chat content:" + content );
Msj. put ("cmd", "chat ");
Msj. put ("msg", content );
SendMessage (getRoom (user), msj );
}
} Catch (Exception e ){
E. printStackTrace ();
Log. error ("reading chat content error ");
}
/*** Encoding conversion, turning Chinese into UTF-8
* @ Param chenese: Chinese Characters
* @ Return
*/
Public static String gbkToutf8 (String chinese ){
Char c [] = chinese. toCharArray ();
Byte [] fullByte = new byte [3 * c. length];
For (int I = 0; I <c. length; I ++ ){
Int m = (int) c [I];
String word = Integer. toBinaryString (m );
StringBuffer sb = new StringBuffer ();
Int len = 16-word. length ();
// Add zero
For (int j = 0; j <len; j ++ ){
Sb. append ("0 ");
}
Sb. append (word );
Sb. insert (0, "1110 ");
Sb. insert (8, "10 ");
Sb. insert (16, "10 ");
String s1 = sb. substring (0, 8 );
String s2 = sb. substring (8, 16 );
String s3 = sb. substring (16 );
Byte b0 = Integer. valueOf (s1, 2). byteValue ();
Byte b1 = Integer. valueOf (s2, 2). byteValue ();
Byte b2 = Integer. valueOf (s3, 2). byteValue ();
Byte [] bf = new byte [3];
Bf [0] = b0;
FullByte [I * 3] = bf [0];
Bf [1] = b1;
FullByte [I * 3 + 1] = bf [1];
Bf [2] = b2;
FullByte [I * 3 + 2] = bf [2];
}
Try {
String fullStr = new String (fullByte, "UTF-8 ");
Return fullStr;
} Catch (UnsupportedEncodingException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
Log. error ("conversion character encoding error ");
Return "";
}
}
Http://blog.csdn.net/yc7369/article/details/35567105