1

ログイン後に各ユーザー用に作成されたCookieで作成されたユーザーセッションIDをソケットに接続しようとしています...どの方向や例でも素晴らしいでしょう...私はnodejs、express、redis、socketを使用しています。 io と mongodb を使用して、残りの db を処理します。

これがapp.jsファイルのコードです(ただし、一部は不明です)

var connect = require('connect');
var RedisStore = require('connect-redis')(express);
  var redis = require("redis").createClient();
  var io = require('socket.io');
  var notificationsN = io.listen(server);

app.use(express.session({
      secret: "secret sauce",
      store: new RedisStore({ host: 'localhost', port: 3000, client: redis })
  }));

//Not sure if below is correct actually... need to connect it with the current user... how do i go about doing that?

notificationsN.on('connection', function(client) {
    const subscribe = redis
    subscribe.subscribe('realtime');

    subscribe.on('message', function(channel, message) {
      client.send(message);
      console.log('message received from ' + channel + " : " + message);
    });

    client.on('message', function(msg) {
      console.log(msg);
    });

    client.on('disconnect', function() {
      console.log('disconnecting from redis');
      subscribe.quit();
    });

  });

これはクライアントです:

script(src='http://localhost:3000/socket.io/socket.io.js')
script.
            var socket = io.connect('http://localhost/3000/');

            socket.on('connect', function (data) {
                setStatus('connected');
                socket.emit('subscribe', { channel: 'realtime' });
            });

            socket.on('reconnecting', function(data) {
                setStatus('reconnecting');
            });

            socket.on('message', function(data) {
                console.log('received a message: ', data);
                addMessage(data);
            });

            function addMessage(data) {
                $('#notificationNumber').html(data);
            }

            function setStatus(msg) {
                console.log('connection status: ' + msg);
            }
4

1 に答える 1