Fork me on GitHub
一共有 61 篇文章,分页:10 / 13

nodejs请求HTTPS报错:UNABLE_TO_VERIFY_LEAF_SIGNATURE的解决

出现这个错误是因为对方网站的证书不正确导致的。

在请求的时候指定忽略证书验证,即options的rejectUnauthorized参数设置为false

var https = require('https'); 

var options = { 
  hostname: 'encrypted.google.com', 
  port: 443, 
  path: '/', 
  method: 'GET', 
  rejectUnauthorized:false 
}; 

var req = https.request(options, function(res) { 
  console.log("statusCode: ", res.statusCode); 
  console.log("headers: ", res.headers); 

  res.on('data', function(d) { 
    process.stdout.write(d); 
  }); 
}); 
req.end(); 

req.on('error', function(e) { 
  console.error(e); 
});

nodejs官方SDK关于该参数的说明:

rejectUnauthorized: If true, the server certificate is verified against the list of supplied CAs. An 'error'event is emitted if verification fails. Verification happens at the connection level, before the HTTP request is sent. Default true.

flash全屏模式不支持键盘输入的解决方案

在flash里面如果设置stage的displayState为StageDisplayState.FULL_SCREEN的话,会不支持键盘输入。只能按esc退出全屏模式。

其实在flashplayer 11.3版本以上支持全屏键盘输入了,只要设置displayState属性为StageDisplayState.FULL_SCREEN_INTERACTIVE就可以了。

前提是在html里面镶嵌swf之前需要对swf的params增加一个allowFullScreenInteractives属性为true

flash代码如下:

stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;

html代码片段:

<param name="allowScriptAccess" value="sameDomain" /> 
< param name="allowFullScreenInteractive" value="true" />

如果是使用swfobject则js代码片段:

params.quality = "high"; 
params.bgcolor = "#000000"; 
params.allowscriptaccess = "always"; 
params.allowfullscreen = "true"; 
params.allowFullScreenInteractive = "true"; 
params.wmode = "direct";

注意 还要设置需求flash版本为11.3以上,

Mini Ajax is a Lightweight Javascript AJAX library

来源:https://code.google.com/p/miniajax/

非常棒的一个迷你ajax库!

HOW TO USE:

ajax.x - The XMLHttpRequest object (or MS equivalent) used for communication

ajax.serialize(f) f = the form element you wish to be serialized This function serializes all the fields in a form so that they can be passed as a query string in the form 'arg1=val1&arg2=val2'.

ajax.get(url, func) url = the url to query (can contain arguments after a '?') func = the function to call once the response is returned This function uses a GET request to query the specified url and return a response to the specified function.

ajax.gets(url) url = the url to query (can contain arguments after a '?') This function uses a GET request to query the specified url and return a response synchronously. Use this sparingly, as synchronous calls can lock up the browser.

ajax.post(url, func, args) url = the url to query func = the function to call once the response is returned args = a string containing arguments to be passed to the url This function uses a POST request to query the specified url and return a response to the specified function.

ajax.update(url, elm) url = the url to query elm = the (name of the) element to update This function uses a GET request to query the specified url and insert the result into the specified element.

ajax.submit(url, elm, frm) url = the url to query elm = the (name of the) element to update frm = the form element to submit This function is typically used in the onsubmit handler of a function. The form is not submitted the usual way; the form is instead serialized using 'ajax.serialize' and submitted using 'ajax.post'. The result is then inserted into the specified element.

SOURCE CODE:

function $(e){if(typeof e=='string')e=document.getElementById(e);return e}; function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};

ajax={}; 
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}}; 
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');}; 
ajax.send=function(u,f,m,a){var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)}; 
ajax.get=function(url,func){ajax.send(url,func,'GET')}; 
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText}; 
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)}; 
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)}; 
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};

Object、Array、Vector遍历性能测试

talk is cheap,show me the code!

var obj:Object = { }; 
var arrVector:Vector.<int> = new Vector.<int>(); 
var arrArray:Array = []; 

var len:int; 
var i:int; 
var key:String; 
var val:int; 
var t1:int, t2:int, t3:int, t4:int, t5:int, t6:int, t7:int, t8:int, t9:int,t10:int; 
var t11:int, t12:int, t13:int, t14:int, t15:int, t16:int, t17:int, t18:int, t19:int; 

