2
* Copyright (c) 2005-2008, The haXe Project Contributors
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* - Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* - Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
* DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
17
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
1
25
package haxe.remoting;
3
class FlashJsConnection extends haxe.remoting.AsyncConnection {
7
public override function __resolve( field : String ) : AsyncConnection {
27
class FlashJsConnection #if flash implements AsyncConnection, implements Dynamic<AsyncConnection> #end {
31
var __path : Array<String>;
36
error : Dynamic -> Void,
37
queue : haxe.TimerQueue,
40
function new( data, path ) {
45
public function close() {
46
connections.remove(__data.name);
49
public function resolve( name ) : AsyncConnection {
8
50
var c = new FlashJsConnection(__data,__path.copy());
14
override public function call( params : Array<Dynamic>, ?onData : Dynamic -> Void ) {
15
var p = __path.copy();
17
var path = p.join(".");
55
public function setErrorHandler(h) {
59
public function call( params : Array<Dynamic>, ?onResult : Dynamic -> Void ) {
18
60
var s = new haxe.Serializer();
19
61
s.serialize(params);
20
var cnx : { private function escapeString(s : String) : String; } = haxe.remoting.Connection;
21
var params = cnx.escapeString(s.toString());
62
var params = escapeString(s.toString());
63
var error = __data.error;
23
haxe.Timer.queue(function() {
24
var s = flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection.flashCall",me.__data,path,f,params);
65
__data.queue.add(function() {
66
var data = flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection.flashCall",me.__data.id,me.__data.name,me.__path.join("."),params);
28
throw "Failed to call JS method "+path;
29
v = { r : new haxe.Unserializer(s).unserialize() };
70
throw "Call failure : FlashJsConnection is not compiled in JS";
71
v = new haxe.Unserializer(data).unserialize();
30
72
} catch( e : Dynamic ) {
76
if( onResult != null )
38
public static function flashConnect( objId : String ) : AsyncConnection {
81
static var connections = new Hash<FlashJsConnection>();
83
static function escapeString( s : String ) {
85
return s.split("\\").join("\\\\");
87
return s.split("\\").join("\\\\").split("&").join("&");
91
static function doCall( name : String, path : String, params : String ) : String {
93
var cnx = connections.get(name);
94
if( cnx == null ) throw "Unknown connection : "+name;
95
if( cnx.__data.ctx == null ) throw "No context shared for the connection "+name;
96
var params = new haxe.Unserializer(params).unserialize();
97
var ret = cnx.__data.ctx.call(path.split("."),params);
98
var s = new haxe.Serializer();
100
return escapeString(s.toString());
101
} catch( e : Dynamic ) {
102
var s = new haxe.Serializer();
103
s.serializeException(e);
111
public static function connect( name : String, objId : String, ?ctx : Context ) {
39
112
if( !flash.external.ExternalInterface.available )
40
113
throw "External Interface not available";
41
if( flash.external.ExternalInterface.call("haxe.remoting.FlashJsConnection"+".jsRemoting") != "yes" )
42
throw "haxe.remoting.FlashJsConnection"+" is not available in JavaScript";
43
return new FlashJsConnection(objId,[]);
48
static function jsRemoting() {
52
static function flashCall( flashObj : String, path : String, f : String, params : String ) : String {
115
try flash.external.ExternalInterface.addCallback("flashJsRemotingCall",doCall) catch( e : Dynamic ) {};
117
flash.external.ExternalInterface.addCallback("flashJsRemotingCall",null,doCall);
119
var cnx = new FlashJsConnection({
123
error : function(e) throw e,
124
queue : new haxe.TimerQueue(),
126
connections.set(name,cnx);
132
static function flashCall( flashObj : String, name : String, path : String, params : String ) : String {
54
var cnx : { private var __data : Dynamic; } = haxe.remoting.Connection.flashConnect(flashObj);
55
return cnx.__data.remotingCall(path,f,params);
134
var fobj : Dynamic = untyped window.document[flashObj];
135
if( fobj == null ) fobj = untyped window.document.getElementById[flashObj];
136
if( fobj == null ) throw "Could not find flash object '"+flashObj+"'";
138
try data = fobj.flashJsRemotingCall(name,path,params) catch( e : Dynamic ) {};
139
if( data == null ) throw "Flash object "+flashObj+" does not have an active FlashJsConnection";
56
141
} catch( e : Dynamic ) {
57
142
var s = new haxe.Serializer();
58
143
s.serializeException(e);