繁体   English   中英

为什么不起作用?

[英]Why doesn't work?

我在node.js和socket.io中是一个新手,当我尝试在控制台(带有节点app.js)中运行app.js时,它只是告诉我“正在听3000”,而当我输入index.html和在控制台中刷新它不会告诉我这里连接的用户是我的文件夹`` 这是app.js:

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');
 });

这是index.htm`

     <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>
       </body>
     </html>
     </html>
</html>`

我想要的是在进入页面(或刷新页面)时在控制台中“我已连接用户”中写我。

如果您继续阅读所遵循的同一教程,那么您将进入其中,因为它们包括frontend socket.io库。 因此,请继续阅读“集成Socket.IO”部分

链接: http//socket.io/get-started/chat/

暂无
暂无

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

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