gpt4 book ai didi

sockets - 检查是否有人在Laravel Controller的套接字 channel 上进行了连接

转载 作者:行者123 更新时间:2023-12-03 11:54:51 29 4
gpt4 key购买 nike

我有一个这样的结构:

套接字服务器端:

    var fs = require('fs');
var pkey = fs.readFileSync('../../../../etc/letsencrypt/privkey.pem');
var pcert = fs.readFileSync('../../../../etc/letsencrypt/cert.pem')
var options = {
key: pkey,
cert: pcert,
requestCert: false,
rejectUnauthorized: false
};
var app = require('https').createServer(options);
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
app.listen(8443, function () {
console.log('Server is running!');
});

redis.subscribe('resumo');

redis.on('message',function(channel,message){
console.log(channel, message);
message=JSON.parse(message);

console.log(channel+':'+message.event, message);
io.emit()
});

function handler(req, res) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.writeHead(200);
res.end('');
}

var totalUsers = 0;

io.on('connection', function (socket) {
totalUsers++;
console.log('a user connected');
io.emit('users', totalUsers);
socket.on('disconnect', function(){
totalUsers--;
io.emit('users', totalUsers);
console.log('user disconnected');
});
socket.on('exit', function(channel){
var teste = socket.eventNames();
console.log('desconectado de: '+teste);
});
socket.on('entrar', function(channel){
socket.join(channel);
console.log('conectado em: '+channel);
});
});
redis.psubscribe('*', function (err, count) {
//
});
redis.on('pmessage', function (subscribed, channel, message) {
io.emit(channel, message);
});

客户端:
    var socket = io.connect('https://mywebsite.com:8443/');
socket.on('u_123', function (data) {
data = JSON.parse(data);
window[data['function']](data);
});

我在这样的 channel 上发布:
    Redis::publish('u_123', json_encode([
'data' => [somedata]
]));

我需要检查laravel Controller 的此 channel “u_123”上是否有人连接,这可能吗?我该怎么办?我在javascript方面真的很不好,所以我在这方面遇到了很多困难...

最佳答案

根据socket.IO文档

Each Socket in Socket.IO is identified by a random, unguessable, unique identifier Socket#id. For your convenience, each socket automatically joins a room identified by this id.



因此,您可以将这些id存储在 connection事件的数组中,并在 exit上将其从列表中删除。

关于sockets - 检查是否有人在Laravel Controller的套接字 channel 上进行了连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54771494/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com