BinaryHTTPService or how to help an HTTPService accept ByteArray data
-
- package mx.messaging.channels
- {
- publicclass DirectHTTPChannel extends Channel
- {
-
- .....
-
- override protectedfunction internalSend(msgResp:MessageResponder):void
- {
- var httpMsgResp:DirectHTTPMessageResponder = DirectHTTPMessageResponder(msgResp);
- var urlRequest:URLRequest;
-
- try
- {
- urlRequest = createURLRequest(httpMsgResp.message);
- }
- catch(e: MessageSerializationError)
- {
- httpMsgResp.agent.fault(e.fault, httpMsgResp.message);
- return;
- }
-
- var urlLoader:URLLoader = httpMsgResp.urlLoader;
- urlLoader.addEventListener(ErrorEvent.ERROR, httpMsgResp.errorHandler);
- urlLoader.addEventListener(IOErrorEvent.IO_ERROR, httpMsgResp.errorHandler);
- urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpMsgResp.securityErrorHandler);
- urlLoader.addEventListener(Event.COMPLETE, httpMsgResp.completeHandler);
- urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpMsgResp.httpStatusHandler);
- urlLoader.load(urlRequest);
- }
-
- .....
-
- }
- }
-
For URLLoader, by default, the dataFormat parameter is set to URLLoaderDataFormat.TEXT, and to receive data in the form of ByteArray, you need URLLoaderDataFormat.BINARY.
Everything is done very simply. The first thing we need is to create an extension for the DirectHTTPChannel class and set the urlLoader parameter dataFormat = URLLoaderDataFormat.BINARY;
-
- publicclass DirectHTTPBinaryChannel extends DirectHTTPChannel
- {
- publicfunction DirectHTTPBinaryChannel(id:String, uri:String="")
- {
- super(id, uri);
- }
-
- override protectedfunction getMessageResponder(agent:MessageAgent,
- message:IMessage):MessageResponder
- {
- returnnew DirectHTTPBinaryMessageResponder(agent, message, this, new URLLoader());
- }
-
- override protectedfunction internalSend(msgResp:MessageResponder):void
- {
- var httpMsgResp:DirectHTTPBinaryMessageResponder = DirectHTTPBinaryMessageResponder(msgResp);
- var urlRequest:URLRequest;
-
- try
- {
- urlRequest = createURLRequest(httpMsgResp.message);
- }
- catch(e: MessageSerializationError)
- {
- httpMsgResp.agent.fault(e.fault, httpMsgResp.message);
- return;
- }
-
- var urlLoader:URLLoader = httpMsgResp.urlLoader;
- urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
- urlLoader.addEventListener(ErrorEvent.ERROR, httpMsgResp.errorHandler);
- urlLoader.addEventListener(IOErrorEvent.IO_ERROR, httpMsgResp.errorHandler);
- urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpMsgResp.securityErrorHandler);
- urlLoader.addEventListener(Event.COMPLETE, httpMsgResp.completeHandler);
- urlLoader.load(urlRequest);
- }
- }
-
So that we can use our DirectHTTPBinaryChannel, we create an extension of the HTTPService class.
-
- publicclass BinaryHTTPService extends HTTPService
- {
- protected static var binaryChannel:Channel;
- protected static var binaryChannelSet:ChannelSet;
-
- publicfunction BinaryHTTPService(rootURL:String = null, destination:String = null)
- {
- super(rootURL, destination);
- }
-
- override publicfunction send(parameters:Object = null):AsyncToken
- {
- if (useProxy == false)
- {
- /* force the use of our binary channel */
- if (binaryChannelSet == null)
- {
- var dcs:ChannelSet = new ChannelSet();
- binaryChannel = new DirectHTTPBinaryChannel("direct_http_binary_channel");
- dcs.addChannel(binaryChannel);
- channelSet = dcs;
- binaryChannelSet = dcs;
- }
- elseif (channelSet != binaryChannelSet)
- {
- channelSet = binaryChannelSet;
- }
- }
- returnsuper.send(parameters);
- }
- }
-
Work Class Example: BinaryHTTPService DirectHTTPBinaryChannel