我正在尝试从网址example.com获取json数据,并将其传递给我的index.html。我怎样才能做到这一点。没用我想每5秒更新一次文件index.html的数据。 app.js
int ws_dissect_init(void) {
epan_register_plugin_types(); /* Types known to libwireshark */
scan_plugins(REPORT_LOAD_FAILURE);
if (!epan_init(register_all_protocols, register_all_protocol_handoffs, NULL, NULL)) {
/*fprintf(stderr, "Error at epan_init\n");*/
return 2;
}
/*set_disabled_protos_list();*/
// TODO: this one here closes stdin for whatever reason, y tho?
proto_initialize_all_prefixes();
//FIXME: do this properly
#if _WIN32
#else
freopen("/dev/tty", "r", stdin);
#endif
dissect_initialized = TRUE;
return 0;
}
ws_dissect_t *ws_dissect_capture(ws_capture_t *capture) {
epan_free(capture->cfile.epan);
capture->cfile.epan = tshark_epan_new(&capture->cfile);
ws_dissect_t *handle = g_malloc0(sizeof *handle);
handle->cap = capture;
return handle;
}
void ws_dissect_finalize(void) {
// crashes, but we only call this at exit time
// So it's no big deal (TM)
/*epan_cleanup();*/
dissect_initialized = FALSE;
}
index.html
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var https = require('https');
app.get('/', function(req, res) {
res.sendfile('index.html');
//How to use req object ?
});
io.on('connection', function(socket) {
console.log('A user connected');
setInterval(function() {
urlString = "https://example.com/trip?trip_id=1234";
$.get(urlString, function(data, status){
console.log('data');
})
socket.send('');
}, 4000);
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
http.listen(3000, function() {
console.log('listening on *:3000');
});
答案 0 :(得分:0)
您做错了很多事情:
$.get()
未在服务器上运行。那是客户端jQuery代码setInterval()
,而不是为每个客户端连接创建一个新的document.write()
在客户端中,它将仅清除原始文档,因此您想将信息附加到DOM,而不使用document.write()
。.emit(someMessage, someData)
。这是编写代码的一种方法:
// server.js
const app = require('express')();
const server = require('http').Server(app);
const io = require('socket.io')(server);
const request = require('request');
app.get('/', function(req, res) {
res.sendfile('index.html');
});
// create one and only one interval
setInterval(function() {
let urlString = "https://example.com/trip?trip_id=1234";
request(urlString, function(err, response, data) {
if (err) {
console.log("error on request", err);
} else {
console.log('data');
// send to all connected clients
io.emit('message', data);
}
});
}, 5000);
io.on('connection', function(socket) {
console.log('A user connected');
socket.on('disconnect', function () {
console.log('A user disconnected');
});
});
server.listen(3000, function() {
console.log('listening on *:3000');
});
// index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('message', function(data){
let div = document.createElement("div");
div.innerHTML = data;
document.body.appendChild(div);
});
</script>