Node.js脚本通过socket.io连接MySQL数据库

发布于 2024-12-04 11:10:31 字数 2614 浏览 1 评论 0原文

我开发了一个 Node.js 脚本来使用 socket.io 连接到 MySQL 数据库。但是,当我尝试使用 Titanium 客户端应用程序连接到服务器时,它只会传递我创建的连接方法;没有错误消息或任何类型的消息,并且套接字只是关闭而不调用脚本。有谁明白我的问题可能是什么。作为参考,我的代码是:

    var app = require('net')
      , fs = require('fs')
      , Client = require('mysql').Client
      , client = new Client();  

    client.user = 'user';
    client.password = 'password'; // Obviously this is not my real password

    var sockets_list = [];

    var server = app.createServer(function (socket) {
      sockets_list.push(socket);
      socket.write("Hello Client\r\n");

      socket.on('data', function(data) {
        console.log(data);
        for (var i = 0; i < sockets_list.length; i++) {
            sockets_list[i].write(data);
        }


      });

      client.connect(function(err, results) {
        if (err) {
            console.log("ERROR: " + err.message);
            socket.write("NO!\r\n");
            throw err;
        }
        console.log("connected.");
        socket.write("Got through here\r\n");
        clientConnected(client);
      });

      socket.on('end', function() {
        var i = sockets_list.indexOf(socket);
        sockets_list.splice(i, 1);
      });


    });



    server.listen(8080);

    clientConnected = function(client)
    {
        client.query('USE db', function(err, results) {
            if (err) {
                console.log("ERROR: " + err.message);
                throw err;
            }
        });
    };

如果有人能告诉我这段代码哪里出了问题,我将不胜感激。谢谢。

问候

陀螺仪队长


正如我之前提到的,我继续使用 node-mysql-libmysqlclient 处理这个脚本,但在实际连接到服务器时遇到了一些困难。我的连接脚本附在此处:

exports.CallStoredProcedureSync = function (test) {
test.expect(12);

var conn;
var result;
var row;
var rows;
var host = "localhost";
var user = "test";
var password = **********;
var database = *******;

var sockets_list = [];
conn = mysql.createConnectionSync();
conn.connectSync(host, user, password, database, 0, 0, conn.CLIENT_MULTI_RESULTS);

if (!conn.connectedSync()) 
{   
sys.puts("Connection error " + conn.connectErrno + ": " + conn.connectError);
socket.write("Connection error " + conn.connectErrno + ": " + conn.connectError);
process.exit(1);
}
// More code here
} 

但是,我实际上无法访问连接后定义的任何代码。如果有人能告诉我哪里出了问题,我将不胜感激。作为记录,我在这里使用 node-mysql-libmysqlclient 的测试代码: https://github.com/Sannis/node-mysql-libmysqlclient/blob/master/tests/complex/test-call-stored-procedure.js 。谢谢。

问候

陀螺船长

I have developed a Node.js script to connect to a MySQL database using socket.io. However, when I do try and connect to the server using my Titanium client application, it just passes over the connection method which I have created; there are no error messages or messages of any kind, and the socket just closes without calling the script. Does anybody understand what my problem might be. For the reference, my code is:

    var app = require('net')
      , fs = require('fs')
      , Client = require('mysql').Client
      , client = new Client();  

    client.user = 'user';
    client.password = 'password'; // Obviously this is not my real password

    var sockets_list = [];

    var server = app.createServer(function (socket) {
      sockets_list.push(socket);
      socket.write("Hello Client\r\n");

      socket.on('data', function(data) {
        console.log(data);
        for (var i = 0; i < sockets_list.length; i++) {
            sockets_list[i].write(data);
        }


      });

      client.connect(function(err, results) {
        if (err) {
            console.log("ERROR: " + err.message);
            socket.write("NO!\r\n");
            throw err;
        }
        console.log("connected.");
        socket.write("Got through here\r\n");
        clientConnected(client);
      });

      socket.on('end', function() {
        var i = sockets_list.indexOf(socket);
        sockets_list.splice(i, 1);
      });


    });



    server.listen(8080);

    clientConnected = function(client)
    {
        client.query('USE db', function(err, results) {
            if (err) {
                console.log("ERROR: " + err.message);
                throw err;
            }
        });
    };

