网络功能
BK.Socket
通过Socket发送/接收消息:
**示例代码:
var socket = new BK.Socket(0);
var connect = socket.connect('127.0.0.1', 9527);
var update = socket.update();
// current socket is readable andr writable
if(update == 3) {
var buff = new BK.Buffer(4);
buff.writeUint32Buffer(1234);
socket.send(buff); //how to send
//buff = socket.recv(4); //how to recv
}
script/demo/net/socket_demo.js
**
## BK.WebSocket
使用WebSocket:
var ws = new BK.WebSocket('wss://echo.websocket.org');
ws.onOpen = function(ws) {
ws.send('hello');
}
ws.onClose = function(ws) {
BK.Script.log(1, 0, 'onClose');
}
ws.onError = function(ws) {
BK.Script.log(1, 0, 'onError');
}
ws.onMessage = function(ws, data) {
BK.Script.log(1, 0, 'onMessage:'+data.data.readAsString());
}
ws.connect();
**示例代码:script/demo/net/websocket_demo.js
**
## BK.HttpUtil
从http下载一张图png图,并且将其作为纹理,最终绘制到精灵上:
function onResponseImage (res,code)
{
var resPicTexture = BK.Texture.createTextureWithBuffer(res);
var icon =new BK.Sprite(279,132,resPicTexture,0,1,1,1);
//居屏幕中央
icon.position = {x:BK.Director.screenPixelSize.width/2.0,y:BK.Director.screenPixelSize.height/2.0}
icon.anchor = {x:0.5,y:0.5};
BK.Director.root.addChild(icon);
}
var httpimagreq = new BK.HttpUtil("https://sqimg.qq.com/qq_product_operations/im/qqlogo/imlogo_b.png");
httpimagreq.setHttpReferer("sqimg.qq.com");
httpimagreq.setHttpMethod("get")
httpimagreq.setHttpCookie("cookie1=value1; cookie2=value2; cookie3=value3;"); //设置cookies
httpimagreq.setHttpHeader("key1","val1"); //设置header
httpimagreq.setHttpHeader("key2","val2");
httpimagreq.requestAsync(onResponseImage);
**示例代码:script/demo/net/http_demo.js
**
## BK.MQQ.Webview
使用 Webview 显示网页:
BK.MQQ.Webview.open("http://www.qq.com")
接口说明:BK.MQQ.Webview
示例代码:script/demo/mqq/mqq_demo.js
原文: https://hudong.qq.com/docs/engine/engine/native/func/funcs/network.html