2015-09-27 142 views
-2

我試圖使我第一次推送通知腳本使用和的NodeJS Socket.io 我發現這個教程: http://www.gianlucaguarini.com/blog/push-notification-server-streaming-on-a-mysql-database/語法錯誤:意外標記<

這是真正的幫助,但我還是堅持了幾個小時,不能去進一步。

我創建了server.js和client.html文件。當我呼叫節點服務器時,它正在工作,控制檯在某人輸入等時更新,但當我在瀏覽器上訪問我的網站時,我沒有看到來自我的數據庫的任何內容。

雖然我嘗試在控制檯運行client.html(節點client.html)我得到以下錯誤:

 
[email protected]:~# node client.html 
/root/client.html:1 (function (exports, require, module, __filename, __dirname) { <html> 
                       ^

SyntaxError: Unexpected token < 
    at exports.runInThisContext (vm.js:53:16) 
    at Module._compile (module.js:413:25) 
    at Object.Module._extensions..js (module.js:452:10) 
    at Module.load (module.js:355:32) 
    at Function.Module._load (module.js:310:12) 
    at Function.Module.runMain (module.js:475:10) 
    at startup (node.js:117:18) 
    at node.js:951:3 

client.html:

<html> 
<head> 
<title>Test</title> 
</head> 
    <body> 
     <time></time> 
     <div id="container">Loading ...</div> 
    <script src="socket.io/socket.io.js"></script> 
    <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
    <script> 

     // create a new websocket 
     var socket = io.connect('http://46.101.226.135:8000/'); 
     // on message received we print all the data inside the #container div 
     socket.on('notification', function (data) { 
     var usersList = "<dl>"; 
     $.each(data.test_payout,function(index,user){ 
      usersList += "<dt>" + user.nick + "</dt>\n" + 
         "<dd>" + user.payout + "\n" + 
          "<figure> <img class='img-polaroid' width='50px' src='" + user.country + "' /></figure>" 
         "</dd>"; 
     }); 
     usersList += "</dl>"; 
     $('#container').html(usersList); 

     $('time').html('Last Update:' + data.time); 
     }); 
    </script> 
    </body> 
</html> 

server.js:

var app     = require('http').createServer(handler), 
    io     = require('socket.io').listen(app), 
    fs     = require('fs'), 
    mysql    = require('mysql'), 
    connectionsArray = [], 
    connection   = mysql.createConnection({ 
     host  : '46.105.14.241', 
     user  : 'user', 
     password : 'password', 
     database : 'database', 
     port  : 2083 
    }), 
    POLLING_INTERVAL = 3000, 
    pollingTimer; 

// If there is an error connecting to the database 
connection.connect(function(err) { 
    // connected! (unless `err` is set) 
    console.log(err); 
}); 

// create a new nodejs server (http://46.101.226.135:8000/) 
app.listen(8000); 

// on server ready 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); 
    }); 
} 

/* 
* 
* HERE IT IS THE COOL PART 
* This function loops on itself since there are sockets connected to the page 
* sending the result of the database query after a constant interval 
* 
*/ 
var pollingLoop = function() { 

    // Make the database query 
    var query = connection.query('SELECT * FROM test_payout'), 
     test_payout = []; // this array will contain the result of our db query 


    // set up 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 
     test_payout.push(user); 
    }) 
    .on('end',function(){ 
     // loop on itself only if there are sockets still connected 
     if(connectionsArray.length) { 
      pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL); 

      updateSockets({test_payout:test_payout}); 
     } 
    }); 

}; 

// create a new websocket connection to keep the content updated without any AJAX request 
io.sockets.on('connection', function (socket) { 

    console.log('Number of connections:' + connectionsArray.length); 
    // start the polling loop only if at least there is one user connected 
    if (!connectionsArray.length) { 
     pollingLoop(); 
    } 

    socket.on('disconnect', function() { 
     var socketIndex = connectionsArray.indexOf(socket); 
     console.log('socket = ' + socketIndex + ' disconnected'); 
     if (socketIndex >= 0) { 
      connectionsArray.splice(socketIndex, 1); 
     } 
    }); 

    console.log('A new socket is connected!'); 
    connectionsArray.push(socket); 

}); 

var updateSockets = function (data) { 
    // store the time of the latest update 
    data.time = new Date(); 
    // send new data to all the sockets connected 
    connectionsArray.forEach(function(tmpSocket){ 
     tmpSocket.volatile.emit('notification' , data); 
    }); 
}; 
+0

Node.js無法運行HTML代碼。你正在尋找一個瀏覽器。 – SLaks

+0

@SLaks它可以在本地主機上工作?我幾乎從教程中複製粘貼,只改變了數據庫信息,我使用我的服務器IP而不是本地主機。本教程不好或者我錯過了一些東西? ?? – lewtakm

+0

否;你需要在瀏覽器中打開HTML。 – SLaks

回答

1

您只需要運行

node server.js 

確保您有socket.iomysql通過npm安裝。

然後將您的瀏覽器指向http://localhost:8000以獲取由節點服務器提供的client.html。

似乎數據庫憑證不再有效,因爲命令行中的輸出建議。

{ [Error: Connection lost: The server closed the connection.] fatal: true, code: 'PROTOCOL_CONNECTION_LOST' } 
相關問題