[r255]: trunk / framework / OSMF / org / osmf / net / NetClient.as  Maximize  Restore  History

Download this file

234 lines (216 with data), 6.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*****************************************************
*
* Copyright 2009 Adobe Systems Incorporated. All Rights Reserved.
*
*****************************************************
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
*
* The Initial Developer of the Original Code is Adobe Systems Incorporated.
* Portions created by Adobe Systems Incorporated are Copyright (C) 2009 Adobe Systems
* Incorporated. All Rights Reserved.
*
*****************************************************/
package org.osmf.net
{
import flash.utils.Dictionary;
import flash.utils.Proxy;
import flash.utils.flash_proxy;
/**
* The NetClient class provides support for handling
* callbacks dynamically from an RTMP server that is streaming
* to a MediaElement that works with NetStream under the hood (such
* as VideoElement).
*
* <p>Use this class to listen for callbacks on the NetConnection
* and NetStream created by a NetLoader's load operation.</p>
* <p>Assign the value of the <code>client</code>
* property of the NetConnection or NetStream
* to an instance of the NetClient class.
* Then use the NetClient's <code>addHandler()</code>
* and <code>removeHandler()</code> methods to register and unregister handlers for
* the NetStream callbacks.</p>
*
* @see NetLoader
* @see flash.net.NetConnection
* @see flash.net.NetStream
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
dynamic public class NetClient extends Proxy
{
/**
* Adds a handler for the specified callback name.
*
* <p>If multiple handlers register for the same callback,
* the result of the callback is an array holding the results
* of each handler's invocation.
* </p>
* <p>
* This example sets up handler for the <code>onMetaData</code>
* callback.
* <listing>
* function onMetaData(value:Object):void
* {
* trace("Got metadata.");
* }
*
* var stream:NetStream;
* var client:NetClient = (stream.client as NetClient); //assign the stream to the NetClient
* client.addHandler("onMetaData", onMetaData); //add the handler
* </listing>
* </p>
*
* @param name Name of callback to handle.
* @param handler Handler to add.
* @priority The priority level of the handler. The higher the number, the higher the priority.
* All handlers with priority N are processed before handlers of priority N-1. If two or more
* handlers are added with the same priority, they are processed in the order in which they
* were added.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function addHandler(name:String, handler:Function, priority:int=0):void
{
var handlersForName:Array
= handlers.hasOwnProperty(name)
? handlers[name]
: (handlers[name] = []);
if (handlersForName.indexOf(handler) == -1)
{
var inserted:Boolean = false;
priority = Math.max(0, priority);
// Higher priority handlers are at the front of the list.
if (priority > 0)
{
for (var i:int = 0; i < handlersForName.length; i++)
{
var handlerWithPriority:Object = handlersForName[i];
// Stop iterating when we're passed all handlers of
// this priority.
if (handlerWithPriority.priority < priority)
{
handlersForName.splice(i, 0, {handler:handler, priority:priority});
inserted = true;
break;
}
}
}
if (!inserted)
{
handlersForName.push({handler:handler, priority:priority});
}
}
}
/**
* Removes a handler method for the specified callback name.
*
* @param name Name of callback for whose handler is being removed.
* @param handler Handler to remove.
*
* @langversion 3.0
* @playerversion Flash 10
* @playerversion AIR 1.5
* @productversion OSMF 1.0
*/
public function removeHandler(name:String,handler:Function):void
{
if (handlers.hasOwnProperty(name))
{
var handlersForName:Array = handlers[name];
for (var i:int = 0; i < handlersForName.length; i++)
{
var handlerWithPriority:Object = handlersForName[i];
if (handlerWithPriority.handler == handler)
{
handlersForName.splice(i, 1);
break;
}
}
}
}
// Proxy Overrides
//
/**
* @private
*/
override flash_proxy function callProperty(methodName:*, ... args):*
{
return invokeHandlers(methodName, args);
}
/**
* @private
*/
override flash_proxy function getProperty(name:*):*
{
var result:*;
if (handlers.hasOwnProperty(name))
{
result
= function():*
{
return invokeHandlers(arguments.callee.name, arguments);
}
result.name = name;
}
return result;
}
/**
* @private
*/
override flash_proxy function hasProperty(name:*):Boolean
{
return handlers.hasOwnProperty(name);
}
// Internals
//
/**
* @private
*
* Holds an array of handlers per callback name.
*/
private var handlers:Dictionary = new Dictionary();
/**
* @private
*
* Utility method that invokes the handlers for the specified
* callback name.
*
* @param name The callback name to invoke the handlers for.
* @param args The arguments to pass to the individual handlers on
* invoking them.
* @return <code>null</code> if no handlers have been added for the
* specified callback, or otherwise an array holding the result of
* each individual handler invokation.
*
*/
private function invokeHandlers(name:String, args:Array):*
{
var result:Array;
if (handlers.hasOwnProperty(name))
{
result = [];
var handlersForName:Array = handlers[name];
for each (var handler:Object in handlersForName)
{
result.push(handler.handler.apply(null,args));
}
}
return result;
}
}
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks