2017-04-12 94 views
0

我有一個server.js運行在端口3000和adminserv.js運行在端口8000,我想使他們同時運行在相同的端口號。我通過index.html頁面的套接字將數據輸入到數據庫中,並能夠從數據庫中獲取數據並將其顯示在admin.html中,因此我想讓它們在同一時間同時運行在相同的端口號上。 這裏是我的 server.js使用相同的端口爲兩個不同的頁面

var app  =  require("express")(); 
var mysql  =  require("mysql"); 
var http  =  require('http').Server(app); 
var io  =  require("socket.io")(http); 

/* Creating POOL MySQL connection.*/ 

var pool = mysql.createPool({ 
     connectionLimit : 100, 
     host    : 'localhost', 
     user    : 'root', 
     password   : '', 
     database   : 'fbstatus', 
     debug    : false 
}); 

app.get("/",function(req,res){ 
    res.sendFile(__dirname + '/index.html'); 
}); 

/* This is auto initiated event when Client connects to Your Machien. */ 

io.on('connection',function(socket){ 
    console.log("A user is connected"); 
    socket.on('status added',function(status){ 
     add_status(status,function(res){ 
     if(res){ 
      io.emit('refresh feed',status); 
     } else { 
      io.emit('error'); 
     } 
     }); 
    }); 
}); 

var add_status = function (status,callback) { 
    pool.getConnection(function(err,connection){ 
     if (err) { 
      callback(false); 
      return; 
     } 
    connection.query("INSERT INTO `fbstatus` (`s_text`) VALUES ('"+status+"')",function(err,rows){ 
      connection.release(); 
      if(!err) { 
       callback(true); 
      } 
     }); 
    connection.on('error', function(err) { 
       callback(false); 
       return; 
     }); 
    }); 
} 

http.listen(3000,function(){ 
    console.log("Listening on 3000"); 
}); 

的index.html

<html> 
    <head> 
    <title>Socket.io</title> 
    <script src="/socket.io/socket.io.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 
    <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css"> 
    <script src = "http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script> 
    <script> 
    $(document).ready(function(){ 
      var socket = io("http://localhost:3000"); 
      $("#add_status").click(function(){ 
      socket.emit('status added',$("#comment").val()); 
      }); 
      socket.on('refresh feed',function(msg){ 
      $("#show_comments").append(msg + '<br /><br />'); 
      }); 
    }); 
    </script> 
    </head> 
    <body> 
    <div id="comment_box" style = "padding:5%;"> 
     <textarea id="comment" rows="5" cols="70"></textarea><br /><br /> 
     <input type="button" id="add_status" value="Add Status"> 
    </div> 
     <div 
     id = "show_comments" 
     class = "jumbotron" 
     style = "width: 38%; 
       height: 100%; 
       padding: 2%; 
       margin-left:5%; 
       margin-top:-53px;" 
    > 
     </div> 
    </body> 
</html> 

adminserv.js

var app = require('http').createServer(handler), 
    url = require("url"), 
    io = require('socket.io').listen(app), 
    fs = require('fs'), 
    mysql = require('mysql'), 
    connectionsArray = [], 
    connection = mysql.createConnection({ 
     host: 'localhost', 
     user: 'root', 
     password: '', 
     database: 'fbstatus' 
    }), 
    POLLING_INTERVAL = 100, 
    pollingTimer; 
    connection.connect(function(err) { 
     console.log(err); 
    }); 
    app.listen(8000); 

    function handler(req, res) { 
     var url_parts = url.parse(req.url, true); 
     var query = url_parts.query; 

     fs.readFile('admin.html', 
     function(err, data) { 
      if (err) { 
       console.log(err); 
       res.writeHead(500); 
       return res.end('Error loading admin.html'); 
      } 
      res.writeHead(200); 
      res.end(data); 
     }); 
    } 
    var pollingLoop = function() { 
     var query = connection.query('SELECT * FROM fbstatus'), 
     fbstatus = []; 
     query.on('error', 
     function(err) { 
      console.log(err); 
      updateSockets(err); 
     }).on('result', 
     function(user) { 
      fbstatus.push(user); 
     }).on('end', 
     function() { 
      if (connectionsArray.length) { 
       pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL); 
       updateSockets({ 
        fbstatus: fbstatus 
       }); 
      } 
     }); 
    }; 
    io.sockets.on('connection', 
    function(socket) { 
     console.log('Number of connections:' + connectionsArray.length); 
     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) { 
     data.time = new Date(); 
     connectionsArray.forEach(function(tmpSocket) { 
      tmpSocket.volatile.emit('notification', data); 
     }); 
    }; 

admin.html

<html> 
<head> 
<script src="socket.io/socket.io.js"></script> 
<script src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script type="text/javascript"> 
var socket = io.connect('http://localhost:8000'); 
socket.on('notification', 
function(data) { 
    var usersList = "<div>"; 
    $.each(data.fbstatus, 
    function(index, fbstatus) { 
     usersList += "<div><p><span style='width:100px;float:left;text-align:center'>ID :</span>" + fbstatus.status_id + "</p></div>" + "<div><p><span style='width:100px;float:left;text-align:center'>Message :</span>" + fbstatus.s_text + "</p></div>" + "<div><p><span style='width:100px;float:left;text-align:center'>TimeStamp :</span>" + fbstatus.t_status + "</p></div>"; 
    }); 
    usersList += "</div>"; 
    $('#container').html(usersList); 
}); 
</script> 
</head> 
<body> 
<div id="container">Loading ...</div> 
</body> 
</html> 

回答

2

你不能有2個應用程序在同一臺計算機上的同一端口上工作。

不過你可以試試這個:

在server.js聽像app.get('/admin',(req,res)=>{});另一個URL,並在這裏爲你的admin.html。

+0

它說不能得到admin.html –

+0

是在同一文件夾中的那些文件? – mJehanno

+0

是的,他們都在同一個目錄 –

相關問題