2016-04-01 16 views
0

Resmi socket.io eğiticisini takip etmeye çalışıyorum ancak iletileri almak için sunucuyu alamıyorum. Her türlü şeyi denemek için birkaç saat harcadım. Tamamen fikirlerim tükendi.Socket.io sunucusu ileti alamıyor

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

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

ve istemci: istemci sunucudan gelen mesajları alabilir ama tersi çalışmıyor

<!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="/socket.io/socket.io.js"></script> 
    <script src="http://code.jquery.com/jquery-1.11.1.js"></script> 
    <script> 
     //localStorage.debug = '*'; 
     var socket = io("http://localhost:3000/"); 
     socket.on('greeting', 
      function (msg) 
      { 
       console.log(msg); 
      } 
      ); 
     /* 
     $('form').submit(function() 
     { 
      socket.emit('chat message', $('#m').val()); 
      //$('#m').val(''); 
      return false; 
     }); 
     */ 
     $("#sendBtn").click(function() 
     { 
      socket.emit('chat', $('#m').val()); 
      return false; 
     }); 
    </script> 
</head> 
<body> 
    <ul id="messages"></ul> 
    <form action=""> 
     <input id="m" autocomplete="off" /><button id="sendBtn">Send</button> 
    </form> 
</body> 
</html> 

İşte sunucu var. Hata ayıklamayı açmayı denedim, ancak gördüğüm şeyin doğru ya da yanlış olup olmadığını anlayamıyorum. Ancak, web sayfasında gönderim yaptığımda kesinlikle bir etkileşim oluyor.

Burada istemci tarafında nasıl göründüğünü var:

Sen elemanı bağlamak düzeltmek gerekir

enter image description here

cevap

1

, sen henüz oluşturulmamışsa bir öğeye bağlamak çalışıyoruz. DOM'ın hazır olmasını beklemelisin. Bunu dene. sayfa Vay

+0

yüklendiğinde

$(function() { $("#sendBtn").on('click', function() { socket.emit('chat', $('#m').val()); return false; }); }); 

sadece yürütmeye söylüyorum bu gibi işlevi vardır tamamlayan. Onu görmedim! Çok teşekkür ederim Eduardo! –