nodejs-无法获取从客户端发送的值

我正在创build一个Node服务器,它将执行长轮询并从MySql数据库读取数据,并通过WebSocket将其发送到连接到套接字的许多客户端。

我正在学习这个教程

它正在工作(它长时间轮询并将数据返回给客户端),但不是我所期望的。

在我的系统中,每个用户都有唯一的用户ID(主键字段)。 所以我试图通过这个userId到服务器,并返回属于一个用户的通知。

我的客户端代码是用纯JavaScript编写的,看起来像这样:

<script> // create a new websocket var socket = io.connect('http://localhost:8000'); var params={ userId : 1 }; socket.emit('myotherevent', { user: params }); // on message received we print all the data inside the #container div socket.on('notification', function (data) { console.log(data); }); </script> 

正如你所看到的,我试图通过userId,但我无法在服务器端获取。 这里是我提取它的代码部分。

 // creating a new websocket to keep the content updated without any AJAX request io.sockets.on('connection', function (socket) { socket.on('myotherevent', function (data) { var userId = data.userId; }); // starting the loop only if at least there is one user connected if (!connectionsArray.length) { pollingLoop(userId); } socket.on('disconnect', function () { var socketIndex = connectionsArray.indexOf(socket); console.log('socketID = %s got disconnected', socketIndex); if (~socketIndex) { connectionsArray.splice(socketIndex, 1); } }); console.log('A new socket is connected!'); connectionsArray.push(socket); }); 

我不知道为什么我不能在那里得到它。 这里是完整的服务器端代码。

 var app = require('http').createServer(handler), io = require('socket.io').listen(app), fs = require('fs'), mysql = require('mysql'), connectionsArray = [], connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'dumydb', port: 3306 }), POLLING_INTERVAL = 5000, pollingTimer; // If there is an error connecting to the database connection.connect(function (err) { // connected! (unless `err` is set) if (err) { console.log(err); } }); // creating the server ( localhost:8000 ) app.listen(8000); // on server started we can load our client.html page function handler(req, res) { fs.readFile(__dirname + '/client.html', function (err, data) { if (err) { console.log(err); res.writeHead(500); return res.end('Error loading client.html'); } res.writeHead(200); res.end(data); }); } var pollingLoop = function (userId) { // Doing the database query var query = connection.query("SELECT * FROM users WHERE user_id='" + userId + "' "), 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 }); } else { console.log('The server timer was stopped because there are no more socket connections on the app') } }); }; // creating a new websocket to keep the content updated without any AJAX request io.sockets.on('connection', function (socket) { socket.on('myotherevent', function (data) { var userId = data.userId; }); // starting the loop only if at least there is one user connected if (!connectionsArray.length) { pollingLoop(userId); } socket.on('disconnect', function () { var socketIndex = connectionsArray.indexOf(socket); console.log('socketID = %s got disconnected', socketIndex); if (~socketIndex) { connectionsArray.splice(socketIndex, 1); } }); 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(); console.log('Pushing new data to the clients connected ( connections amount = %s ) - %s', connectionsArray.length, data.time); // sending new data to all the sockets connected connectionsArray.forEach(function (tmpSocket) { tmpSocket.volatile.emit('notification', data); }); }; console.log('Please use your browser to navigate to http://localhost:8000');