简体   繁体   English

Socket.io和Redis Pub / Sub无法正常工作

[英]Socket.io and Redis Pub/Sub not working

Hello guys i would like to figure out what is the error in my code, my code is about socket.io and redis pub/sub it is my first time to try this, I hope you can help me guys. 大家好我想知道我的代码中有什么错误,我的代码是关于socket.io和redis pub / sub这是我第一次尝试这个,希望你能帮助我们。

This is my index.html 这是我的index.html

<!doctype html>
<html>
    <script src="/socket.io/socket.io.js"></script>
    <script> 
        var socket = new io.Socket();
        socket.connect();

        socket.on('connection', function (socket) {
            console.log('Connected');
        });

        socket.on('disconnect', function (socket) {
            console.log('Disconnected');
        });
    </script>
    <center>
    <h1>Test Page</h1>
    </center>
</html>

This is my app.js 这是我的app.js.

var redis = require('redis');
var app = require('http').createServer();
var io = require('socket.io').listen(app);
var client = redis.createClient();
var pub = redis.createClient();
var sub = redis.createClient();

app.listen(1234);

io.sockets.on('connection', function (socket){
    sub.on('subscribe', function (channel){
        pub.publish('Pub','Test Message 1');
        pub.publish('Pub','Test Message 2');
        pub.publish('Pub','Test Message 3');
    });
    sub.on('message', function (channel, message) {
        console.log(channel + ':' + message);
    sub.unsubscribe();
    pub.end();
    sub.end();
    });
    sub.incr('Channel Test');
    sub.incr('Pub');
});

I hope you can help me fix this code. 我希望你能帮我解决这个问题。 Thanks in advance guys. 先谢谢你们。

I can see many errors in your code : 我可以在你的代码中看到很多错误:

  • in index.html, you should connect to http://localhost:1234/ , because it's defined in your server code. 在index.html中,您应该连接到http://localhost:1234/ ,因为它是在您的服务器代码中定义的。
  • var client is not used in app.js app var client未在app.js中使用
  • sub is never subscribing to something. sub永远不会订阅某些东西。 You need to subscribe to a channel 您需要订阅频道
  • A connection in subscriber mode can not send commands to redis : only commands that modify the subscription set are valid subscriber mode的连接无法向redis发送命令:只有修改订阅集的命令才有效
  • sub.incr will never publish a message : you have to call publish. sub.incr永远不会发布消息:你必须调用publish。
  • do not call pub.end() or sub.end() because the connection will be closed. 不要调用pub.end()sub.end()因为连接将被关闭。
  • do not add an handler to event message under connection event : memory leak 不要在连接事件下向事件message添加处理程序:内存泄漏

I don't know exactly what do you want to do but here is an updated version : 我不知道你想要做什么,但这里是一个更新版本:

index.html 的index.html

<!doctype html>
<html>
    <script src="http://localhost:1234/socket.io/socket.io.js"></script>
    <script>
        var socket = io.connect('http://localhost:1234/');

        socket.on('connection', function (socket) {
            console.log('Connected');
        });

        socket.on('disconnect', function (socket) {
            console.log('Disconnected');
        });
    </script>
    <center>
    <h1>Test Page</h1>
    </center>
</html>

app.js app.js

var redis = require('redis');
var app = require('http').createServer();
var io = require('socket.io').listen(app);
var pub = redis.createClient();
var sub = redis.createClient();

app.listen(1234);

sub.subscribe('Pub');//subscribe to Pub channel
sub.on('message', function (channel, message) {
    console.log(channel + ':' + message);
});

io.sockets.on('connection', function (socket) {
    pub.publish('Pub', 'New Connection');
    pub.incr('Channel Test');   //increment 'Channel Test' but do not publish messages
    pub.incr('Pub');            //increment 'Pub' but do not publish messages
});

I didn't see you set redis as store in socket.io server. 我没有看到你在socket.io服务器中将redis设置为store。 Some example I did for your reference: 我做的一些例子供你参考:

// start up express server along with socket.io
var express = require('express');
var server = express();
var socket = require('socket.io');
var io = socket.listen(server);

// socket.io
io.set('store', new socket.RedisStore);

// set-up connections...
io.sockets.on('connection', function(socket) {

        io.emit('an event sent to all connected clients');

        socket.on('some-event', function(rooms) {
            ...
        });

});

You can read document links below: 您可以阅读以下文档链接:

  1. Configuring Socket.IO 配置Socket.IO
  2. socket.io github site socket.io github网站

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

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