科尔多瓦chrome.socket API.有什么例子吗? [英] Cordova chrome.socket API. Any example?

查看:98
本文介绍了科尔多瓦chrome.socket API.有什么例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用org.chromium.socket插件.但是我找不到很多例子.这是我的代码:

I am trying to use org.chromium.socket plugin. But I cannot find many examples. Here is my code:

var connButton = document.getElementById("connButton");

connButton.addEventListener("click", doConnect, false);

function doConnect() {
    var theSocketId = 0;
    chrome.socket.create("tcp", null, function(createInfo) {
        alert(createInfo.socketId);
        theSocketId = createInfo.socketId;
     });
     chrome.socket.connect(theSocketId, "http://www.yahoo.com", 80, function(result) {
        alert(result);
     });
chrome.socket.read(theSocketId, 1000, function(readInfo) {
      alert(readInfo.resultCode);
  });
  }

我尝试将主机名更改为其他IP或更改端口.但是有时我会看到结果= -1000,这表示失败.或有时我看不到结果警报.我在使用chrome.socket.read时也遇到问题.

I try to change the host name to different IP or change the port. But sometime I see result = -1000, which indicates failure. Or sometime I cannot see result alert. I also have problem using chrome.socket.read.

请评论我的代码.非常感谢你!

Please critique my code. Thank you very much!

推荐答案

此插件的最佳API示例可能是在GitHub TCP服务器 Web服务器

The best API examples for this plugin are probably those published on GitHub at https://github.com/GoogleChrome/chrome-app-samples/. They're for the chrome.socket API, which is what the plugin implements. In particular, you can check out the TCP Server, Web Server and Websocket Server.

9月份,我还使用该插件作为示例编写了一个简单的Web服务器;您可以在GitHub上找到它

I also wrote a simple web server using the plugin as a demo back in September; you can find it on GitHub here

这些示例都显示了服务器套接字-我没有一个很好的示例向您展示客户端.

These examples all show server sockets, though -- I don't have a good example to show you of the client side.

就您的代码而言,立即有两件事脱颖而出:

As far as your code goes, two things stand out immediately:

首先,您尝试连接到TCP端口0,几乎可以肯定这是不正确的.要连接到HTTP服务,您应该在地址中省略"http://",只需使用服务器名称,并使用80作为端口号即可.

First, you are attempting to connect to TCP port 0, which is almost certainly incorrect. TO connect to an HTTP service, you should leave out the "http://" from the address -- just use the server name, and use 80 as the port number.

第二,看起来您是在chrome.socket.create之后立即调用chrome.socket.connect,在chrome.socket.connect之后立即调用chrome.socket.read,而不是在回调中执行这些操作.这可能意味着可以在创建完成之前发出连接调用,或者可以在绑定调用完成之前发出读取调用.我将从嵌套调用开始,像这样:

Second, it looks like you are calling chrome.socket.connect immediately after chrome.socket.create, and chrome.socket.read immediately after chrome.socket.connect, rather than doing these in callbacks. This might mean that the connect call could be issued before the create is finished, or the read call could be issued before the bind call is completed. I would start by nesting the calls, like this:

chrome.socket.create("tcp", null, function(createInfo) {
    alert(createInfo.socketId);
    theSocketId = createInfo.socketId;
    chrome.socket.connect(theSocketId, "www.yahoo.com", 80, function(result) {
        alert(result);
        if (result === 0) {
            chrome.socket.read(theSocketId, 1000, function(readInfo) {
                alert(readInfo.resultCode);
            });
        }
    });
});

但是,阻止此操作正常进行的下一件事是该代码实际上并未向服务器发送任何请求. chrome.socket是一个低级界面,因此,如果您想通过HTTP进行通信,则您的应用将不得不使用HTTP.

However, the next thing that's going to stop this from working is that the code doesn't actually send any request to the server. chrome.socket is a low-level interface, and so if you want to communicate over HTTP, your app is going to have to talk HTTP.

非常简单的示例代码将与Web服务器通信,并返回其主页的前一千个字节,如下所示:

A very simple bit of example code that will talk to a web server, and return the first thousand bytes of its home page, looks like this:

// Utility functions to convert between array buffers and strings

function stringToArrayBuffer(string) {
    var buffer = new ArrayBuffer(string.length);
    var bufView = new Uint8Array(buffer);
    for (var i=0; i < string.length; i++) {
        bufView[i] = string.charCodeAt(i);
    }
    return buffer;
}

function arrayBufferToString(buffer) {
    return String.fromCharCode.apply(null, new Uint8Array(buffer));
}

// Set the hostname; we'll need it for the HTTP request as well
var hostname = "www.yahoo.com";

chrome.socket.create("tcp", function(createInfo) {
    var socketId = createInfo.socketId;
    chrome.socket.connect(socketId, hostname, 80, function(result) {
        if (result === 0) {
            var requestString = "GET / HTTP/1.1\r\nHost: "+hostname+"\r\nConnection: close\r\n\r\n";
            var requestBuffer = stringToArrayBuffer(requestString);
            chrome.socket.write(socketId, requestBuffer, function(writeInfo) {
                chrome.socket.read(socketId, 1000, function(readInfo) {
                    var htmlString = arrayBufferToString(readInfo.data);
                    // do something with htmlString here
                });
            });
        }
    });
});

这篇关于科尔多瓦chrome.socket API.有什么例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