我有以下的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?
提前許多感謝,
我無法在端口80上看到監聽,只有'app.listen(8888);'。也許這是一個[同源的政策問題](http://en.wikipedia.org/wiki/Same_origin_policy)? – mindandmedia 2012-02-29 10:11:24
當我在8888上收聽時,我在jQuery文件中遇到了一些錯誤。例如「在XML表達式中缺少} [Break On This Error]」你認爲socket.io和jQuery之間存在一些衝突嗎? – Eamorr 2012-02-29 10:21:49
好的,我現在正在工作......我只需要執行以下操作:「」「而不是」 「Woop woop! – Eamorr 2012-02-29 10:34:56