Javascript 用户连接时未注册socket.io

Javascript 用户连接时未注册socket.io,javascript,node.js,sockets,socket.io,Javascript,Node.js,Sockets,Socket.io,我这里的问题是,当有人连接到页面时,它应该使用node.js在命令行上打印“已连接的用户”,但该消息从未显示,因此当用户访问页面时它不会注册(所有操作都在localhost中完成)。我将在下面发布一些代码,也许您可以找到问题并帮助我解决 JSON: HTML: 替换 <script src="/Users/kavehkhorram/Desktop/chat-example/node_modules/socket.io/node_modules/socket.io-client/socket

我这里的问题是,当有人连接到页面时,它应该使用node.js在命令行上打印
“已连接的用户”
,但该消息从未显示,因此当用户访问页面时它不会注册(所有操作都在localhost中完成)。我将在下面发布一些代码,也许您可以找到问题并帮助我解决

JSON:

HTML:

替换

<script src="/Users/kavehkhorram/Desktop/chat-example/node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>



这是节点模块(通过http)公开socket.io客户端API的正确(默认)url。

仍然没有成功,但在/socket.io目录下没有socket.io.js文件,只有index.js文件。我的原始源代码之所以这么长,是因为我只能在/socket.io/node_modules/socket.io-client/目录下找到socket.io.js。此文件夹中物理上不存在此文件,默认情况下,socket.io在该位置为其自身提供服务。如果您的路径有效,您是否尝试了
var socket=io.connect()
而不是
io()
?(检查您的控制台是否存在错误,如未定义IO)啊,我得到了它,在我的JS文件中,变量被称为“IO”(小写),但我在“index.html”文件中使用大写字母来调用它。更改该选项并添加“.connect()”似乎已经解决了我的问题!
<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>
    <style>
      * { margin: 0; padding: 0; box-sizing: border-box; }
      body { font: 13px Helvetica, Arial; }
      form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
      form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
      form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
      #messages { list-style-type: none; margin: 0; padding: 0; }
      #messages li { padding: 5px 10px; }
      #messages li:nth-child(odd) { background: #eee; }
    </style>
  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
    <script src="/Users/kavehkhorram/Desktop/chat-example/node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
    <script>var socket = IO();</script>
  </body>
</html>
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);


app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection',function(socket){
    console.log('a user connected');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});
<script src="/Users/kavehkhorram/Desktop/chat-example/node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>
<script src="/socket.io/socket.io.js"></script>