Answer the question
In order to leave comments, you need to log in
How to make a long connection to mysql?
In general, the essence: there is a script that connects to mysql. The socket server is also started there. The client connects and receives some information from the database throughout the session (i.e. the client needs information - a request is made to the database in the script). A session can last either 1 minute or a whole day.
Now implemented like this:
var mysql = require('mysql');
var http = require('http');
var net = require('net');
var connection = mysql.createConnection({
connectionLimit : 10,
host : 'localhost',
user : 'root',
password : '',
database : 'dbase'
});
connection.connect();
var app = http.createServer(function(req, res) { }).listen(5555);
net.createServer(function (socket) {
socket.on('data', function (data) {
var json = JSON.parse(data);
connection.query('SELECT * FROM os_devices WHERE device_hash = ?', [json.device], function(err, rows) {
if(rows) {
connection.query('UPDATE os_devices SET counter = counter + 1 WHERE device_hash = ?', [json.device], function(err, rows) {
socket.write("ok\r\n");
});
}
else {
socket.write("err\r\n");
}
});
});
});
Error: Connection lost: The server closed the connection.
at Protocol.end (/var/www/server/node_modules/mysql/lib/protocol/Protocol.js:109:13)
at Socket. (/var/www/server/node_modules/mysql/lib/Connection.js:102:28)
at Socket.emit(events.js:117:20)
at _stream_readable.js:944:16
at process._tickCallback(node. js:448:13)
Answer the question
In order to leave comments, you need to log in
Catch the try{ }cath(error){ } error and open another connection, no way?)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question