Hi,
I was trying to tail a log and send the out put to a connected
browser, i can see the log data in the part of:
tail_child.stdout.on('data', function (data) from console.log, but
the socket.broadcast.emit seems not sending out any data, any way to
make this work? thanks, Angelo
part of app.js
socketio.listen(server).on('connection', function (socket) {
console.log("new socket")
// everytime there is a new request, spawn an child process
var tail_child = spawn('tail', ['-f', '/var/log/system.log']);
tail_child.stdout.on('data', function (data) {
// write it on the console
console.log(data.toString()); // yes, i can see the data here
// send it to the browser because the browser will be waiting
for the next chunk of data
socket.broadcast.emit('message', data.toString()); // no,
browser can not see any
});
socket.on('disconnect', function () {
tail_child.kill();
console.log('disconnected me')
});
});
index.html
<html>
<head>
<script src="
http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/
jquery.min.js"></script>
<script src="/
socket.io/socket.io.js"></script>
<script>
$(function () {
var iosocket = io.connect();
iosocket.on('connect', function () {
$('#incomingChatMessages').append($
('<li>Connected</li>'));
iosocket.on('message', function (message) {
$('#incomingChatMessages').append($('<li></
li>').text(message));
});
iosocket.on('disconnect', function () {
$
('#incomingChatMessages').append('<li>Disconnected</li>');
});
});
$('#outgoingChatMessage').keypress(function (event) {
if (event.which == 13) {
event.preventDefault();
iosocket.send($
('#outgoingChatMessage').val());
$('#incomingChatMessages').append($('<li></
li>').text($('#outgoingChatMessage').val()));
$('#outgoingChatMessage').val('');
}
});
});
</script>
</head>
<body>Incoming Chat:
<ul id="incomingChatMessages"></ul>
<br />
<input type="text" id="outgoingChatMessage">
</body>
</html>