【问题标题】:not getting the values passed by the socket.io client没有得到 socket.io 客户端传递的值
【发布时间】:2014-12-30 19:56:27
【问题描述】:

我被困在socket.io 教程的入门部分,关于发射事件。在此之前,我确实在控制台中获得了 user connecteduser disconnected。但是,在发射部分,我没有收到控制台中 socket.io 客户端传递的任何 消息

这是目前为止的代码:

index.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){
  socket.on('chat message', function(msg){
    console.log('message: ' + msg);
  });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

index.html:

<!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>

  <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
  <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
  <script>
    var socket = io();
    $('form').submit(function(){
      socket.emit('chat message', $('#m').val());
      $('#m').val('');
      return false;
    });
  </script>

  </head>
  <body>
    <ul id="messages"></ul>
    <form action="">
      <input id="m" autocomplete="off" /><button>Send</button>
    </form>
  </body>
</html>

如果您能指导我解决这个问题,我将不胜感激。谢谢。

【问题讨论】:

    标签: javascript html node.js socket.io


    【解决方案1】:

    您的 index.js 中缺少一行

    代替:

    io.on('connection', function(socket){
      socket.on('chat message', function(msg){
        console.log('message: ' + msg);
      });
    });
    

    你应该有:

    io.on('connection', function(socket){
      socket.on('chat message', function(msg){
        console.log('message: ' + msg);
        io.emit('chat message', msg);
      });
    });
    

    发生的情况是,在 html 文件中,当您提交聊天消息时,您会将其发送回服务器,服务器需要将其发送回所有其他用户。如果没有该 io.emit 行,所有其他用户都不会收到消息

    *edit: 同样在你的 html 文件中,它应该是这样的:

    <!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>
      </body>
    
      <script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
      <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
      <script>
        var socket = io();
        $('form').submit(function(){
          socket.emit('chat message', $('#m').val());
          $('#m').val('');
          return false;
        });
       socket.on('chat message', function(msg){
          $('#messages').append($('<li>').text(msg));
        });
      </script>
    </html>
    

    它不起作用,因为您首先运行脚本,该脚本引用了一个尚不存在的 jQuery 元素。如果您想将正文保留在原处,则需要将脚本包装在 $(document).ready 函数中。此外,您忘记在传入的聊天消息套接字上附加消息。

    【讨论】:

    • 是的,但在此之前,即使向用户发出回传,它也应该在控制台中显示消息,但事实并非如此。我什至继续添加客户端 javascript 以获取服务器传递的消息。但运气不好,它既没有向控制台显示消息,也没有在浏览器端显示消息。
    • 澄清一下,控制台日志将显示在您的终端或您运行 index.js 的任何位置,而不是在您的浏览器中
    • 是的。我在终端中运行它,但它既没有在控制台中显示,也没有在浏览器中显示。 :(
    • 我发现了问题并更新了答案。基本上有一些 html 改组和缺少 socket.on 调用
    猜你喜欢
    • 2013-02-21
    • 2018-01-30
    • 2016-01-10
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多