【问题标题】:Client side receive polling on socket.io客户端在 socket.io 上接收轮询
【发布时间】:2015-11-30 02:25:17
【问题描述】:

我的服务器上有以下代码

var socket  = require( 'socket.io' );
var express = require('express');
var app     = express();
var server  = require('http').createServer(app);
var io      = socket.listen( server );
var port    = process.env.PORT || 3000;

server.listen(port, function () {
  console.log('Server listening at port %d', port);
});


io.on('connection', function (socket) {

  socket.on( 'new_count_message', function( data ) {
    io.sockets.emit( 'new_count_message', { 
    	new_count_message: data.new_count_message

    });
  });

  socket.on( 'update_count_message', function( data ) {
    io.sockets.emit( 'update_count_message', {
    	update_count_message: data.update_count_message 
    });
  });

  socket.on( 'new_message', function( data ) {
    io.sockets.emit( 'new_message', {
    	name: data.name,
    	email: data.email,
    	subject: data.subject,
    	created_at: data.created_at,
    	id: data.id
    });
  });

  
});

但不是在控制台中显示“update_count_message”,而是控制台记录一个连续的轮询流。

	
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081140-15
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850080247-12
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850080252-13
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081252-16
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081290-17
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081351-18
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081416-19
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081474-20
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081532-21
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081576-22
GET http://localhost/socket.io/?EIO=3&transport=polling&t=1448850081651-23

我该如何解决这个问题? 我不想在浏览器中收到循环。

【问题讨论】:

  • 抱歉我的英语来自巴西。

标签: node.js socket.io


【解决方案1】:

浏览器/套接字未连接到 NodeJS。 NodeJS 要么没有运行,要么您尝试连接到错误的端口 - 在您的情况下,它看起来像是在尝试连接到端口 80。如果您已经在端口 80 上运行 Apache,则无法同时运行 NodeJS 和Apache 在同一端口上,除非您在 httpd.conf 中设置反向代理。

您可以按照此处的调试说明查看 SocketIO 可能对您有哪些更具描述性的错误:

http://socket.io/docs/logging-and-debugging/

您也可以在浏览器中访问此 URL,查看它尝试连接的页面:

http://localhost/socket.io/

【讨论】:

  • 当我访问页面localhost:3000/socket.io时出现如下:{"code":0,"message":"Transport unknown"}。
  • 我这样调用脚本: 有问题吗?
  • 你的 io.connect() 在客户端/浏览器端是什么样子的?例如,我的看起来像这样:var socket = io.connect('http://localhost:3000')
  • Clayton,谢谢,我居然忘记把端口放在客户端了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-23
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 2014-06-13
  • 2013-03-06
  • 2016-06-16
相关资源
最近更新 更多