var url="https://test1.openerserver.com:10008/op"; // 安装了 opener_server 的服务器地址: test1.openerserver.com var start_http_server={'action':'new_http_server','ip':'','port':'1008'}; // 开启一个新的 http 服务器,监听在端口 1008 上 var reg_url={'action':'reg_url','type':'http_get','url':'/helloworld','host':'*:1008','go':hello_fun}; //注册一个 url 地址 /helloworld ,绑定到 1008 端口上,设定这个 url 处理模式为 http get 模式,最后处理这个 url 的代码放到 hello_fun var hello_fun=`my ($r,$key)=@_; $n->{send_normal_resp}->($r,$key,'Hello Worlds'); `; //### 接收传入参数,$r 包含所有这个 http 请求相关信息,$key 包含该 http 请求的唯一 id //### 发送返回,返回内容是 html ,加入一个 hello world 字符串。 var opener_flag='opener'; // 设定 http header 中 opener_flag 字段,相当于访问该 opener_server 的密码 url_post_data(url,JSON.stringify(start_http_server)); // ajax post 到 opener_server 服务器 url_post_data(url,JSON.stringify(reg_url)); // ajax post 到 opener_server 服务器 function url_post_data(go,data){ $.ajax({ url: go, cache: false, headers: { opener_flag:opener_flag }, data: data, type: 'POST', dataType: 'json', success: function(data){ if (data.result=='ok') { console.log(data); }else{ console.log('error'); } }, error: function(dd,mm){ console.log('error:'); console.log(dd); console.log(mm); } }); }
在一个含有 jquery 代码的 html 页面上,执行上面的代码就可以了。
然后访问 http://test1.openervpn.com:1008/helloworld 就可以看到结果。
我们已经执行了上面的代码,那么下面你可以更改上面这段代码,来实现你需要的 Hello Worlds 例如:更改要注册的 url 地址...