//初始化50万个元素 
t1 = getTimer(); 
for (i = 0; i < 500000; i++)  
{ 
    obj[i] = i; 
    arrVector.push(i); 
    arrArray.push(i); 
} 
//for遍历key 
t2 = getTimer(); 
for (key in obj) 
{ 
    val = obj[key]; 
} 
//for each 遍历value 
t3 = getTimer(); 
for each(val in obj) 
{ 
    val; 
} 
//for each 遍历Vector 
t4 = getTimer(); 
for each(val in arrVector) 
{ 
    val; 
} 
//for循环Vector,length属性放for里面 
t5 = getTimer(); 
for (i = 0; i < arrVector.length; i++)  
{ 
    val = arrVector[i]; 
} 
//for循环Vector,length属性放for外面 
t6 = getTimer(); 
len = arrVector.length; 
for (i = 0; i < len; i++)  
{ 
    val = arrVector[i]; 
} 
//for each遍历Vector 
t7 = getTimer(); 
for each(val in arrVector) 
{ 
    val; 
} 
//for循环Array,length属性放for里面 
t8 = getTimer(); 
for (i = 0; i < arrArray.length; i++)  
{ 
    val = arrArray[i]; 
} 
//for循环Array,length属性放for外面 
t9 = getTimer(); 
len = arrArray.length; 
for (i = 0; i < len; i++)  
{ 
    val = arrArray[i]; 
} 
t10 = getTimer(); 

trace("1:" + (t2-t1));//288 
trace("2:" + (t3-t2));//1974 
trace("3:" + (t4-t3));//67 
trace("4:" + (t5-t4));//50 
trace("5:" + (t6-t5));//44 
trace("6:" + (t7-t6));//42 
trace("7:" + (t8-t7));//52 
trace("8:" + (t9-t8));//70 
trace("9:" + (t10-t9));//46

可以看出for遍历object获得key,然后再用obj[key]获得value的方式性能是最低的!

用Vector,然后把length提前赋值出来,再for循环是最快的!

补充:根据这篇文章 //jacksondunstan.com/articles/2514 的测试结果,在使用for in循环时,不指定key的类型,比定义string类型的key,快5倍:

// 慢! 
for (var str:String in obj) 
{ 
} 

// 比上面那种方式快 5 倍! 
for (var key:* in obj) 
{ 
}

exbuffer.c——纯C写的TCP中的粘包、分包问题的解决方案

设计目标是一个纯C的网络协议缓冲器,该协议简单介绍:

协议包分为包头和包体:包长采用2个字节或者4个字节,用来表示本次数据包中包体的长度

接受到数据就存储在缓冲区,缓冲区动态扩展以保证可以足够存储。

当接收到一个以上完整的数据包就调用回调函数recvHandle。

项目托管页面:https://github.com/play175/exbuffer.c

另外有nodejs版本的exbuffer:https://github.com/play175/ExBuffer

对C不熟悉,第一次写C代码,可能很多不合理的地方,欢迎批评指正!

附使用代码:

#include "exbuffer.c" 

void recvHandle(unsigned char *rbuf,size_t len) 
{ 
    printf("收到数据:%d\n",len); 
    exbuffer_printHex(rbuf,len); 
} 

int main(int argc, char **argv) 
{ 
    exbuffer_t* value; 
    value = exbuffer_new(); 
    value->recvHandle = recvHandle; 

    unsigned char buf[] = {0,2,3,4,0,1,5,0}; 
    exbuffer_put(value,(unsigned char*)buf,0,8); 

    unsigned char buf2[] = {3}; 
    exbuffer_put(value,(unsigned char*)buf2,0,1); 

    unsigned char buf3[] = {6,6,6}; 
    exbuffer_put(value,(unsigned char*)buf3,0,3); 

    //printf("有效数据长度:%d\n",exbuffer_getLen(value)); 
    //printf("缓冲区长度:%d\n",value->bufferlen); 
    printf("缓冲区:\n"); 
    exbuffer_dump(value); 
    exbuffer_free(&value); 

    //getchar(); 
    return EXIT_SUCCESS; 
}