HELLO CHAT!

In previous lessons we learned how to execute a command in the server for create users, write and read fields in a register etc,

We learned that all is done using messages that go from the user to the server and come from the server to the user.

The Director MUS has a powerful resource: if, instead of a command I send the word: @AllUsers and instead of the parameters I send some string ("Hello world!", by example), this string will be broadcasted to all the movies/users that are connected to the server. This message will be received by the default handler (DefaultMessageHandler in our case) of received messages of the movies - if they have one.

To work with MUS and database you need to know very well how to work with: property lists an exclusive type of array of Lingo. because the messages use property lists.

In the message sended we have the properties:

In the message received we have the properties:

To create the possibility that a gamer sends an receives messages (chat) we will create a new button and glue to it the script:

global gConnect,avatarobj
on mouseUp me
  whichMsg = member("cent").text -- "cent" is the   chat entry field
  errCode = gConnect.sendNetMessage("@AllUsers", "chat", whichMsg)
  
  if errCode <> 0 then 
     member("cwin").text = "Error sending message: "gConnect.getNetErrorString(errCode) 
  end if
end

Only this :-)

Our default message handler (in the setupscript) needs to be changed to identify chat messages (with the subject chat) and to build a message to be placed in the chat window.

Look the new code:

on DefaultMessageHandler
  newMessage = gConnect.getNetMessage()
  errCode = newMessage.errorCode
  if errCode <> 0 then 
    member("cwin").text = "Error: "& errCode & " : " & gConnect.getNetErrorString(errCode)  
  else
    -- if we have a connection message
    if newMessage.senderID = "System" AND newMessage.subject = "ConnectToNetServer" then
      member("cwin").text = newMessage.subject & " : " & "Connection Successful"  
    else
    -- if we have a chat message
    if newMessage.subject = "chat" then
      member("cwin").text =  string(newMessage.senderID & ": "&   newMessage.content)
    else
      --receiving avatar transform?
      member("cwin").text = string(newMessage.content[1][1])    
      avatarobj.svect=   newMessage.content[1][1]  
      if(avatarobj.load=1) then
        myworld.model("avatar").transform = avatarobj.svect  
       avatarobj.load=0
      end if
    end if
    end if
  end if
end

Try the demo, using your server prepared like we have seen in previous lessons. Open two web pages using the URL of this page.Type a message in the chat entry field and press the SEND MESSAGE button. The message will go to the two movies! A chat!



PREVIOUS LESSON NEXT LESSON
T. CONTENTS HOMEPAGE