希望你能帮我解决这个问题...我正在使用node socket.io和mysql中的数据库向10或20人推送新闻,我们有一个简单的asp页面,带有一个文本框和一个按钮,只需插入服务器查询的数据库中的消息.....这就是事情!我需要将桌面通知添加到连接到页面的客户端,并使用刚刚推送的相同信息....
这就是我从client.html获取新闻的方式
<script src="socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
// creamos el websocket con socket.io
var socket = io.connect('http://mysite:8000');
// monstramos todos los datos incluidos en #container div
socket.on('notification', function (data) {
var registrosList = "";
$.each(data.registros,function(index,user){
registrosList += "<div class='media'><div class='media-left'><a><img class='media-object' style='margin-left:4px'; alt='imagen' width='50px' height='50px' src='" + user.link + "'></a></div><div class='media-body'><h5 class='media-heading'>somevariablename <small><i>"+ user.tiempo +"</i></small></h5><p>" + user.user_name + "</p></div></div> <hr />"
""
; });
registrosList += "";
$('#container').html(registrosList);
$('time').html('Ultimo Update:' + data.time +"<hr>") ;
});
</script>
这是srv.js
var moment = require('moment-timezone');
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs'),
mysql = require('mysql'),
connectionsArray = [],
connection = mysql.createConnection({
host: 'mysite',
user: 'root',
password: 'root',
database: 'myBD',
dateStrings:true,
port: 3306
}),
POLLING_INTERVAL = 20000,
pollingTimer;
// if error
connection.connect(function(err) {
// connected! (unles `err` is set)
if (err) {
console.log(err);
}
});
app.listen(8000);
// html
function handler(req, res) {
fs.readFile(__dirname + '/client.html', function(err, data) {
if (err) {
console.log(err);
res.writeHead(500);
return res.end('Error cargando client.html');
}
res.writeHead(200);
res.end(data);
});
}
var pollingLoop = function() {
// query
var query = connection.query("SELECT registros.idImagen, registros.user_name, registros.user_description, registros.user_img, registros.tiempo, tipomensaje.link, registros.idRegistro FROM registros INNER JOIN tipomensaje ON registros.idImagen = tipomensaje.id ORDER BY registros.idregistro desc LIMIT 5"),
registros = []; // registros es el array de la query
query
.on('error', function(err) {
// Handle error, and 'end' event will be emitted after this as well
console.log(err);
updateSockets(err);
})
.on('result', function(user) {
// it fills our array looping on each user row inside the db
registros.push(user);
})
.on('end', function() {
// loop on itself only if there are sockets still connected
if (connectionsArray.length) {
pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
updateSockets({
registros: registros
});
} else {
console.log('el timer del server se detuvo porque no hay mas conexiones a la aplicacion')
}
});
};
// creando un nuevo websocket para mantener el contenido updateado sin request de ajax
io.sockets.on('connection', function(socket) {
console.log('Cant de conexiones:' + connectionsArray.length);
// starting the loop only if at least there is one user connected
if (!connectionsArray.length) {
pollingLoop();
}
socket.on('disconnect', function() {
var socketIndex = connectionsArray.indexOf(socket);
console.log('socketID = %s got disconnected', socketIndex);
if (~socketIndex) {
connectionsArray.splice(socketIndex, 1);
}
});
console.log('nuevo socket conectado!');
connectionsArray.push(socket);
});
var updateSockets = function(data) {
// last update
data.time = moment().tz('America/Argentina/Buenos_Aires').format();
console.log('pusheando data a los clientes conectados ( cant de conexiones = %s ) - %s', connectionsArray.length , data.time);
// sending new data to all the sockets connected
connectionsArray.forEach(function(tmpSocket) {
tmpSocket.volatile.emit('notification', data);
});
};
我可以在向nodejs提交插件时从admin.asp发送通知,并将所有客户端连接到client.html吗?怎么样? ....或者如果有一种方法,当表上有新的插入时,nodejs可以完成这项工作..可能会调用显示最后数据的this api。
我是新节点js和socket.io,我现在迷路了!
非常感谢你!