【问题标题】:Socket.io on port 8888 and webserver on port 80 - can't combine the two8888 端口上的 Socket.io 和 80 端口上的网络服务器 - 不能将两者结合起来
【发布时间】:2012-03-18 19:38:26
【问题描述】:

我有以下 node.js 服务器端代码:

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);。也许这是same origin policy problem
  • 当我在 8888 上收听时,我在 jQuery 文件中发现了大量错误。例如“XML 表达式中缺少 } [Break On This Error]” 你认为 socket.io 和 jQuery 之间存在冲突吗?
  • 好的,我现在开始工作了...我只需要执行以下操作:“" 呜呜呜!
  • 对你有好处!然后将其标记为正确答案:)

标签: javascript jquery node.js websocket port


【解决方案1】:

服务器将 Socket 绑定到特定端口,进入系统的数据根据​​寻址端口重定向。

您的 Apache 也在特定端口上工作。它具有绑定到特定端口的侦听套接字。默认为 80。

当您导航到 localhost:80 时,您的浏览器会创建与 Apache 的连接,它们会交换一些数据和 html。

然后,当您使用 JS 创建 WebSocket 时,浏览器会创建套接字并尝试连接到提供的地址和端口。如果您的 websockets 服务器绑定到端口 8888,那么它将成功连接到它。

然后,如果您尝试在浏览器顶部 localhost:8888 中导航,会发生什么情况,浏览器会创建与您的服务器的连接,但在端口 8888 上您有 WebSockets 服务器,它们会连接,但会在握手和其他操作中失败东西。

在 Apache 中托管文件是一个端口,而 websockets 服务器是另一个端口。 查看一些info 了解什么是端口以及它们是如何工作的。

【讨论】:

  • 非常有用的答案。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 2013-10-29
相关资源
最近更新 更多