socket io连接但不发送

时间:2015-07-05 10:21:49

标签: node.js redis socket.io

我有socket io redis和express pubsub正在进行中,在我的本地主机上工作正常我正在使用laravel事件发布到redis然后socket io应该发出消息,它在本地主机上工作正常但在我的生产服务器,这是一个ec2 vm它只能连接,我可以看到一个控制台消息'连接',但它不发出任何事件,即使消息发布到redis这里是我的客户端和服务器

//client

<script type="text/javascript">// <![CDATA[
            var socket = io.connect('http://127.0.0.1:3000/');

            socket.on('connect', function(data){
                //socket.emit('subscribe', {channel:'score.update'});
                console.log('connected');
            });

            socket.on('notification.update', function (data) {
                //Do something with data
                console.log('Notification Caught: ', data);

            });




// ]]></script>




//server
var express =   require('express'),
    http =      require('http'),
    server =    http.createServer(app);

var app = express();

const redis =   require('redis');
const io =      require('socket.io');
const client =  redis.createClient();

server.listen(3000, 'localhost');

io.listen(server).on('connection', function(client) {
    const redisClient = redis.createClient()
    redisClient.subscribe('notification.update');

 });
    redisClient.on("message", function(channel, message) {
        //Channel is e.g 'score.update'
        client.emit(channel, message);

    });

    client.on('disconnect', function() {
        redisClient.quit();
    });
});

1 个答案:

答案 0 :(得分:0)

您需要更改服务器的监听:

更改这些:

var express =   require('express'),
http =      require('http'),
server =    http.createServer(app);

var app = express();

const redis =   require('redis');
const io =      require('socket.io');
const client =  redis.createClient();

server.listen(3000, 'localhost');    

io.listen(server).on('connection', function(client){
    //Code
});

要:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

io.on('connection, function(client){
    //Code
});

server.listen(3000, function(){
    console.log("Listening on :3000");
});