我正在使用socket.io制作一个聊天应用程序,在服务器端它说的是未定义的io,即使我包含了所有文件,但我阅读了所有文档并进行了所有必要的更改后,我仍然遇到错误
错误:GET https://cdn.socket.io/socket.io-1.2.0.js净值:: ERR_ABORTED 502 Patient-doc:36 Uncaught ReferenceError:未定义io 在Patient-doc:36(匿名)@ 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) 未定义r.Deferred.exceptionHook @ jquery.min.js:2 k @ jquery.min.js:2 setTimeout(异步)(匿名)@ jquery.min.js:2我@ 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未捕获的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(异步)r.readyException @ jquery.min.js:2(匿名)@ jquery.min.js:2 j @ jquery.min.js:2 k @ jquery.min.js:2 setTimeout(异步)(匿名)@ jquery.min.js:2我@ 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(异步)(匿名)@ jquery.min.js:2我@ jquery.min.js:2 fireWith @ jquery.min.js:2 fire @ jquery.min.js:2我@ 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>
答案 0 :(得分:0)
我们昨天(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文件,从而解决了该问题。