【问题标题】:io is undefined on clientsideio 在客户端未定义
【发布时间】:2019-03-05 14:10:26
【问题描述】:

我正在使用 socket.io 制作一个聊天应用程序,在服务器端它说 undefined io 即使我包含了所有文件,我阅读了所有文档并进行了所有必要的更改,但我仍然收到错误

错误:GET https://cdn.socket.io/socket.io-1.2.0.js net::ERR_ABORTED 502 患者文档:36 未捕获的 ReferenceError:io 未定义 在患者文档:36(匿名)@患者文档:36 jquery.min.js:2 jQuery.Deferred 异常:io 未定义 ReferenceError:io 未定义 定义 在 HTML 文档。 (http://localhost:1337/stylescript/pm.js:4:18) 在 j (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:29568) 在 k (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:29882) 未定义 r.Deferred.exceptionHook @ jquery.min.js:2 k @ jquery.min.js:2 setTimeout (async) (匿名) @ jquery.min.js:2 i @ jquery.min.js:2 fireWith@jquery.min.js:2 fire@jquery.min.js:2 i @ jquery.min.js:2 fireWith @jquery.min.js:2 ready @ jquery.min.js:2 R @ jquery.min.js:2 jquery.min.js:2 Uncaught ReferenceError: io is not 定义 在 HTML 文档。 (pm.js:4) 在 j (jquery.min.js:2) 在 k (jquery.min.js:2) (匿名) @ pm.js:4 j @ jquery.min.js:2 k @ jquery.min.js:2 setTimeout (async) r.readyException @ jquery.min.js:2(匿名)@jquery.min.js:2 j@jquery.min.js:2 k @ jquery.min.js:2 setTimeout (async) (匿名) @ jquery.min.js:2 i @ jquery.min.js:2 fireWith@jquery.min.js:2 fire@jquery.min.js:2 i @ jquery.min.js:2 fireWith @ jquery.min.js:2 k @ jquery.min.js:2 setTimeout (async) (匿名) @ jquery.min.js:2 i @ jquery.min.js:2 fireWith@jquery.min.js:2 fire@jquery.min.js:2 i@jquery.min.js:2 fireWith@jquery.min.js:2 准备好了@jquery.min.js:2 R@jquery.min.js:2

服务器端代码:

var express = require("express"),
    app = express();
var http = require('http').Server(app);
var io = require("socket.io")(http, {path: '/chat/:name'});
app.get('/chat/:name', function (req, res) {
    /*async.parallel([
        function (callback) {
            Doctor.findOne({'username': req.user.username})
        }
    ])*/
    res.render('chat/chat', {user: req.user}); 
});

io.sockets.on('connection', function (socket) {

    socket.on('send message', function (data, callback) {//1st parameter is the name we used in html 
        var msg = data.trim();
        var newMsg = new Message({
            body: msg
        });
        newMsg.save(function (err) {
            if (err) throw err;
            io.sockets.emit('new message', { msg: data}); //giving to the subscribers/all the users including me
        })
        // socket.broadcast.emit('new message', data);
        //will send everyone except me
    });

    socket.on('disconnect', function (data) {
        if (!socket.id) return;
    });
});

客户端代码:

<!DOCTYPE html>
<html>
<head>
    <title>MediDesk</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">

    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">

    <script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js" type="text/javascript" async></script>

    <link rel="stylesheet" href="/stylesheets/main.css">
    </head>
<body>


    <h1>chat setup</h1>
    <h1>Prescription</h1>
    <h1>View patient</h1>

    <div class="well" id="contentWrap">
        <div id="chatWrap">
            <div id='chat'></div>
            <form id="send-message">
                <input class="form-control" type="text" size="35" id="message">
                <input type="submit">
            </form>
        </div>

    </div>

    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
    <script>
        var socket = io.connect();
        jQuery(function ($) {
            var $messageForm = $("#send-message");
            var $messageBox = $('#message');
            var $chat = $('#chat');

            $messageForm.submit(function (e) {
                e.preventDefault();
                socket.emit('send message', $messageBox.val());
                $messageBox.val('');
            });

            if (uri === undefined) {
                uri = window.location.pathname;
            }

            var value1 = window.location.pathname;
            var value2 = value1.split('/');
            var value3 = value2.pop();


            socket.of('http://localhost:1337/chat/' + value3).on('new message', function (data) {
                var username = user.username;
                $chat.append('<b>' + username + ': </b>' + data.msg + "<br/>");
            });
        });
    </script>
    <script src="/stylescript/pm.js"></script>
    <script src="/stylescript/deparam.js"></script>
</body>
</html>

【问题讨论】:

    标签: node.js socket.io ejs


    【解决方案1】:

    我们昨天(29sep2018-18:30UTC)遇到了同样的问题。 也就是说,https://cdn.socket.io/socket.io-1.2.0.js 正在返回 502 错误。 现在(30sep2018-04:50UTC)https://cdn.socket.io/socket.io-1.2.0.js 正在按预期再次返回 Javascript 文件,这解决了问题。

    【讨论】:

    • 我发现虽然没有返回 1.2.0 版本(暂时),但 cdn.socket.io 正在返回 1.3.3 版本。因此,如果 CDN 停止返回 1.2.0 版本,您可能会切换到更新的版本。您可以在 socket.io/blog 上查找最新版本,或者从另一个 CDN 获取它,例如 cdnjs.cloudflare.com/ajax/libs/socket.io/1.2.0/socket.io.js 或将其存储在您自己的服务器上。
    猜你喜欢
    • 2013-07-22
    • 2021-04-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2021-08-08
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多