下面是我的服务器代码:
var app = require('http').createServer(),
io = require('socket.io').listen(app),
fs = require('fs'),
mysql = require('mysql'),
connectionsArray = [],
connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '',
database : 'test',
port : 3306
}),
POLLING_INTERVAL = 1000,
pollingTimer;
// If there is an error connecting to the database
connection.connect(function(err) {
// connected! (unless `err` is set)
console.log( err );
});
// creating the server ( localhost:8000 )
app.listen(8000);
/*
*
* HERE IT IS THE COOL PART
* This function loops on itself since there are sockets connected to the page
* sending the result of the database query after a constant interval
*
*/
var pollingLoop = function () {
// Doing the database query
var query = connection.query('SELECT * FROM users'),
users = []; // this array will contain the result of our db query
// setting the query listeners
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
users.push( user );
})
.on('end',function(){
// loop on itself only if there are sockets still connected
if(connectionsArray.length) {
pollingTimer = setTimeout( pollingLoop, POLLING_INTERVAL );
updateSockets({users:users});
}
});
};
// creating a new websocket to keep the content updated without any AJAX request
io.sockets.on( 'connection', function ( socket ) {
console.log('Number of connections:' + 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('socket = ' + socketIndex + ' disconnected');
if (socketIndex >= 0) {
connectionsArray.splice( socketIndex, 1 );
}
});
socket.on('ddd', function (name) {
socket.username=name;
});
console.log( 'A new socket is connected!' );
connectionsArray.push( socket );
});
var updateSockets = function ( data ) {
// adding the time of the last update
data.time = new Date();
// sending new data to all the sockets connected
connectionsArray.forEach(function( tmpSocket ){
tmpSocket.volatile.emit( 'notification' , data );
});
};
首先,您必须为客户端嵌入socket.io JavaScript:
script(rel="text/javascript" src="/js/libraries/socket.io.min.js");
然后像这样连接到套接字服务器:
var socket = io.connect("https://localhost:8080");
button.on("click", function(){
socket.emit("event-name", { userId : 2 });
});
socket.on("event-name", function(data){
console.log(data.userId); // logs: 2
var query = // ...
socket.emit("response", query);
});
socket.on("response", function(data){
console.log(data); // logs: the query
});
要确定哪个客户端应该接收发射,请参见:
io房间还是命名空间?
如何在我的routes.jsx文件中定义一个路由来捕获参数值,这些参数值是在从服务器重定向后由Twitter的单点登录进程生成的URL中获得的? 我尝试了以下路由配置,但未捕获提到的参数:
问题内容: 我们可以像在PHP中一样在Node.js中获取查询字符串中的变量吗? 我知道在Node.js中,我们可以在请求中获取URL。有没有获取查询字符串参数的方法? 问题答案: 在Express中,它已经为您完成,您只需使用req.query即可: 否则,在NodeJS中,您可以手动访问req.url和内置模块来[url.parse](https://nodejs.org/api/url.ht
问题内容: 从服务器重定向后,如何在我的routes.jsx文件中定义路由以从Twitter的单点登录过程生成的URL 捕获参数值? 我尝试使用以下路由配置,但没有捕获上述参数: 问题答案: 反应路由器v3 React Router已经为您解析了位置并将其作为道具传递给RouteComponent。您可以通过访问查询(在URL中的?之后)部分 如果要查找路径参数值(在路由器内部用冒号(:)分隔),
问题内容: 我正在使用BASH,但我不知道如何查找子字符串。它一直失败,我有一个字符串(应该是数组吗?) 下面是数据库名称的字符串列表,是答复(这些数据库之一)。以下内容仍然无效: 我也尝试过此方法,但不起作用: 问题答案: LIST=”some string with a substring you want to match” SOURCE=”substring” if echo “$LIST
问题内容: 是否有通过jQuery(或不通过jQuery)检索[查询字符串]值的无插件方法? 如果是这样,怎么办?如果没有,是否有可以做到的插件? 问题答案: const urlParams = new URLSearchParams(window.location.search); const myParam = urlParams.get(‘myParam’); 原版的 为此,您不需要jQue
我有url从中我需要得到的值. “TypeError:无法读取未定义的属性'query'”