首页 > 解决方案 > io 在客户端未定义

问题描述

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

错误:GET https://cdn.socket.io/socket.io-1.2.0.js net::ERR_ABORTED 502 patient-doc:36 Uncaught ReferenceError: io is not defined at patient-doc:36 (anonymous) @patient -doc:36 jquery.min.js:2 jQuery.Deferred 异常:io 未定义 ReferenceError:io 未在 HTMLDocument 中定义。( 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) undefined 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 未在 HTMLDocument 中定义。(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:2k@jquery.min.js:

服务器端代码:

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.jssocket.ioejs

解决方案


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


推荐阅读