简体   繁体   English

在浏览器上运行套接字期间发生未捕获的错误(未捕获的ReferenceError:未定义io)

[英]Uncaught Error during socket run on browser ( Uncaught ReferenceError : io is not defined )

Recently I started to learn Nodejs. 最近,我开始学习Nodejs。 and presently working on a chat server. 目前正在聊天服务器上工作。 I know this question asked many times. 我知道这个问题问了很多遍。 I also have seen all solution but they can't solve my problem. 我也看到了所有解决方案,但他们不能解决我的问题。 when I run this HTML code in the browser then it shows Uncaught ReferenceError: io is not defined . 当我在浏览器中运行此HTML代码时,它显示出Uncaught ReferenceError:io未定义 any idea behind this error. 这个错误背后的任何想法。 I asked this question after spend 4 hours on this and not getting the solution. 我花了4个小时在这个问题上并没有找到解决方案后问了这个问题。

<!--  CODE RUN on my browser -->
<!DOCTYPE html>
<html>
<head>
<title>CharRoom</title>

<!-- below js file working correctly when I click on this then it open 
  socket.io.js file -->

<link src="/socket.io/socket.io.js">
<link 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
<script> 
 var socket = io('http://localhost:8000');
</script>
</head>
<body>
<h1>Welcome to chat Room</h1>
</body></html>



// code on my server
app = require('express')();
var server = http.Server(app);
var io = require('socket.io')(server);
io.sockets.on('connection', function(socket) {
    console.log("Esta");
});
server.listen(8000,function (req,res) {
console.log("Chat is running on port "+ 8000);  
});

Since you are loading a js script, replace 由于您正在加载js脚本,因此请替换

<link src="/socket.io/socket.io.js">

with

<script src="/socket.io/socket.io.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM