Sending private messages

A common use of buddy list is sending private messages to buddies. In some cases, we may be playing a game in virtual world and want to ask whether my friend wants to join it. We can open up the buddy list within the game and send a private message out. We are going to implement it by using the built-in private message feature.

We added a private message button in the info panel. A private message panel prompts out when we click on the button. After the player inputs some text and clicks on send, we construct the private message in a format to include the recipient's name. We need the recipient's name to display in the chat dialog panel. The chat message is composed in following format:

Sender|recipient|private message

The following code in PrivateMessagePanel.as sends out the private message. We need to check if the recipient is from current room or buddy list in order to get the recipient's user ID.

private function clickSendBtn(e:MouseEvent):void {
var targetBuddyId:int;
var targetBuddy:User = SmartFox.sfs.getActiveRoom().getUser (_targetBuddy);
if (targetBuddy != null) {
targetBuddyId = targetBuddy.getId();
} else {
targetBuddyId = SmartFox.sfs.getBuddyByName(_targetBuddy).id;
}
/* prepare the chat message */
var message:String = SmartFox.sfs.myUserName+"|"+_ targetBuddy+"|"+txtInputContent.text;
SmartFox.sfs.sendPrivateMessage(message,targetBuddyId);
parent.removeChild(this);
}

We will display the private message in chat box. The private message handler deserializes the message and gets the sender name, recipient name, and the content of message.

The reason we put the sender and recipient into the message is that the default private message will store the sender as null if the sender is not from current room of the user. We need to know who sends the message to us, so that we can include the sender's and recipient's name inside the message.

We listen to the onPrivateMessage event in ChatBox class and decode the private message to display it in the chat log.

private function onPrivateMessage(e:SFSEvent):void {
var msg:String = e.params.message;
var splitArray:Array = msg.split('|'),
var sender:String = splitArray[0];
var recipient:String = splitArray[1];
var message:String = splitArray[2];
if (sender == _sfs.myUserName){
txtChatLog.appendText("
You said to "+ recipient+": " + message);
} else {
txtChatLog.appendText("
"+sender+" said to you: " + message);
}
txtChatLog.scrollV = txtChatLog.maxScrollV;
}

Capturing private message in server-side with internal event

There are several internal events available in server-side extension. These internal events are useful for us to extend several internal behaviors such as sending public and private messages. Whenever the internal event dispatches, the extension can handle it via the handleInternalEvent function. We are going to save the messages on server-side when buddies are chatting with private messages.

By default, the internal event of the private messages is not created. In order to handle this internal event, we have to configure the server by using the setPrivMsgInternalEvent to turn on this event.

We can enable the private message internal event on server-side to extend the default private message feature. We will extend the private messages to log them in the database. Players can access all their private message logs so it will look like an e-mail system between players.

We create a table called chatlog in database with following schema:

Field name

Type

sender

varchar(50)

recipient

varchar(50)

message

text

logtime

datetime

In the server-side virtualworld.as extension, we need to enable the internal private message event in the init function.

_server.getCurrentZone().setPrivMsgInternalEvent(true);

We can then handle the private message via the handleInternalEvent function. After we save the message into database or do anything we want, we need to dispatch the private message manually. The SmartFoxServer will not send the private message to the recipient automatically once handled in extension.

function handleInternalEvent(e){
if (e.name == "userExit" || e.name == "userLost") {
var user = e.user;
saveItemToDatabase(user);
} else if (e.name == "privMsg") {
var sender = e.sender.getName();
var recipient = e.recipient.getName();
var msg = e.msg.split("<,>");
var message = msg[3];
var sql = "INSERT INTO chatlog(`sender`,`recipient`,`message`,`logtime`) VALUES ('"+sender+"','"+recipient+"','"+message+"',NOW())";
var success = dbase.executeCommand(sql);
_server.dispatchPrivateMessage(e.msg, e.room, e.sender, e.recipient);
}
}

The following graph shows the differences of handling the private messages on the server. When the private message internal event is turned off, the message is sent to the recipient directly through the server. With the private message internal event turned on, it is the extension's responsibility to dispatch the private message.

Capturing private message in server-side with internal event

With the internal event and the saved private messages, it is not far off to implement the offline message feature. We can load the saved message from the database when the player logs in and design a panel such as message inbox to show the unread messages. Playing with messages in a virtual world can be far more creative than this. We may also create a common chat room within friends. They will be able to comment on chat messages or status. This becomes a basic form of social networking. We are not going to implement these features here but thinking about a creative way to let players communicate with friends is good for selling the virtual world.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.163.171