简体   繁体   English

socket.io发出所有套接字不起作用

[英]socket.io emit to all sockets not working

I have the following code: 我有以下代码:

/* WEB SERVER AND SOCKET */
var express = require("express");
var app = express();
var port = process.env.PORT || 3010;
var io = require('socket.io').listen(app.listen(port));


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

/* TWITTER STUFF */

t.track('hashtag');
t.track('sometag');

t.on('tweet', function (tweet) {
    console.log('Tweet received from ' + tweet.user.name + ' (@' + tweet.user.screen_name + '): ' + tweet.text);
    io.sockets.emit('tweet', {
        user: {
            name: tweet.user.name,
            screen_name: tweet.user.screen_name,
            profile_pic: tweet.user.profile_image_url
        },
        text: tweet.text,
        time: tweet.created_at,
        photo: tweet.media.media_url
    });
});

When running this, if I tweet using the hashtag, I get the log but no socket message is sent. 运行此命令时,如果我使用井号标签进行鸣叫,则会收到日志,但不会发送套接字消息。 Also, any following tweets do not do anything, suggesting that the emit function is hanging. 另外,随后的任何推文都没有执行任何操作,表明发出功能正在挂起。 In my log when starting the server, at the beginning I see: 在启动服务器的日志中,开始时我看到:

info  - socket.io started
info  - FlashPolicyFileServer received an error event: listen EADDRINUSE

However, connections can still be made. 但是,仍然可以建立连接。 Running netstat -tulpn | grep :3010 运行netstat -tulpn | grep :3010 netstat -tulpn | grep :3010 doesn't return anything, so I don't really get what the issue is. netstat -tulpn | grep :3010不返回任何内容,因此我并没有真正了解问题所在。 I'm running socket.io 0.9.16 and express 3.5.1. 我正在运行socket.io 0.9.16并表达3.5.1。

change this line 改变这条线

var port = process.env.PORT || 3010;

to

var port =8080; // for testing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM