套接字IO不能发射

几天来,我一直在试图从这里得到socket.io聊天的例子。 首先,我通过自己input所有内容来尝试。 但它没有工作。 之后,我用github的文件试了一下。

那个部分

当我从github上得到这些文件后,我试了一下,我没有运行

npm install 

并下载了依赖到node_modules文件夹,我也跑了

  npm install --save express@4.10.2 npm install --save socket.io 

我没有任何的命令错误。

我开始与程序

 node index.js 

这给了我以下信息:

聆听*:30000

如果我去localhost:3000,我得到了聊天应用程序,如在示例中。 但发布表单时不会显示任何消息。

但是,当我使用下面的代码来检查连接:

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

它工作,所以问题是在发射数据。

另外,下面的代码工作:

服务器index.js

 io.on('connection', function(socket){ var tweet = {user: "nodesource", text: "Hello, world!"}; // to make things interesting, have it send every second var interval = setInterval(function () { socket.emit("tweet", tweet); console.log("tweeting"); }, 1000); socket.on("disconnect", function () { clearInterval(interval); }); }); 

客户

 socket.on("tweet", function(tweet) { // todo: add the tweet as a DOM node console.log("tweet from", tweet.username); console.log("contents:", tweet.text); }); 

所以发送和接收从服务器到客户端的工作。

我试过了

  • 在不同的浏览器中运行
  • 运行在不同的PC上
  • 从github复制
  • 自己input一切
  • 在Chrome中检查控制台是否有错误(未find错误)
  • 更改端口
  • 不同的path(使用c:\​​ chat,d:\ chat,c:\ personalfiles \ chat)
  • 禁用防火墙

什么工作

  • 当遵循本教程时,用户连接并在控制台上login的部分工作。

  • 我得到聊天页面

  • 控制台说,它听

  • 没有错误

有关如何解决这个问题的任何提示,或者如何find问题?

我的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> </head> <body> <ul id="messages"></ul> <form action=""> <input id="m" autocomplete="off" /><button>Send</button> </form> <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 src="/socket.io/socket.io.js"></script> <script> var socket = io(); $('form').submit(function(){ console.log('Send message'); socket.emit('chat message', $('#m').val()); $('#m').val(''); return false; }); socket.on('chat message', function(msg){ $('#messages').append($('<li>').text(msg)); }); </script> </body> </html> 

我的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){ io.emit('chat message', msg); }); }); 

试试这个(对于版本<= 0.8):

 io.configure(function () { io.set("transports", ["xhr-polling"]); io.set("polling duration", 10); }); 

对于版本1.0.x,它会是这样的:

 var io = require('socket.io').listen(http, { "transports": ['websocket', 'flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling', 'polling'], "polling duration": 10 }); 

你:

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

你确定这不应该是这个吗?

 io.sockets.on('connection', function (socket) { socket.on('chat message', function(msg){ io.sockets.emit('chat message', msg); }); });