javascript - 端口 8888 上的 Socket.io 和端口 80 上的网络服务器 - 无法将两者结合起来

标签 javascript jquery node.js websocket port

我有以下 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 ,图表显示正常,但没有启动 websockets。如果我导航到 http://localhost:8888 ,图表不会显示,但 websocket 工作正常!如何结合使用 node.js 和 jQuery?

非常感谢,

最佳答案

服务器将 Socket 绑定(bind)到特定的端口,到达系统的数据将根据地址端口进行重定向。

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

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

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

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

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

关于javascript - 端口 8888 上的 Socket.io 和端口 80 上的网络服务器 - 无法将两者结合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9497078/

相关文章:

javascript - 链二选择 rxjs store in Angular Guard

javascript - 如何使用 nodejs(加密)生成 32 字节的 SHA256 哈希,以避免 tweetnacl.js 抛出错误的 key 大小错误?

javascript - CreateJS - 缩放 Canvas 不会缩放鼠标坐标

javascript - Node.JS Sandbox 包含某些方法和库

node.js - NodeJS/Express + HTTPS : How to deploy key & certificate to AWS EC2 node?

javascript - 在javascript中使用特定时区转换特定日期时间

jquery - 如何在克隆元素时或更快地重置 id 和 data-attr

javascript - Ajax 中的加载指示器

javascript - 在浏览器中将一页水平滚动的站点居中(而不是将 div 居中)

node.js - 使用 ExpressJS 发送生成的 zip 文件