几天来,我一直在尝试让 socket.io 聊天示例从 here 开始工作。 。首先我尝试自己输入所有内容。但这没有用。之后我尝试了 github 中的文件。 。
部分
当我从 github 获取文件后,我尝试了它不起作用,我已经运行了
npm install
它将依赖项下载到node_modules文件夹中,我也运行了
npm install --save <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f5908d8587908686b5c1dbc4c5dbc7" rel="noreferrer noopener nofollow">[email protected]</a>
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);
});
因此从服务器向客户端发送和接收是可行的。
我尝试过的
- 在不同的浏览器中运行
- 在不同的电脑上运行
- 从 github 复制
- 我自己输入所有内容
- 检查 Chrome 控制台是否有错误(未发现错误)
- 更改端口
- 不同的路径(使用 c:\chat 、 d:\chat 、 c:\personalfiles\chat )
- 禁用防火墙
什么有效
按照教程进行操作后,用户连接并登录控制台的部分即可工作。
我进入聊天页面
控制台显示正在监听
没有错误
有关如何解决此问题或如何查找问题的任何提示?
我的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
});
关于node.js - Socket IO 无法发出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29079907/