我是一个绝对的nodejs初学者,在实现简单的东西时面临着问题。我想实现以下情况
请注意,这是一个持续的过程。我的日志文件不完整,但它们会不断填充。
非常感谢任何帮助/提示。
此致
答案 0 :(得分:0)
您可以使用socket.io
轻松解决问题Socket.IO支持实时双向基于事件的通信。
在你的情况下,你有2个服务器,每个服务器写入日志文件,所以在写入的函数之后,你必须发出一个我们记录消息的事件,然后我们将在客户端机器中捕获这个事件&# 34; mcentral&#34 ;.你没有发布你的源代码,但是在包含socket io之后,它会以某种方式发布:
机器m1和m2
var io= require("socket.io").listen(http);
// http is your HTTP server created with require("http")
// your function writes in log file a variable called line
// We will emit an event to mcentral that we logged line
io.sockets.on('connection', function (socket) {
socket.broadcast.emit('logging', {message: line}); // we emit an event
});
});
// our server is listening on port 3000 (example)
http.listen(3000,function(){
console.log("Listening on 3000");
});
机器中心 在一个简单的网页中,我将收听我们在机器m1和m2中创建的称为日志记录的事件,当捕获此事件时,我们将在html部分中显示添加的行(我的示例)
<html>
<head>
<title> List </title>
</head>
<body>
<section id="zone_msg">
</section>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:3000');
socket.on('logging', function(data) {
$('#zone_msg').prepend('<p> </strong> ' + data.message + '</p>');
})
</script>
</body>