【问题标题】:Uncaught ReferenceError: io is not defined [closed]未捕获的ReferenceError:io未定义[关闭]
【发布时间】:2013-06-20 01:36:07
【问题描述】:

我知道这是一个在这里讨论了很多的错误。但是通过谷歌找到了一些帖子后,它仍然没有解决我的问题。

尝试在我的 index.html 中包含 io javascript 时出现上述错误。

我试过了: <script src="http://localhost:8080/socket.io/socket.io.js"></script> 还有<script src="/socket.io/socket.io.js"></script>

甚至尝试将端口切换到3000。但问题仍然存在。

我尝试使用npm install socket.io 重新启动我的 app.js 并重新安装 socket.io(虽然它在我的包中)。我看到在我的 node_modules 文件夹中创建了一个 socket.io 文件夹。

我按如下方式启动我的服务器:

var express = require('express');

var app = express(), 
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

app.listen(8080);

而且我的索引还加载了来自 Google 的最新 jQuery:<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

当我重新启动 app.js 时,我的终端显示 socket.io 已启动:

23 Jun 13:31:56 - [nodemon] starting `node app.js`
   info  - socket.io started

我的 index.html 总共(我从教程中重新输入/改造了它:http://psitsmike.com/2011/09/node-js-and-socket-io-chat-tutorial/

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

<script>

    var socket = io.connect();

    socket.on('connect', function() {
        socket.emit('adduser', prompt("Hoe heet je?"));
    });

    socket.on('updateChat', function(username, data) {
        $('#conversation').append('<strong>' + username  + '</strong> says: ' + data + '<br>');
    });

    socket.on('updateUsers', function(data) {
        $('#users').empty();
        $.each(data, function(k, v) {
            $("#users").append('<div>' + k + '</div>');
        });
    });

    $(document).ready(function() {
        $('#datasend').click(function() {
            //get the message and empty the input
            var msg = $('#data').val();
            $('#data').val('');

            //Let the server execute sendchat along with the msg
            socket.emit("sendChat", msg);
        });

        //ENTER key
        $('#data').keypress(function(e) {
            keyCode = (e.keyCode ? e.keyCode : e.which);
            if (keycode == 13) {
                $(this).blur();
                $('#datasend').focus().click();
            }
        });
    });

</script>
<div style="float:left;width:100px;border-right:1px solid black;height:300px;padding:10px;overflow:scroll-y;">
    <b>USERS</b>
    <div id="users"></div>
</div>
<div style="float:left;width:300px;height:250px;overflow:scroll-y;padding:10px;">
    <div id="conversation"></div>
    <input id="data" style="width:200px;" />
    <input type="button" id="datasend" value="send" />
</div>

【问题讨论】:

  • 错误在哪里?在客户端还是在服务器中? (我假设您在此处发布的代码是服务器端的,我非常希望是这样)
  • 它在我的 Chrome 控制台中。所以它是客户端。
  • 想发布您正在使用的客户端代码吗?此引用错误与服务器端无关。
  • @BenjaminGruenbaum 查看我的编辑
  • 尝试一个相对路径(而不是使用socket.io.jshttp://localhost:8080/socket.io/socket.io.js(假设它在同一个目录中)。这听起来像socket.io 没有正确加载到浏览器

标签: javascript node.js express socket.io


【解决方案1】:

在你的代码中,改变这个

app.listen(8080);

到这里

server.listen(8080);

【讨论】:

    猜你喜欢
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    相关资源
    最近更新 更多