2012-02-29 79 views
3

我有以下的node.js服務器端代碼:Socket.io 8888端口和網絡服務器的80端口 - 不能將二者結合起來

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

app.listen(8888); 

var fn='/home/eamorr/workspace/node_proxy_remote5/data'; 

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) { 
    fs.watchFile(fn,function(curr,prev){ 
     //curr.time 
     fs.readFile(fn,function(err,data){ 
      socket.emit('data',data.toString()); 
      console.log(data); 
     }); 
    }); 
}); 

正如你所看到的,我看文件並向瀏覽器發送修改。

在客戶端,我有:

<html> 

<head> 
<script src="/socket.io/socket.io.js"></script> 

<script type="text/javascript" src="./jqPlot/jquery.min.js"></script> 
<script type="text/javascript" src="./jqPlot/jquery.jqplot.min.js"></script> 
<script type="text/javascript" src="./jqPlot/plugins/jqplot.canvasTextRenderer.min.js"></script> 
<script type="text/javascript" src="./jqPlot/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script> 
<link rel="stylesheet" type="text/css" href="./jqPlot/jquery.jqplot.min.css" /> 

</head> 

<body> 
<script type="text/javascript"> 
var socket = io.connect('http://localhost:8888'); 
socket.on('data', function (data) { 
    console.log(data); 
    //socket.emit('my other event', { my: 'data' }); 
}); 
</script> 
<div id="chart2" style="height:300px; width:500px;"></div> 


<script type="text/javascript"> 
$(document).ready(function(){ 
    var plot2 = $.jqplot ('chart2', [[3,7,9,1,5,3,8,2,5]], { 
     // Give the plot a title. 
     title: 'Bandwidth over port 10001', 
     // You can specify options for all axes on the plot at once with 
     // the axesDefaults object. Here, we're using a canvas renderer 
     // to draw the axis label which allows rotated text. 
     axesDefaults: { 
      labelRenderer: $.jqplot.CanvasAxisLabelRenderer 
     }, 
     // Likewise, seriesDefaults specifies default options for all 
     // series in a plot. Options specified in seriesDefaults or 
     // axesDefaults can be overridden by individual series or 
     // axes options. 
     // Here we turn on smoothing for the line. 
     seriesDefaults: { 
      rendererOptions: { 
       smooth: true 
      } 
     }, 
     // An axes object holds options for all axes. 
     // Allowable axes are xaxis, x2axis, yaxis, y2axis, y3axis, ... 
     // Up to 9 y axes are supported. 
     axes: { 
      // options for each axis are specified in seperate option objects. 
      xaxis: { 
      label: "X Axis", 
      // Turn off "padding". This will allow data point to lie on the 
      // edges of the grid. Default padding is 1.2 and will keep all 
      // points inside the bounds of the grid. 
      pad: 0 
      }, 
      yaxis: { 
      label: "Y Axis" 
      } 
     } 
    }); 
}); 

</script> 

</body> 

</html> 

正如你所看到的,我想提請使用jqPlot從服務器發送的數據的圖表。

我用這段代碼的問題是,如果我導航到http://localhost:80,圖表顯示正常,但沒有啓動websocket。如果我導航到http://localhost:8888,圖表不會顯示,但websocket工作正常!我怎樣才能結合node.js和jQuery?

提前許多感謝,

+1

我無法在端口80上看到監聽,只有'app.listen(8888);'。也許這是一個[同源的政策問題](http://en.wikipedia.org/wiki/Same_origin_policy)? – mindandmedia 2012-02-29 10:11:24

+0

當我在8888上收聽時,我在jQuery文件中遇到了一些錯誤。例如「在XML表達式中缺少} [Break On This Error]」你認爲socket.io和jQuery之間存在一些衝突嗎? – Eamorr 2012-02-29 10:21:49

+0

好的,我現在正在工作......我只需要執行以下操作:「」「而不是」 「Woop woop! – Eamorr 2012-02-29 10:34:56

回答

3

服務器綁定套接字到特定的端口和數據來系統重定向基於解決端口。

您的Apache正在處理特定的端口。它具有綁定到特定端口的監聽套接字。默認情況下,它是80.

當您導航到localhost:80時,您的瀏覽器創建與Apache的連接,並且它們交換一些數據和htmls。

然後,當您使用JS創建WebSocket時,瀏覽器會創建套接字並嘗試連接到提供的地址和端口。如果您的websocket服務器綁定到端口8888,那麼它將成功連接到它。

然後,如果您試圖在瀏覽器頂端localhost:8888中導航,那麼瀏覽器會創建與服務器的連接,但是在Port 8888上您擁有WebSockets服務器,它們將連接,但它們將在握手時失敗並且其他事情。

Apache中的主機文件是一個端口,而websockets服務器是另一個端口。 查看一下info關於什麼是端口以及它們是如何工作的。

+1

真的很有用的答案。謝謝。 – miksiii 2015-01-10 19:35:53