Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从WinRT StreamSocket读取所有可用数据并清空inputStream?_Javascript_Sockets_Asynchronous_Windows Runtime - Fatal编程技术网

Javascript 如何从WinRT StreamSocket读取所有可用数据并清空inputStream?

Javascript 如何从WinRT StreamSocket读取所有可用数据并清空inputStream?,javascript,sockets,asynchronous,windows-runtime,Javascript,Sockets,Asynchronous,Windows Runtime,在向套接字写入新数据之前,我想读取当前等待它的所有数据。 WinRT中的读取方法都是异步的,因此在套接字为空之前,我不能简单地while。 因为我确实想丢弃套接字上的数据,所以我不想使用读取器,而是直接从套接字的IInputStream读取数据 类似于以下伪代码: function emptySocket(inputStream, timeout, buffer) { if (!buffer) { buffer = new Windows.Storage.Streams.Buffer

在向套接字写入新数据之前,我想读取当前等待它的所有数据。 WinRT中的读取方法都是异步的,因此在套接字为空之前,我不能简单地
while
。 因为我确实想丢弃套接字上的数据,所以我不想使用读取器,而是直接从套接字的
IInputStream
读取数据

类似于以下伪代码:

function emptySocket(inputStream, timeout, buffer) {
  if (!buffer) {
    buffer = new Windows.Storage.Streams.Buffer(4096);
  }
  WinJS.Promise.timeout(timeout, inputStream.readAsync(buffer, 4096, Windows.Storage.Streams.InputStreamOptions.partial)
  .then(function(buffer) {
    if (buffer.length === 0) return;
    emptySocket(inputStream, timeout, buffer);
  }), function (error) {
    return;
  });
}
当套接字上不再有任何内容时,超时用于中断等待操作<代码>Windows.Storage.Streams.InputStreamOptions.partial如果套接字上没有任何内容,则似乎不会返回


顺便问一下:
Windows.Storage.Streams.InputStreamOptions.partial
Windows.Storage.Streams.InputStreamOptions.readAhead

除非流/连接结束,否则
IInputStream
不会返回零字节
partial
读取介于1到
count
字节之间的任何内容,并在有数据时立即返回
readAhead
读取
count
或更多字节(如果有更多数据可用),并在至少有
count
字节时返回。如果流结束,最后一个
readAhead
调用返回的字节数可能小于
count
字节。这意味着终止等待新字节的唯一方法是使用超时?或者我可以使用
readAhead
count=0
读取至少0个字节吗?取消
readAsync
操作的问题是关闭底层套接字:(那么是否真的没有与旧Winsock ioctlsocket等效的套接字?您可能有兴趣阅读