Accessibility

TechNote (Archived)

Detecting unintentional user disconnections in a Multiuser application

This TechNote is intended for intermediate to advanced developers of applications using the Multiuser Server and Xtra. It assumes a basic understanding of parent/child scripting.

For introductory information on the Multiuser Server and Xtra, please refer to Creating Multiuser Applications with Director.

How can a Multiuser application detect if a client has accidentally disconnected?
Developing an application using the Multiuser Server and Xtra in many cases requires that user connections, user disconnections and user crashes all be broadcast to other connected users. This can be accomplished with a "control movie" which is nothing more than one specialized Director movie with a unique login name. This control movie could reside as a projector on the same machine as the Multiuser Server, a different machine, or as a Shockwave movie in a browser on any machine.

The control movie would keep a list of user objects each with a property called "pConnectionStatus". At a predetermined interval a custom "ping" message would be sent to all users in the object list. Each object's "pConnectionStatus" value would be set to"#pinging".

A callback handler would need to be in place to respond to user messages with the subject "ping".

When a message with the subject "ping" arrives in the Xtra's messaging queue, the sender's object's "pConnectionStatus" property would be updated with the value,"#connected". If, after another interval, a user's "pConnectionStatus" still does not have the value"#connected", the control movie would ping again to verify, or simply delete the user object and send the updated information to all interested clients.

The following is one example of how to create a control movie which manages user connections:

1

In a new Director movie, create scripts which:

Instantiate the Multiuser Xtra.

Define callback handlers.

Connect to the server.

Instantiate user objects with the properties "pName" and"pConnectionStatus". These objects could be instantiated when a client sends an "InitPlayer" message to the control movie.

2

Create a callback handler which processes messages with the subject "ping". For example:

 global gAvatarList, gConnect on playerPingedCallbackHandler     newMessage = gConnect.getNetMessage()     errCode = newMessage.errorCode     if errCode <> 0 then ShowMessageStatus(errCode)      sender = newMessage.senderID          avatarNum = gAvatarList.count     repeat with i = 1 to avatarNum       if gAvatarList[i]<> 0 then         x = gAvatarList[i]         if x <> 0 then           if x.pName = sender then             x.pConnectionStatus = #connected           end if         end if       end if     end repeat end playerPingedCallbackHandler 
3

To set an interval in which to ping, use timeOut Scripts. The following could go in a "startMovie" handler:

 the timeOutScript = "pingUsers" the timeOutLength = 30 * 60 the timeOutKeyDown = FALSE the timeOutMouse = FALSE 
4

The actual "ping" handler could look something like the following:

 global gPingFlag, gAvatarList on pingUsers     --flag set so that the following two are alternated     if gPingFlag then           --go through avatarList and ping all players     avatarNum = gAvatarList.count     repeat with i = 1 to avatarNum       if gAvatarList[i]<> 0 then         x = gAvatarList[i]         --doubt first                 x.pConnectionStatus = #pinging          --then confirm when they respond to "ping"         SendMessage(x.pName, "Ping")        end if     end repeat     gPingFlag = 0   else     --go through avatarList and see if      --anyone is no longer connected     avatarNum = gAvatarList.count     repeat with i = 1 to avatarNum       if gAvatarList[i]<> 0 then         x = gAvatarList[i]         if x.pConnectionStatus <> #connected then deleteUser(i)       end if     end repeat     gPingFlag = 1   end if end pingUsers 
Note:The above snippets of code are meant to give a general, working model. Mileage may vary.



AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Creative Commons License

Search Support


Document Details

ID:tn_14120

Products Affected: