2012-05-30 52 views
3

我試着用HTML5 + MySQL + Node.js + socket.io登錄數據庫,但失敗了。請幫忙。 我想用Node.js和socket.io從HTML5頁面登錄到MySQL數據庫使用socket.io在Node.js中使用MySql數據庫記錄HTML5頁面

我有一個帶有2個文本框用戶名和密碼的HTML5頁面和一個LogIn按鈕。 用戶想要從HTML5頁面以用戶名/密碼登錄發送到服務器,服務器收到用戶名/密碼並使用mysql數據庫驗證並返回結果。

我的代碼: server_test.js

var io = require('C:/Program Files (x86)/nodejs/node_modules/socket.io/lib/socket.io'); 
var socket = io.listen(8124); 
socket.sockets.on('connection',function(socket){ 
    socket.on('login', function(data, usr, pass){ 
     var mysql = require('mysql'); 
     var TEST_DATABASE = 'employee'; 
     var TEST_TABLE = 'tblusers'; 
     var client = mysql.createClient({ 
      user: 'root', 
      password: 'secret10', 
     }); 

     client.query('USE '+TEST_DATABASE); 

     client.query(
      'SELECT name FROM '+TEST_TABLE+' WHERE user = '+usr+' AND password = '+pass, 
      function selectCb(err, results) { 
      if (err) { 
       throw err; 
      } 
      //Emit a message to client 
      socket.emit('retuLogIn',{username: results[0]['name']}); 
      client.end(); 
      } 
     ); 
    }); 
     socket.on('disconnect', function(){ 
      console.log('Server has disconnected'); 
     }); 
}); 

client_test.html

<!doctype html> 
<html> 
    <title>WebSocket Client Demo [socket.io]</title> 
    <script src="/json.js"></script> <!-- for ie --> 
    <script src="http://localhost:8124/socket.io/socket.io.js"></script> 
    <script> 
    function connect() { 

     try 
     { 
      var socket = io.connect('http://localhost:8124/'); 
      socket.on("connect",function(){ 
       document.getElementById('status').innerHTML ="Browser has connected to the app server"; 
      }); 
      socket.on('login', function (data, document.getElementById('txtUser').value, document.getElementById('txtPass').value) { 
       //alert(data.hello); 
       document.getElementById('status').innerHTML = 'Welcome '+data.username; 
      }); 
     } 
     catch(err) 
     { 
      document.getElementById('status').innerHTML = err.message; 
     } 
    } 
    </script> 
    <body> 
     <h1>WebSocket Client Demo</h1> 
     <div><p id="status">Enter user and password to Log-In</p></div> 
     <label>User :</label> 
     <input id="txtUser" type="text" maxlength="10" /> 
     <label>Password :</label> 
     <input id="txtPass" type="text" maxlength="10" /> 
     <button id="connect" onClick='connect()'/>Log-In</button> 
    </body> 
</html> 

注: 我試着用上面提到的代碼,但沒有成功。我試圖用socket.send('abcd','ab12')將用戶名/密碼從客戶端發送到服務器。能夠發送但不能在服務器中接收。

請幫我解決這個問題。我正在使用MySQL數據庫。

作爲一個更新,當我申請喬爾的建議,我還看到一個錯誤,當我嘗試在Node.js的運行app.js:

C:\Program Files (x86)\nodejs>node "C:\Program Files (x86)\nodejs\app.js" info - socket.io started 

C:\Program Files (x86)\nodejs\app.js:6 
    var client = mysql.createConnection({ 
        ^TypeError: Object #<Object> has no method 'createConnection' 
    at Object.<anonymous> (C:\Program Files (x86)\nodejs\app.js:6:24) 
    at Module._compile (module.js:449:26) 
    at Object..js (module.js:467:10) 
    at Module.load (module.js:356:32) 
    at Function._load (module.js:312:12) 
    at module.js:487:10 
    at EventEmitter._tickCallback (node.js:238:9) 

C:\Program Files (x86)\nodejs> 

可能是什麼不對的呢?

回答

0

我只是固定的腳本,我需要類似的東西,在這裏你去:

app.js:

var app = require('http').createServer(handler) 
     , io = require('socket.io').listen(app) 
     , fs = require('fs') 
     , mysql = require('mysql') 

    var client = mysql.createConnection({ 
       host: 'localhost', 
       user: 'root', 
       password: '', 
    }); 

    client.connect(); 

    app.listen(3232); 

    function handler (req, res) { 
     fs.readFile(__dirname + '/index.html', 
     function (err, data) { 
     if (err) { 
      res.writeHead(500); 
      return res.end('Error loading index.html'); 
     } 

     res.writeHead(200); 
     res.end(data); 
     }); 
    } 

    io.sockets.on('connection', function (socket) { 
     socket.on('login', function(usr, pass){ 
      var TEST_DATABASE = 'mysqltest'; 
      var TEST_TABLE = 'users'; 

      client.query('USE '+TEST_DATABASE); 

      client.query('SELECT name FROM '+TEST_TABLE+' WHERE user = "'+usr+'" AND password = "'+pass+'"', function(err, results) { 
       if (err) throw err; 
       console.log(results[0]); // [{1: 1}] 
       socket.emit('retuLogIn',results[0]['name']); 
      }); 

     }); 
     socket.on('disconnect', function(){ 
      console.log('Server has disconnected'); 
     }); 
    }); 

而且index.html的:

<html> 
    <title>WebSocket Client Demo [socket.io]</title> 
    <script src="http://localhost:3232/socket.io/socket.io.js"></script> 
    <script> 
    function connect() { 

     try 
     { 
      var socket = io.connect('http://localhost:3232/'); 
      socket.on("connect",function(){ 
       document.getElementById('status').innerHTML ="Browser has connected to the app server"; 
       socket.emit('login', document.getElementById('txtUser').value, document.getElementById('txtPass').value); 

      }); 
      socket.on('retuLogIn', function (data) { 
       document.getElementById('status').innerHTML = 'Welcome '+data; 
      }); 
     } 
     catch(err) 
     { 
      document.getElementById('status').innerHTML = err.message; 
     } 
    } 
    </script> 
    <body> 
     <h1>WebSocket Client Demo</h1> 
     <div><p id="status">Enter user and password to Log-In</p></div> 
     <label>User :</label> 
     <input id="txtUser" type="text" maxlength="10" /> 
     <label>Password :</label> 
     <input id="txtPass" type="text" maxlength="10" /> 
     <button id="connect" onClick='connect()'/>Log-In</button> 
    </body> 
</html> 

您需要改變像MySQL數據庫名稱,用戶名,密碼和probaly端口如果需要的東西。

我用node.js中,socket.io和節點的MySQL

+0

喬爾,我試着用你的代碼,但沒有成功。你能看到我錯在哪裏。我得到錯誤:C:\ Program Files文件(x86)\ nodejs>節點「C:\ Program Files文件(x86)\ nodejs \ app.js」信息 - socket.io啓動C :Program Files(x86)\ nodejs \ app.js:6 var client = mysql.createConnection({^ TypeError:Object#在Object處沒有方法'createConnection'(C:\ Program Files(x86)\ nodejs \ app .js:6:24)在Module.load(module.js:356:32)上的Object.js(module.js:467:10)上​​的Module._compile(module.js:449:26)。在EventEmitter._tickCallback(node.js:238:9)處的module.js:487:10處的_load(module.js:312:12) –

0

CHANDAN德伊你缺少

mysql = require('mysql') 
在你的代碼

,歡呼聲

相關問題