简体   繁体   English

django模板中的socket.io - node.js不是服务socket.io.js

[英]socket.io in django template - node.js not service socket.io.js

i have Django app which need to use real time push to client. 我有Django应用程序,需要使用实时推送到客户端。
i want to use node.js and socket.io (which i understood is the easiest platform to do it today..). 我想使用node.js和socket.io(我知道这是今天最简单的平台...)。
to implement it i put the socket.io framework code on the template: 为了实现它,我将socket.io框架代码放在模板上:

{% extends "base.html" %}

{% block content %}
{% if error %}
  <div id="error">
        <h3>{{ error }}</h3>
  </div>
{% endif %}
<div id="reply">

<script src="http://localhost:8889/socket.io/socket.io.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script>

        var socket = io.connect('http://localhost:8889');

        socket.on('connect', function(){
                socket.emit('addOrder', 99, {{ reply_id }});
        });

</script>
Thank you for your reply
</div>
{% endblock %}

my node.js runs on 8889 as you can see.. so i need to call "script src" to import it from localhost:8889/socket.io/socket.io.js (the Django server and the node.js server is the same server. this is why i am using localhost). 我的node.js在8889上运行,你可以看到..所以我需要调用“script src”从localhost导入它:8889 / socket.io / socket.io.js(Django服务器和node.js服务器是相同的服务器。这就是我使用localhost的原因)。

the issue is that when i calling to this template (render it from view), and using F12 on chrome, i see "CAUTION: Provisional headers are shown.". 问题是,当我调用此模板(从视图中呈现),并在chrome上使用F12时,我看到“注意:显示临时标题。”。
i tried to check here what is means but i only saw talks about AdBlocks, so i uninstalled it, but i still getting this error... 我试着检查一下是什么意思,但我只看到有关AdBlocks的谈话,所以我卸载它,但我仍然得到这个错误...

after more investigation i saw that even on apache (native HTML files) i cannot get the js file (same error). 经过更多调查后,我发现即使在apache(原生HTML文件)上我也无法获取js文件(同样的错误)。

it looks like the reason is i have to use node.js http server (express) to serve this file, and cannot call it from any other web server. 看起来原因是我必须使用node.js http服务器(express)来提供此文件,并且无法从任何其他Web服务器调用它。

Does anyone have any idea what can be the reason or how to fix it? 有谁知道可能是什么原因或如何解决它?

Node.js Server code: Node.js服务器代码:

var express = require("express");
var app = express();
var io = require('socket.io').listen(app.listen(8889));
app.get('/', function (req, res) {
  res.sendfile(__dirname + '/index.html');
});
app.get('/menu.css', function (req, res) {
  res.sendfile(__dirname + '/menu.css');
});
var branches = {};
ojson = {"count":1,"orders":[{"id":100,"state":"ok"}]};
io.sockets.on('connection', function (socket) {
        socket.on('connectBranch', function(branchID) {
                //save ID info on the socket
                socket.branchID = branchID
                // save the socket of the branch by ID
                branches[branchID]=socket;
                // ask the client to run getAll function with orders JSON
                branches[branchID].emit('getAll', ojson);
        });

        // clear - for test
        socket.on('clearOrders', function(branchID) {
                branches[branchID].emit('clearOrders');
        });

        // when the client emits 'addOrder', this listens and executes
        socket.on('addOrder', function(branchID, orderID){
                console.log(branches);
                branches[branchID].emit('addOrder', orderID);
        });

        // when the user disconnects.. perform this
        socket.on('disconnect', function(){
                // remove the branch from global branches list
                delete branches[socket.branchID];
        });
});

unless you are running this on a localhost 除非您在localhost上运行此操作

<script src="http://localhost:8889/socket.io/socket.io.js"></script>

needs to be changed to 需要改为

<script src="http://SERVER_IP:8889/socket.io/socket.io.js"></script>

better yet, if your frontent HTTP server is NGINX or similar, you can proxy calls to socket.io.js to port 8889, and then change your page to 更好的是,如果您的前端HTTP服务器是NGINX或类似的,您可以将对socket.io.js调用代理到端口8889,然后将您的页面更改为

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

this way you dont have to specify IP, it will be the same as the one where original HTML page came from 这种方式你不必指定IP,它将与原始HTML页面来自的那个相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM