我正在将socket.io用于我的Web应用程序的实时评论系统。 我的前端javascript代码是这个
let socket = io();
//when a comment is received from the server
socket.on("comment", function(commentObject){
console.log(commentObject);
});
function sendCommentToServer(name, email, message){
let commentObject = {"name" :name,"email" : email ,"message" : message};
socket.emit("comment", commentObject);
}
window.onload = function () {
sendCommentToServer("vishal", "vish@gmail.com", "hello there");
};
我正在使用node.js作为后端 这是我的index.js文件的一些代码
const http = require('http');
const socketServer = http.Server(app);
const Socket = require('socket.io');
const io = Socket(socketServer);
io.on('connection', function(socket){
console.log('a user connected');
socket.broadcast.emit('hi');
socket.on('disconnect', function () {
console.log("user disconnected");
});
socket.on('comment', function (commentObject) {
//sending the comment received to the rest of the users
socket.emit("comment", commentObject);
})
});
socketServer.listen(port, function () {
console.log("Listening at port " + port);
});
我还没有在此处发布整个代码。这里只是套接字相关的代码。除此之外,我使用快递。我正在提供包含index.html和script.js(前端js)的Public文件夹。 一切正常。 但是,我仍然想问为什么控制台中会出现此错误。