Node.js 插座IO can';不发射
几天来,我一直在尝试让socket.io聊天示例从中运行。首先,我试着自己打字。但它不起作用。在那之后,我尝试了它与文件从 角色 在我从github获得文件后,我尝试了它,但它不起作用,我已经运行了Node.js 插座IO can';不发射,node.js,sockets,Node.js,Sockets,几天来,我一直在尝试让socket.io聊天示例从中运行。首先,我试着自己打字。但它不起作用。在那之后,我尝试了它与文件从 角色 在我从github获得文件后,我尝试了它,但它不起作用,我已经运行了 npm install 它将依赖项下载到node_modules文件夹中,我还运行了 npm install --save express@4.10.2 npm install --save socket.io 我在任何命令上都没有错误 我从 node index.js 这给了我以下信息
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');
});
它是有效的,所以问题在于发送数据
此外,以下代码也起作用:
server 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复制
- 我自己打字
- 检查Chrome中的控制台是否有错误(未发现错误)
- 更换端口
- 不同的路径(使用c:\chat、d:\chat、c:\personalfiles\chat)
- 禁用防火墙
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);
});
});
- 在学习本教程时,用户连接并登录控制台的部分将起作用
- 我得到了聊天页面
- 控制台说它在听
- 没有错误
<!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>
试试这个(对于您的版本:
你确定不应该是这个吗
io.sockets.on('connection', function (socket) {
socket.on('chat message', function(msg){
io.sockets.emit('chat message', msg);
});
});
所以,我假设,控制台并没有说,用户已连接?不,它什么也不做,只是说它正在监听。@yergoI在这种情况下,我请求您的
index.js
文件以及中提到的index.html
文件。我嗅到有什么遗漏了。@yergo添加了它们,我试图将脚本标记放在头部,但没有做任何更改不同之处。目前它是一个开头提到的github文件的精确副本。讨厌它。复制,npm安装,启动和…工作。不告诉用户已连接(因为在该示例中它未实现)但是消息从一个选项卡发送到另一个选项卡,我确实可以聊天……我有旧版本,更新过。至少尝试一下,或者使用不同的轮询方法——似乎这只是windows的问题,我无法在上复制它*nixNo,检查我从官方socket.io网站发布到教程的链接。你的代码几乎与Sams中的代码相同教你自己NGeorge Ornbo的ode.js,24个一小时的课程。例如,第13小时,示例02。代码几乎相同。我通读了整个章节,效果非常好。我所展示的代码是我能看到的唯一显著差异。如果你想做更多的研究,请参阅本书第218页。
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);
});
});