If somebody could tell me where I am going wrong with this code, it would be greatly appreciated. Thank you.

Regards

The Gyro Captain


As I mentioned previously, I have continued to work on this script using node-mysql-libmysqlclient, but I have been encountering some difficulty with actually connecting to the server. My connection script is attached here:

exports.CallStoredProcedureSync = function (test) {
test.expect(12);

var conn;
var result;
var row;
var rows;
var host = "localhost";
var user = "test";
var password = **********;
var database = *******;

var sockets_list = [];
conn = mysql.createConnectionSync();
conn.connectSync(host, user, password, database, 0, 0, conn.CLIENT_MULTI_RESULTS);

if (!conn.connectedSync()) 
{   
sys.puts("Connection error " + conn.connectErrno + ": " + conn.connectError);
socket.write("Connection error " + conn.connectErrno + ": " + conn.connectError);
process.exit(1);
}
// More code here
} 

However, I can't actually access any of the Code defined after the connection. If somebody could tell me where I am going wrong, I would greatly appreciate it. For the record, I am using the test code for node-mysql-libmysqlclient here: https://github.com/Sannis/node-mysql-libmysqlclient/blob/master/tests/complex/test-call-stored-procedure.js. Thanks.

Regards

The Gyro Captain

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光病人 2024-12-11 11:10:32

这是我使用 node-mysqlclient 尝试解决您的问题,它连接到数据库并拉取返回选定表中的所有内容,然后将虚拟字符串写入套接字。您可能还想查看 socket.io ...您在问题中提到了它,但没有使用它这个应用程序。

var app = require('net')
  , mysql = require('mysql-libmysqlclient')
  , conn = mysql.createConnectionSync();


conn.connectSync('localhost', 'user', 'password', 'database');
conn.setCharsetSync('utf8');

var sockets_list = [];

var server = app.createServer(function (socket) {
  sockets_list.push(socket);
  socket.write("Hello Client\r\n");

  socket.on('data', function(data) {
    console.log(data);
    for (var i = 0; i < sockets_list.length; i++) {
      sockets_list[i].write(data);
    }
  });

  clientConnected = function() {
    conn.query('SELECT * FROM table', function(err, results) {
      if (err)
        throw err;
      else {
        results.fetchAll(function (err, rows) {
        if (err)
          throw err;
        // print out database info
        console.log(rows);
        socket.write('socket writes here');
      });
      }
    });
  };

  clientConnected();

  socket.on('end', function() {
    var i = sockets_list.indexOf(socket);
      sockets_list.splice(i, 1);
  });
});

server.listen(8080);

Here is my attempt at your problem using node-mysqlclient, it connects to the db and pulls back everything in a selected table, then it writes out a dummy string to the socket. You may also want to look at socket.io... you mentioned it in your question but it wasn't used in this app.

var app = require('net')
  , mysql = require('mysql-libmysqlclient')
  , conn = mysql.createConnectionSync();


conn.connectSync('localhost', 'user', 'password', 'database');
conn.setCharsetSync('utf8');

var sockets_list = [];

var server = app.createServer(function (socket) {
  sockets_list.push(socket);
  socket.write("Hello Client\r\n");

  socket.on('data', function(data) {
    console.log(data);
    for (var i = 0; i < sockets_list.length; i++) {
      sockets_list[i].write(data);
    }
  });

  clientConnected = function() {
    conn.query('SELECT * FROM table', function(err, results) {
      if (err)
        throw err;
      else {
        results.fetchAll(function (err, rows) {
        if (err)
          throw err;
        // print out database info
        console.log(rows);
        socket.write('socket writes here');
      });
      }
    });
  };

  clientConnected();

  socket.on('end', function() {
    var i = sockets_list.indexOf(socket);
      sockets_list.splice(i, 1);
  });
});

server.listen(8080);

~

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文