找不到 socket.io 的路径.js



我已经安装了node.js和socket.io,并在本地机器上完美运行,但现在我正试图将其传输到我的实时服务器。

我已经安装了node.js和socket.io,它们正在运行,但我似乎无法通过客户端链接到socket.io/socket.io.js文件。它不断返回"500内部服务器错误"

我已经尝试使用这两种路径:

<script type="text/javascript" src="/socket.io/socket.io.js"></script>

<script type="text/javascript" src="https://localhost:8080/socket.io/socket.io.js"></script>

为什么它没有被发现?

"500内部服务器错误"通常意味着"服务器崩溃"或至少"服务器遇到异常"。所以问题可能不是缺少socket.io.js.

无论如何,当我在本地工作和远程不工作之间存在差异时,有时是由于环境变量的差异。您在哪里部署node.js?Heroku,EC2,Joyent?

您更改了连接字符串吗?如果javascript文件已加载,您是否与浏览器检查器进行了检查?

var sio = io.connect('http://yourdomain.com:80');
sio.socket.on('error', function (reason){
  console.error('Unable to connect Socket.IO', reason);
});

在这里我发布了两个文件,一个是chat.js,另一个是chat.html。这是在html.This作品中socket.io.js的路径。

1) chat.js:

var io = require("socket.io");
var socket = io.listen(1223);
socket.set("log level", 1);
var people = {};
socket.on("connection", function (client) {
     client.on("join", function(name){
    people[client.id] = name;
    client.emit("update", "You have connected to the server.");
    socket.sockets.emit("update", name + " has joined the server.")
    socket.sockets.emit("update-people", people);
});
client.on("send", function(msg){
    socket.sockets.emit("chat", people[client.id], msg);
});
client.on("disconnect", function(){
    socket.sockets.emit("update", people[client.id] + " has left the server.");
    delete people[client.id];
    socket.sockets.emit("update-people", people);
});

});

2) chat.html:

     <!DOCTYPE html>
       <html lang="en">
       <head>
        <script src="http://localhost:1223/socket.io/socket.io.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">    </script>
        <script>
        $(document).ready(function(){
            var socket = io.connect("127.0.0.1:1223");
            $("#chat").hide();
            $("#name").focus();
            $("form").submit(function(event){
                  event.preventDefault();
            });
            $("#join").click(function(){
                var name = $("#name").val();
                if (name != "") {
                    socket.emit("join", name);
                    $("#login").detach();
                    $("#chat").show();
                    $("#msg").focus();
                    ready = true;
                }
            });
            $("#name").keypress(function(e){
                if(e.which == 13) {
                    var name = $("#name").val();
                    if (name != "") {
                        socket.emit("join", name);
                        ready = true;
                        $("#login").detach();
                        $("#chat").show();
                        $("#msg").focus();
                    }
                }
            });
            socket.on("update", function(msg) {
                if(ready)
                    $("#msgs").append("<li>" + msg + "</li>");
            })
            socket.on("update-people", function(people){
                if(ready) {
                    $("#people").empty();
                    $.each(people, function(clientid, name) {
                        $('#people').append("<li>" + name + "</li>");
                    });
                }
            });
            socket.on("chat", function(who, msg){
                if(ready) {
                    $("#msgs").append("<li><strong><span class='text-success'>" + who + "</span></strong> says: " + msg + "</li>");
                }
            });
            socket.on("disconnect", function(){
                $("#msgs").append("<li><strong><span class='text-warning'>The server is not available</span></strong></li>");
                $("#msg").attr("disabled", "disabled");
                $("#send").attr("disabled", "disabled");
            });
            $("#send").click(function(){
                var msg = $("#msg").val();
                socket.emit("send", msg);
                $("#msg").val("");
            });
            $("#msg").keypress(function(e){
                if(e.which == 13) {
                    var msg = $("#msg").val();
                    socket.emit("send", msg);
                    $("#msg").val("");
                }
            });
        });
    </script>
   </head>
   <body>
    <div class="row">
      <div class="span2">
        <ul id="people" class="unstyled"></ul>
      </div>
      <div class="span4">
        <ul id="msgs" class="unstyled"></ul>
      </div>
    </div>
    <div class="row">
       <div class="span5 offset2" id="login">
        <form class="form-inline">
        <input type="text" class="input-small" placeholder="Your name" id="name">
            <input type="button" name="join" id="join" value="Join" class="btn btn-primary">
        </form>
    </div>
    <div class="span5 offset2" id="chat">
      <form id="2" class="form-inline">
        <input type="text" class="input" placeholder="Your message" id="msg">
        <input type="button" name="send" id="send" value="Send" class="btn btn-success">
      </form>
        </div>
      </div>
   </body>
</html>

使用命令-node-chat.js运行chat.js并在浏览器中运行chat.html。

我知道它已经8年了,但我也遇到了同样的错误,也许我的解释可以帮助别人。

通过";试图转移到我的实时服务器";我通过Apache的Passenger模块将它部署到运行Node的虚拟共享主机上。然后我联系了技术支持,他们说脚本开始监听一些端口并崩溃,在这种类型的托管计划中根本不可能,我应该申请VPS/VDS。

这听起来很奇怪,因为该应用程序甚至没有开始监听,只是访问静态文件。但Express传递静态文件的方式可能不起作用。日志显示:

[pid 66771]19:33:18侦听(12111)=-1 EPERM(不允许操作)events.js:183投掷者;//未处理的"错误"事件^

我能够阅读Express正在使用";流";(nodejs.org/api/stream.html)来传递静态文件,我有一个建议,它根本不适用于那种类型的托管。其他静态文件是物理存在的,因此它们是通过Nginx交付的,并且不会失败。这让一些人感到惊讶,因为有些文件被加载,有些文件在找不到资源时给出错误500,甚至不是4xx。

基本上响应500告诉我们输出意外结束。日志上写着";"头之前的脚本结束";。当您无法访问任何日志消息,只收到响应500,并且必须联系支持人员才能查看日志时,这是令人不快的。

最新更新