Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flash multipart/x-mixed-replace ActionScript3和Google Chrome(以及其他)_Flash_Actionscript 3_Google Chrome_Twisted_Xmlhttprequest - Fatal编程技术网

Flash multipart/x-mixed-replace ActionScript3和Google Chrome(以及其他)

Flash multipart/x-mixed-replace ActionScript3和Google Chrome(以及其他),flash,actionscript-3,google-chrome,twisted,xmlhttprequest,Flash,Actionscript 3,Google Chrome,Twisted,Xmlhttprequest,我有一个奇怪的问题,我正在开发一个蓝牙摄像头,我们想为世界提供一个mjpeg接口 Mjpeg只是一个http服务器,在连接保持打开的情况下一个接一个地回复jpeg。我的服务器正在为我提供: HTTP/1.1 200 OK Transfer-Encoding: chunked Cache-Directive: no-cache Expires: 0 Pragma-Directive: no-cache Server: TwistedWeb/10.0.0 Connection: Keep-Alive

我有一个奇怪的问题,我正在开发一个蓝牙摄像头,我们想为世界提供一个mjpeg接口

Mjpeg只是一个http服务器,在连接保持打开的情况下一个接一个地回复jpeg。我的服务器正在为我提供:

HTTP/1.1 200 OK Transfer-Encoding: chunked Cache-Directive: no-cache Expires: 0 Pragma-Directive: no-cache Server: TwistedWeb/10.0.0 Connection: Keep-Alive Pragma: no-cache Cache-Control: no-cache, no-store, must-revalidate; Date: Sat, 26 Feb 2011 20:29:56 GMT Content-Type: multipart/x-mixed-replace; boundary=myBOUNDARY HTTP/1.1 200 OK Transfer-Encoding: chunked Cache-Directive: no-cache Expires: 0 Pragma-Directive: no-cache Server: TwistedWeb/10.0.0 Connection: Keep-Alive Pragma: no-cache Cache-Control: no-cache, no-store, must-revalidate; Cate: Sat, 26 Feb 2011 20:29:56 GMT Content-Type: multipart/x-mixed-replace; boundary=myBOUNDARY HTTP/1.1200ok 传输编码:分块 缓存指令:无缓存 过期日期:0 Pragma指令:无缓存 服务器:TwistedWeb/10.0.0 连接:保持活力 Pragma:没有缓存 缓存控制:无缓存,无存储,必须重新验证; 日期:2011年2月26日星期六20:29:56 GMT 内容类型:multipart/x-mixed-replace;边界=我的边界 HTTP/1.1200ok 传输编码:分块 缓存指令:无缓存 过期日期:0 Pragma指令:无缓存 服务器:TwistedWeb/10.0.0 连接:保持活力 Pragma:没有缓存 缓存控制:无缓存,无存储,必须重新验证; 美食:2011年2月26日星期六20:29:56 GMT 内容类型:multipart/x-mixed-replace;边界=我的边界 然后针对每个帧:

--myBOUNDARY Content-Type: image/jpeg Content-Size: 25992 BINARY JPEG CONTENT..... (new line) --我的边界 内容类型:图像/jpeg 内容大小:25992 二进制JPEG内容。。。。。 (新行) 我为它制作了一个Flash客户端,所以我们可以在任何设备上使用相同的代码,服务器是用twisted用Python实现的,目标是Android等等,Android的问题是Google忘记了包含mjpeg支持。。。。此客户端正在使用URLStream

代码如下:

package net.aircable { import flash.errors.*; import flash.events.*; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLRequestHeader; import flash.net.URLStream; import flash.utils.ByteArray; import flash.utils.Dictionary; import flash.system.Security; import mx.utils.Base64Encoder; import flash.external.ExternalInterface; import net.aircable.XHRMultipartEvent; public class XHRMultipart extends EventDispatcher{ private function trc(what: String): void{ //ExternalInterface.call("console.log", what); //for android trace(what); } private var uri: String; private var username: String; private var password: String; private var stream: URLStream; private var buffer: ByteArray; private var pending: int; private var flag: Boolean; private var type: String; private var browser: String; private function connect(): void { stream = new URLStream(); trc("connect") var request:URLRequest = new URLRequest(uri); request.method = URLRequestMethod.POST; request.contentType = "multipart/x-mixed-replace"; trc(request.contentType) /* request.requestHeaders = new Array( new URLRequestHeader("Content-type", "multipart/x-mixed-replace"), new URLRequestHeader("connection", "keep-alive"), new URLRequestHeader("keep-alive", "115")); */ trace(request.requestHeaders); trc("request.requestHeaders") configureListeners(); try { trc("connecting"); stream.load(request); trc("connected") } catch (error:Error){ trc("Unable to load requested resource"); } this.pending = 0; this.flag = false; this.buffer = new ByteArray(); } public function XHRMultipart(uri: String = null, username: String = null, password: String = null){ trc("XHRMultipart()"); var v : String = ExternalInterface.call("function(){return navigator.appVersion+'-'+navigator.appName;}"); trc(v); v=v.toLowerCase(); if (v.indexOf("chrome") > -1){ browser="chrome"; } else if (v.indexOf("safari") > -1){ browser="safari"; } else { browser=null; } trc(browser); if (uri == null) uri = "../stream?ohhworldIhatethecrap.mjpeg"; this.uri = uri; connect(); } private function configureListeners(): void{ stream.addEventListener(Event.COMPLETE, completeHandler, false, 0, true); stream.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true); stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true); stream.addEventListener(Event.OPEN, openHandler, false, 0, true); stream.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true); stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true); } private function propagatePart(out: ByteArray, type: String): void{ trc("found " + out.length + " mime: " + type); dispatchEvent(new XHRMultipartEvent(XHRMultipartEvent.GOT_DATA, true, false, out)); } private function readLine(): String { var out: String = ""; var temp: String; while (true){ if (stream.bytesAvailable == 0) break; temp = stream.readUTFBytes(1); if (temp == "\n") break; out+=temp; } return out; } private function extractHeader(): void { var line: String; var headers: Object = {}; var head: Array; while ( (line=readLine()) != "" ){ if ( stream.bytesAvailable == 0) return; if (line.indexOf('--') > -1){ continue; } head = line.split(":"); if (head.length==2){ headers[head[0].toLowerCase()]=head[1]; } } pending=int(headers["content-size"]); type = headers["content-type"]; if ( pending > 0 && type != null) flag = true; trc("pending: " + pending + " type: " + type); } private function firefoxExtract(): void { trc("firefoxPrepareToExtract"); if (stream.bytesAvailable == 0){ trc("No more bytes, aborting") return; } while ( flag == false ) { if (stream.bytesAvailable == 0){ trc("No more bytes, aborting - can't extract headers"); return; } extractHeader() } trc("so far have: " + stream.bytesAvailable); trc("we need: " + pending); if (stream.bytesAvailable =0; x-=1){ buffer.position=x; buffer.readBytes(temp, 0, 2); // check if we found end marker if (temp[0]==0xff && temp[1]==0xd9){ end=x; break; } } trc("findImageInBuffer, start: " + start + " end: " + end); if (start >-1 && end > -1){ var output: ByteArray = new ByteArray(); buffer.position=start; buffer.readBytes(output, 0 , end-start); propagatePart(output, type); buffer.position=0; // drop everything buffer.length=0; } } private function safariExtract(): void { trc("safariExtract()"); stream.readBytes(buffer, buffer.length); findImageInBuffer(); } private function chromeExtract(): void { trc("chromeExtract()"); stream.readBytes(buffer, buffer.length); findImageInBuffer(); } private function extractImage(): void { trc("extractImage"); if (browser == null){ firefoxExtract(); } else if (browser == "safari"){ safariExtract(); } else if (browser == "chrome"){ chromeExtract(); } } private function isCompressed():Boolean { return (stream.readUTFBytes(3) == ZLIB_CODE); } private function completeHandler(event:Event):void { trc("completeHandler: " + event); //extractImage(); //connect(); } private function openHandler(event:Event):void { trc("openHandler: " + event); } private function progressHandler(event:ProgressEvent):void { trc("progressHandler: " + event) trc("available: " + stream.bytesAvailable); extractImage(); if (event.type == ProgressEvent.PROGRESS) if (event.bytesLoaded > 1048576) { //1*1024*1024 bytes = 1MB trc("transfered " + event.bytesLoaded +" closing") stream.close(); connect(); } } private function securityErrorHandler(event:SecurityErrorEvent):void { trc("securityErrorHandler: " + event); } private function httpStatusHandler(event:HTTPStatusEvent):void { trc("httpStatusHandler: " + event); trc("available: " + stream.bytesAvailable); extractImage(); //connect(); } private function ioErrorHandler(event:IOErrorEvent):void { trc("ioErrorHandler: " + event); } } }; 包网航空电缆{ 导入flash.errors.*; 导入flash.events.*; 导入flash.net.URLRequest; 导入flash.net.URLRequestMethod; 导入flash.net.URLRequestHeader; 导入flash.net.URLStream; 导入flash.utils.ByteArray; 导入flash.utils.Dictionary; 导入flash.system.Security; 导入mx.utils.base64编码器; 导入flash.external.ExternalInterface; 导入net.aircable.XHRMultipartEvent; 公共类XHRMultipart扩展了EventDispatcher{ 私有函数trc(what:String):void{ //ExternalInterface.call(“console.log”,what);//用于android 跟踪(什么); } 私有var-uri:String; 私有变量用户名:字符串; 私有var密码:字符串; 私有var流:URLStream; 专用var缓冲区:ByteArray; 私有var挂起:int; 私有变量标志:布尔值; 私有变量类型:字符串; 私有变量浏览器:字符串; 私有函数connect():void{ stream=新的URLStream(); trc(“连接”) var请求:URLRequest=新的URLRequest(uri); request.method=URLRequestMethod.POST; request.contentType=“multipart/x-mixed-replace”; trc(请求.内容类型) /*request.requestHeaders=新数组( 新的URLRequestHeader(“内容类型”,“多部分/x-mixed-replace”), 新的URLRequestHeader(“连接”,“保持活动”), 新的URLRequestHeader(“保持活动状态”、“115”); */ 跟踪(request.requestHeaders); trc(“request.requestHeaders”) 配置侦听器(); 试一试{ trc(“连接”); 流加载(请求); trc(“已连接”) }捕获(错误:错误){ trc(“无法加载请求的资源”); } 此参数为0; this.flag=false; this.buffer=new ByteArray(); } 公共函数XHRMultipart(uri:String=null, 用户名:String=null, 密码:String=null){ trc(“XHRMultipart()”); var v:String=ExternalInterface.call(“函数(){return navigator.appVersion+'-'+navigator.appName;}”); 真相与和解委员会(五); v=v.toLowerCase(); 如果(v.indexOf(“铬”)>-1){ browser=“chrome”; }否则如果(v.indexOf(“safari”)>-1){ browser=“safari”; } 否则{ 浏览器=空; } trc(浏览器); if(uri==null) uri=“../stream?ohhworldIhatethecrap.mjpeg”; this.uri=uri; connect(); } 私有函数configureListeners():void{ addEventListener(Event.COMPLETE、completeHandler、false、0、true); stream.addEventListener(HTTPStatusEvent.HTTP_STATUS,httpStatusHandler,false,0,true); addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler,false,0,true); addEventListener(Event.OPEN,openHandler,false,0,true); addEventListener(ProgressEvent.PROGRESS,progressHandler,false,0,true); stream.addEventListener(SecurityErrorEvent.SECURITY\u ERROR,securityErrorHandler,false,0,true); } 私有函数传播部分(out:ByteArray,类型:String):void{ trc(“发现”+out.length+“mime:+type”); dispatchEvent(新的XHRMultipartEvent(XHRMultipartEvent.GOT_数据,true、false、out)); } 私有函数readLine():字符串{ var out:String=“”; var-temp:字符串; while(true){ if(stream.bytesavable==0) 打破 温度=流读取字节(1); 如果(温度==“\n”) 打破 输出+=温度; } 返回; } 私有函数extractHeader():void{ var行:字符串; 变量头:对象={}; 变量头:数组; 而((line=readLine())!=“”){ if(stream.bytesavable==0) 返回; if(line.indexOf('--')>-1){ 继续; } head=行。拆分(“:”); 如果(头部长度==2){ headers[head[0].toLowerCase()]=head[1]; } } pending=int(标题[“内容大小]); 类型=标题[“内容类型”]; if(挂起>0&&type!=null) flag=true; trc(“挂起:+挂起+”类型:+类型); } 私有函数firefoxExtract():void{ trc(“firefoxPrepareToExtract”); if(stream.bytesavable==0){ trc(“不再有字节,正在中止”) 返回; } while(flag==false){ 如果(stream.bytesAvail) --myBOUNDARY Content-Type: image/jpeg Content-Size: 25992 XHRMultipart() 5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.114 Safari/534.16-Netscape chrome connect multipart/x-mixed-replace request.requestHeaders connecting connected openHandler: [Event type="open" bubbles=false cancelable=false eventPhase=2] openHandler: [Event type="open" bubbles=false cancelable=false eventPhase=2] progressHandler: [ProgressEvent type="progress" bubbles=false cancelable=false eventPhase=2 bytesLoaded=3680 bytesTotal=0] available: 3680 extractImage chromeExtract() findImageInBuffer, start: 0 end: -1 httpStatusHandler: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=200 responseURL=null] available: 0 extractImage chromeExtract() findImageInBuffer, start: 0 end: -1