LocalConnection

From Directorforum Collaboration Wiki

Jump to: navigation, search

Local Connections between Flash and Director

Contents

Flash


Shockwave


Description

This example shows a LocalConnection between a Flash movie and a Shockwave movie. This functionality can be useful to exchange data between Director/Shockwave movies and/or Flash movies.

Actionscript Code

// step 0: initialize the chat output and input fields
chatOutput.text = "Initializing LocalConnection object...";
chatInput.text = "";
gMyName = "lbxExt";
gOtherName = "lbxDir";
 
// step 1: create the LocalConnection object
gLocalConn = new LocalConnection();
 
// step 2: declare the allowDomain and onStatus
//         callback functions
gLocalConn.allowDomain = function (aSendingDomain) {
	// for our demo purposes always return true
	return true;
}
gLocalConn.onStatus = function (aInfoObject) {
	// check for an error in the outgoing message
	if (aInfoObject.level == "error") {
		// update the chat input with an error message
		chatInput.text = "< onStatus:error >";
	} else {
		// append the outgoing message to the chat output display
		chatOutput.text += newline + gMyName + " : " + chatInput.text;
		// clear the chat input text
		chatInput.text = "";
	}
}
 
// step 3: declare a custom callback function
gLocalConn.incomingMessage = function (aMessage) {
	// append the incoming message to the chat output display
	chatOutput.text += newline + aMessage;
}
 
// update the chat output field
chatOutput.text += newline + "Connecting as " + gMyName + " ..." + newline + "----------";
 
// step 4: connect the LocalConnection object
var tConnResult = gLocalConn.connect(gMyName);
 
// check for connection failure
if (tConnResult == false) {
 
	// clear the reference and step back to the previous frame
	gLocalConn = undefined;
	gotoAndStop(1);
 
}
 
 
// respond to a send button press
sendButton.onPress = function() {
	// compose the outgoing message
	var tMessage = gMyName + " : " + chatInput.text;
	// step 5: send a message
	var tSendResult = gLocalConn.send(gOtherName,"incomingMessage",tMessage);
}
 
// respond to a disconnect button press
disconnectButton.onPress = function () {
	// step 6: close the LocalConnection object
	gLocalConn.close();
	gLocalConn = undefined;
	// clear the chat output and input fields
	chatOutput.text = "";
	chatInput.text = "";
	// step back to the first frame
	gotoAndStop(1);
}
 
// stop and hold on this frame
stop();

Lingo Code

-- MOVIESCRIPT
global gMyID, gExtID
 
on startMovie
  -- set internal and external Connection-IDs
  gMyID = "lbxDir"
  gExtID = "lbxExt"
end
 
on stopMovie
  -- clear any remaining global Flash objects
  clearAsObjects()
end
-- FRAME SCRIPT
global gLocalConn
global gMyID
global gExtID
 
on beginSprite me
  -- step 0: initialize the chat output and input fields
  member("chat output").text = "Initializing LocalConnection object..."
  member("chat input").text = ""
  -- step 1: create the LocalConnection object
  --         using a global Flash object
  gLocalConn = newObject("LocalConnection")
  -- step 2: declare the allowDomain and onStatus
  --         callback functions
  setCallback(gLocalConn,"allowDomain",#allowDomain,me)
  setCallback(gLocalConn,"onStatus",#onStatus,me)
  -- step 3: declare a custom callback function
  setCallback(gLocalConn,"incomingMessage",#incomingMessage,me)
  -- update the chat output field
  member("chat output").text = member("chat output").text & RETURN & "Connecting as " & gMyID & " ..." & RETURN & "----------"
  -- step 4: connect the LocalConnection object
  tConnResult = gLocalConn.connect(gMyID)
  -- check for connection failure
  if not(tConnResult) then
    -- clear the reference (we will navigate back to the first frame on exitFrame)
    gLocalConn = VOID
  end if
end
 
on exitFrame
  -- verify whether gLocalConn is void or not
  if voidP(gLocalConn) then 
    -- step back to the previous frame
    go to the frame - 1
  else
    -- hold on the current frame
    go to the frame
  end if
end
 
on allowDomain (me, aLocalConnObj, aDomain)
  -- for our demo purposes always return true
  return TRUE
end
 
on disconnect (me)
  -- step 6: close the LocalConnection object
  gLocalConn.close()
  gLocalConn = VOID
  -- clear the chat output and input fields
  member("chat output").text = ""
  member("chat input").text = ""
  -- step back to the first frame
  go to the frame - 1
end
 
on incomingMessage (me, aLocalConnObj, aMessage)
  -- append the incoming message to the chat output display
  member("chat output").text = member("chat output").text & RETURN & aMessage
end
 
on sendMessage (me)
  -- compose the outgoing message
  tMessage = gMyID & " : " & member("chat input").text
  -- step 5: send a message
  tSendResult = gLocalConn.send(gExtID,"incomingMessage",tMessage)
end
 
on onStatus (me, aLocalConnObj, aInfoObject)
  --  check for an error in the outgoing message
  if (aInfoObject.level = "error") then
    -- update the chat input with an error message
    member("chat input").text = "< onStatus:error >"
  else
    -- append the outgoing message to the chat output display
    member("chat output").text = member("chat output").text & RETURN & gMyID & " : " & member("chat input").text
    -- clear the chat input text
    member("chat input").text = ""
  end if
end
-- BUTTON SCRIPT (both buttons)
property pType
 
on mouseUp (me)
  -- send a sprite event using the button's type
  sendAllSprites(pType)
end mouseUp
 
on getPropertyDescriptionList (me)
  -- build and return the property initialization list
  tProps = [:]
  tProps.addProp(#pType, \
    [ \
       #comment: "Button type:", \
       #format: #symbol, \
       #range: [#sendMessage, #disconnect], \
       #default: #sendMessage \
    ] \
  )
  return tProps
end getPropertyDescriptionList
Personal tools