网络功能

BK.Socket

通过Socket发送/接收消息:

  1. var socket = new BK.Socket(0);
  2. var connect = socket.connect('127.0.0.1', 9527);
  3. var update = socket.update();
  4. // current socket is readable andr writable
  5. if(update == 3) {
  6. var buff = new BK.Buffer(4);
  7. buff.writeUint32Buffer(1234);
  8. socket.send(buff); //how to send
  9. //buff = socket.recv(4); //how to recv
  10. }
**示例代码:script/demo/net/socket_demo.js** ## BK.WebSocket 使用WebSocket:
  1. var ws = new BK.WebSocket('wss://echo.websocket.org');
  2. ws.onOpen = function(ws) {
  3. ws.send('hello');
  4. }
  5. ws.onClose = function(ws) {
  6. BK.Script.log(1, 0, 'onClose');
  7. }
  8. ws.onError = function(ws) {
  9. BK.Script.log(1, 0, 'onError');
  10. }
  11. ws.onMessage = function(ws, data) {
  12. BK.Script.log(1, 0, 'onMessage:'+data.data.readAsString());
  13. }
  14. ws.connect();
**示例代码:script/demo/net/websocket_demo.js** ## BK.HttpUtil 从http下载一张图png图,并且将其作为纹理,最终绘制到精灵上:
  1. function onResponseImage (res,code)
  2. {
  3. var resPicTexture = BK.Texture.createTextureWithBuffer(res);
  4. var icon =new BK.Sprite(279,132,resPicTexture,0,1,1,1);
  5. //居屏幕中央
  6. icon.position = {x:BK.Director.screenPixelSize.width/2.0,y:BK.Director.screenPixelSize.height/2.0}
  7. icon.anchor = {x:0.5,y:0.5};
  8. BK.Director.root.addChild(icon);
  9. }
  10. var httpimagreq = new BK.HttpUtil("https://sqimg.qq.com/qq_product_operations/im/qqlogo/imlogo_b.png");
  11. httpimagreq.setHttpReferer("sqimg.qq.com");
  12. httpimagreq.setHttpMethod("get")
  13. httpimagreq.setHttpCookie("cookie1=value1; cookie2=value2; cookie3=value3;"); //设置cookies
  14. httpimagreq.setHttpHeader("key1","val1"); //设置header
  15. httpimagreq.setHttpHeader("key2","val2");
  16. httpimagreq.requestAsync(onResponseImage);
**示例代码:script/demo/net/http_demo.js** ## BK.MQQ.Webview 使用 Webview 显示网页:
  1. 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