转自:http://flashteam.tencent.com/post/56/%e4%b8%baas3%e5%92%8cas2%e7%89%b5%e7%ba%bf%e6%90%ad%e6%a1%a5/
Actionscript2实现:
代码
/*
local connection bridge for as2
用法:
import com.tencent.net.Bridge2;
var bridge:Bridge2 = new Bridge2("bridgeKey", this);
bridge.invoke("sayHello", "Hello, I'm a American!");
function sayHello()
{
trace(arguments[1]+" said: "+arguments[0]);
};
*/
class com.tencent.net.Bridge2
{
public function Bridge2(id:String, listener:Object)
{
_lc = new LocalConnection();
_listener = listener;
_queue = [];
_lc.o = this;
_lc.findPair = findPair;
_lc.onInvoke = onInvoke;
// host mode
if (_lc.connect(BRIDGE_MODE_HOST+id))
{
_id = BRIDGE_MODE_HOST+id;
_pair = BRIDGE_MODE_CLIENT+id;
_mode = BRIDGE_MODE_HOST;
}
else // client mode
{
_id = BRIDGE_MODE_CLIENT+id;
_pair = BRIDGE_MODE_HOST+id;
_mode = BRIDGE_MODE_CLIENT;
_lc.connect(_id);
_lc.send(_pair, "findPair", _id);
}
}
// 查找主节点
public function findPair(pair:String):Void
{
if (!_id)
{
// 此处this为LocationConnection
this["o"].findPair.apply(this["o"], Array.prototype.slice.apply(arguments));
return;
}
if (_mode==BRIDGE_MODE_HOST)
_lc.send(_pair, "findPair", _id);
_connected = true;
// 执行并清除调用队列
while(doInvoke(_queue.shift()));
}
// 调用远程方法
public function invoke(/*...*/):Void
{
var args:Array = Array.prototype.slice.apply(arguments, [0]);
// 连接成功之前,将调用压入队列
if (!_connected) _queue.push(args);
else doInvoke(args);
}
// 接受远程调用
public function onInvoke(method:String/*, ...*/):Void
{
if (!_id)
{
// 此处this为LocationConnection
this["o"].onInvoke.apply(this["o"], Array.prototype.slice.apply(arguments));
return;
}
var args:Array = Array.prototype.slice.apply(arguments, [1]);
if (_listener && _listener[method])
_listener[method].apply(_listener, args);
}
// 执行调用操作
private function doInvoke(args/*, ...*/):Boolean
{
if (args==undefined) return false;
args.unshift(_pair, "onInvoke");
args.push(_id);
_lc.send.apply(_lc, args);
return true;
}
private var _id:String;
private var _connected:Boolean = false;
private var _pair:String;
private var _mode:Number;
private var _lc:LocalConnection;
private var _queue:Array;
private var _listener:Object;
static var BRIDGE_MODE_HOST:Number = 0;
static var BRIDGE_MODE_CLIENT:Number = 1;
}//end Bridge2
local connection bridge for as2
用法:
import com.tencent.net.Bridge2;
var bridge:Bridge2 = new Bridge2("bridgeKey", this);
bridge.invoke("sayHello", "Hello, I'm a American!");
function sayHello()
{
trace(arguments[1]+" said: "+arguments[0]);
};
*/
class com.tencent.net.Bridge2
{
public function Bridge2(id:String, listener:Object)
{
_lc = new LocalConnection();
_listener = listener;
_queue = [];
_lc.o = this;
_lc.findPair = findPair;
_lc.onInvoke = onInvoke;
// host mode
if (_lc.connect(BRIDGE_MODE_HOST+id))
{
_id = BRIDGE_MODE_HOST+id;
_pair = BRIDGE_MODE_CLIENT+id;
_mode = BRIDGE_MODE_HOST;
}
else // client mode
{
_id = BRIDGE_MODE_CLIENT+id;
_pair = BRIDGE_MODE_HOST+id;
_mode = BRIDGE_MODE_CLIENT;
_lc.connect(_id);
_lc.send(_pair, "findPair", _id);
}
}
// 查找主节点
public function findPair(pair:String):Void
{
if (!_id)
{
// 此处this为LocationConnection
this["o"].findPair.apply(this["o"], Array.prototype.slice.apply(arguments));
return;
}
if (_mode==BRIDGE_MODE_HOST)
_lc.send(_pair, "findPair", _id);
_connected = true;
// 执行并清除调用队列
while(doInvoke(_queue.shift()));
}
// 调用远程方法
public function invoke(/*...*/):Void
{
var args:Array = Array.prototype.slice.apply(arguments, [0]);
// 连接成功之前,将调用压入队列
if (!_connected) _queue.push(args);
else doInvoke(args);
}
// 接受远程调用
public function onInvoke(method:String/*, ...*/):Void
{
if (!_id)
{
// 此处this为LocationConnection
this["o"].onInvoke.apply(this["o"], Array.prototype.slice.apply(arguments));
return;
}
var args:Array = Array.prototype.slice.apply(arguments, [1]);
if (_listener && _listener[method])
_listener[method].apply(_listener, args);
}
// 执行调用操作
private function doInvoke(args/*, ...*/):Boolean
{
if (args==undefined) return false;
args.unshift(_pair, "onInvoke");
args.push(_id);
_lc.send.apply(_lc, args);
return true;
}
private var _id:String;
private var _connected:Boolean = false;
private var _pair:String;
private var _mode:Number;
private var _lc:LocalConnection;
private var _queue:Array;
private var _listener:Object;
static var BRIDGE_MODE_HOST:Number = 0;
static var BRIDGE_MODE_CLIENT:Number = 1;
}//end Bridge2
ActionScript 3实现
代码
/*
local connection bridge for as3
用法:
import com.tencent.net.Bridge3;
var bridge:Bridge3 = new Bridge3("bridgeKey", this);
bridge.invoke("sayHello", "Hello, I'm a Indians!");
function sayHello(... args)
{
trace(args[1]+" said: "+args[0]);
};
*/
package com.tencent.net
{
import flash.net.LocalConnection;
public class Bridge3
{
public function Bridge3(id:String, listener:Object)
{
_lc = new LocalConnection();
_lc.client = this;
_listener = listener;
try
{
// host mode
_lc.connect(BRIDGE_MODE_HOST+id);
_id = BRIDGE_MODE_HOST+id;
_pair = BRIDGE_MODE_CLIENT+id;
_mode = BRIDGE_MODE_HOST;
}
catch(err:Error)
{
// client mode
_mode = BRIDGE_MODE_CLIENT;
_id = BRIDGE_MODE_CLIENT+id;
_pair = BRIDGE_MODE_HOST+id;
_lc.connect(_id);
_lc.send(_pair, "findPair", _id);
}
}
// 查找主节点
public function findPair(pair:String):void
{
if (_mode==BRIDGE_MODE_HOST)
_lc.send(_pair, "findPair", _id);
_connected = true;
// 执行并清除调用队列
while(doInvoke(_queue.shift()));
}
// 调用远程方法
public function invoke(method:String, ...args):void
{
args.unshift(method);
// 连接成功之前,将调用压入队列
if (!_connected) _queue.push(args);
else doInvoke(args);
}
// 接受远程调用
public function onInvoke(method:String, ...args):void
{
if (_listener && _listener[method])
_listener[method].apply(_listener, args);
}
// 执行调用操作
private function doInvoke(args:Array):Boolean
{
if (args==null) return false;
args.unshift(_pair, "onInvoke");
args.push(_id);
_lc.send.apply(_lc, args);
return true;
}
private var _id:String;
private var _pair:String;
private var _connected:Boolean = false;
private var _mode:uint;
private var _lc:LocalConnection;
private var _queue:Array = [];
private var _listener:Object;
static var BRIDGE_MODE_HOST:uint = 0;
static var BRIDGE_MODE_CLIENT:uint = 1;
}
}
local connection bridge for as3
用法:
import com.tencent.net.Bridge3;
var bridge:Bridge3 = new Bridge3("bridgeKey", this);
bridge.invoke("sayHello", "Hello, I'm a Indians!");
function sayHello(... args)
{
trace(args[1]+" said: "+args[0]);
};
*/
package com.tencent.net
{
import flash.net.LocalConnection;
public class Bridge3
{
public function Bridge3(id:String, listener:Object)
{
_lc = new LocalConnection();
_lc.client = this;
_listener = listener;
try
{
// host mode
_lc.connect(BRIDGE_MODE_HOST+id);
_id = BRIDGE_MODE_HOST+id;
_pair = BRIDGE_MODE_CLIENT+id;
_mode = BRIDGE_MODE_HOST;
}
catch(err:Error)
{
// client mode
_mode = BRIDGE_MODE_CLIENT;
_id = BRIDGE_MODE_CLIENT+id;
_pair = BRIDGE_MODE_HOST+id;
_lc.connect(_id);
_lc.send(_pair, "findPair", _id);
}
}
// 查找主节点
public function findPair(pair:String):void
{
if (_mode==BRIDGE_MODE_HOST)
_lc.send(_pair, "findPair", _id);
_connected = true;
// 执行并清除调用队列
while(doInvoke(_queue.shift()));
}
// 调用远程方法
public function invoke(method:String, ...args):void
{
args.unshift(method);
// 连接成功之前,将调用压入队列
if (!_connected) _queue.push(args);
else doInvoke(args);
}
// 接受远程调用
public function onInvoke(method:String, ...args):void
{
if (_listener && _listener[method])
_listener[method].apply(_listener, args);
}
// 执行调用操作
private function doInvoke(args:Array):Boolean
{
if (args==null) return false;
args.unshift(_pair, "onInvoke");
args.push(_id);
_lc.send.apply(_lc, args);
return true;
}
private var _id:String;
private var _pair:String;
private var _connected:Boolean = false;
private var _mode:uint;
private var _lc:LocalConnection;
private var _queue:Array = [];
private var _listener:Object;
static var BRIDGE_MODE_HOST:uint = 0;
static var BRIDGE_MODE_CLIENT:uint = 1;
}
}