gpt4 book ai didi

node.js - io 在客户端未定义

转载 作者:太空宇宙 更新时间:2023-11-04 01:42:05 24 4
gpt4 key购买 nike

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

errors: 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 exception: io is not defined ReferenceError: io is not defined at HTMLDocument. (http://localhost:1337/stylescript/pm.js:4:18) at j (https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js:2:29568) at 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) (anonymous) @ 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 defined at HTMLDocument. (pm.js:4) at j (jquery.min.js:2) at k (jquery.min.js:2) (anonymous) @ pm.js:4 j @ jquery.min.js:2 k @ jquery.min.js:2 setTimeout (async) r.readyException @ jquery.min.js:2 (anonymous) @ jquery.min.js:2 j @ jquery.min.js:2 k @ jquery.min.js:2 setTimeout (async) (anonymous) @ 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) (anonymous) @ 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

服务器端代码:

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>

最佳答案

我们昨天遇到了同样的问题(2018年9月29日-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 文件,这解决了问题。

关于node.js - io 在客户端未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com