Socket.io在IE11中以突发方式传输数据
Socket.io transmits data in bursts in IE11
编辑:我将源代码简化为一个最小的测试用例来重现行为。要再现行为,需要"index.html"、"server.js"文件并编译"mycppmodule.cpp"文件。它们全部发布在下面
- IE 11版本:11.0.9600.18097
- 节点版本:v4.1.2
这对我来说是一个有点神秘的问题,描述它有点困难,所以请耐心等待
因此,我有一个node.js服务器,一个非常简单的客户端网站和一个我自己用C++编写的本地node.js模块。服务器和网站之间存在socket.io通信。
这是完整的网站客户端代码,它从服务器获得一个整数值,并在网站上更新一个数字:
// index.html
<!doctype html>
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
$(document).ready(function () {
var socket = io();
var counter = 0;
socket.on('data', function (data) {
console.log("received" + data.counter);
$("#id_counter").text(data.counter);
});
});
</script>
</head>
<body>
Counter: <span id="id_counter"></span>
</body>
</html>
服务器上存在以下代码(完整代码):
// server.js
var http = require("http");
var url = require('url');
var fs = require('fs');
var mycppmodule = require('./build/Debug/mycppmodule');
var server = http.createServer(function(request, response){
console.log('Connection');
var path = url.parse(request.url).pathname;
switch(path){
case '/index.html':
fs.readFile(__dirname + path, function(error, data){
console.log('open ' + __dirname + path);
response.writeHead(200, {"Content-Type": "text/html"});
response.write(data, "utf8");
response.end();
});
break;
default:
response.writeHead(404);
response.write("opps this doesn't exist - 404");
response.end();
break;
}
});
var io = require('socket.io').listen(server);
mycppmodule.testAsync(function (counter) {
console.log(counter);
io.emit('data', { 'counter': counter });
});
io.on('connection', function (socket) {
console.log("client connected");
});
server.listen(8001);
console.log("server running");
我的模块的行为与javascript setInterval
有些相似。它大约每秒被调用一次。这是一个简化的C++模块,显示了相同的行为:
// mycppmodule.cpp
#include <node.h>
#include <v8.h>
#include <nan.h>
using namespace v8;
static Persistent<Function, CopyablePersistentTraits<Function>> _cb; // saving the callback function
static uv_async_t async; //asyncronous messaging
// Some short function that will message the callback every second
void asyncWork(void *arg) {
static int i;
for (;;) {
i++;
async.data = (void*)&i;
uv_async_send(&async);
Sleep(1000);
}
}
// the callback that gets called by asyncWork will then call the javascript callback
void testCallback(uv_async_t *handle) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
int counter = *((int*)handle->data);
auto context = isolate->GetCurrentContext();
auto global = context->Global();
auto fn = Local<Function>::New(isolate, _cb);
const int argc = 1;
Handle<Value> argv[argc];
argv[0] = Number::New(isolate, counter);
fn->Call(global, argc, argv); // this is where the javascript callback gets invoked
}
void testAsync(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
Handle<Function> arg0 = Handle<Function>::Cast(args[0]);
Persistent<Function> cb(isolate, arg0);
_cb = cb; // saving the javascript callback as persisitent
uv_async_init(uv_default_loop(), &async, testCallback);
uv_thread_t thread_id;
uv_thread_create(&thread_id, asyncWork, Null);
}
void init(Handle<Object> target) {
NODE_SET_METHOD(target, "testAsync", testAsync);
}
NODE_MODULE(MyCppModule, init);
以下是令人困惑的部分:
- 整件事就像镀铬的魅力
- 在IE11上,传输是突发的。在大约30秒的时间里什么都没有发生,然后所有的数据都在一个短脉冲串中传输
- 神秘:当我打开一个Chrome实例并在那里打开网站时,它在IE11中按预期工作,即数字在IE11和Chrome中按预期更新。我一关闭Chrome,问题就再次出现
- 神秘:当我使用
setInterval
而不是testAsync
函数时,它在IE11中有效。IE11如何知道谁在服务器上调用io.emit
似乎可以通过向socket.io
通信添加确认来解决此问题。
在index.html
中,我添加了:
// ....
socket.on('data', function (data) {
console.log("received" + data.counter);
$("#id_counter").text(data.counter);
socket.emit('ack'); // ADDED THIS LINE
});
// ...
在server.js
中,我添加了
//...
io.on('connection', function (socket) {
console.log("client connected");
// ADDED THE FOLLOWING THREE LINES
socket.on('ack', function () {
console.log('received');
})
});
//...
整件事对我来说仍然有点神秘,但它现在起作用了:)
相关文章:
- 如何在c++中为模板函数实例创建快捷方式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 在C++中将函数压缩为两种方式
- 以螺旋方式打印矩阵的程序.(工作不好)
- Angelscript从C++传输数组
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 创建引用向量的优雅方式
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- Constexpr替代了新的放置方式,可以让内存中的对象保持未初始化状态
- C++初学者从OpenCV流式传输的优雅方式
- 实时在语言之间流水线传输或以其他方式传输数据
- 以二进制方式打开文件,以什么样的方式流式传输它
- Socket.io在IE11中以突发方式传输数据
- 在不同的进程之间传输cv::Mat对象的最佳方式是什么
- 通过boost::asio传输混合unicode和单字节字符的最佳方式
- 通过boost::asio传输多编码字符的最佳方式
- 在不同平台上通过管道传输数据的最佳和安全方式
- 通过套接字传输大型数组的最快方式
- 将像素流式传输到OpenGL的现代方式