Answer the question
In order to leave comments, you need to log in
How to store socket value in mySQL database from Node.js?
Node.js Almost none. )))
I am writing a chat server. Clients are all console. Communicate individually, that is, there is no "broadcasting at all."
Question 1.
Is it possible in NODE js to somehow arrange a dynamically growing array of the type:
[id1] [socket_in] [socket_out]
[id2] [socket_in] [socket_out]
....
[idn] [socket_in] [socket_out]
Question 2.
If I already have a mySQL database, how can I store the socket in it? What type of cell can do this?
I don't know myself (((
Thanks for the help.
My initial state, I don't know how to save the client socket.
var net = require('net');
var mysql = require('mysql');
//var clients = [];
net.createServer(function(socket){
var connection = mysql.createConnection({
host : '127.0.0.1',
port : 3306,
database: 'node',
user : 'root',
password : ''
});
connection.connect(function(err){
if(err != null) {
console.log('Error connecting to mysql');
}
else {
console.log('[+]client connected to mysql');
}
});
///////////////////////////////////////////////////////////////////////////////
socket.on('data', function(request) {
var indata = request.toString("binary");
//console.log(indata+'\r');
var msg = indata.trim();
console.log('after trimming message is: ' + msg);
if( msg.substr(0,3) === '/w '){
// Если клиент задал "/w"
var ind = msg.indexOf(' ');
if(ind !== -1){
}
}
else{
console.log(msg);
// Если клиент не задал "/w" нужно сохранить сокет в базе ........ ?
}
});
//connection.end(function(err){
// console.log('[+]client disconnected from mysql');
//});
///////////////////////////////////////////////////////////////////////////////
socket.on('disconnect', function(socket) {
console.log('[+]client disconnected');
});
socket.on('end', function(socket) {
console.log('[+]client ending');
});
socket.on('close', function(socket) {
console.log('[+]client closing');
});
socket.on('error', function(socket) {
console.log('[+]error from client');
});
}).listen('8888');
console.log('for connect use 127.0.0.1:8888'+'\r\n');
Answer the question
In order to leave comments, you need to log in
1) All arrays in JS are dynamic. Use the Array#push method to add a new element to the end.
2) Assign a unique id to each socket and store it in the database (although it is not clear why this is).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question