我正在使用socket.io和node.js进行套接字连接,我使用下面的laravel代码通过API将数据发送到套接字。
在所有过程中,socket.js文件都能完美运行,该文件在带有节点socket.js的终端中运行。我只想在发布事件的同一函数上做出响应,是否可以这样做,或者我需要做其他事情。
注意:一切都很完美,只需要在我发布的同一功能中做出回应即可即可即可。
谢谢
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use App\Http\Requests;
use LRedis;
class MessageController extends Controller{
public function store(Request $request){
$data = [
'id'=>1,
'game_name'=>'PUB',
];
$redis = LRedis::connection();
$redis->publish('message', json_encode($data));
}
}
///////节点node.js
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');
server.listen(3000, function () {
console.log('Listening on Port 3000');
});
redisClient.subscribe('message');
redisClient.on("message", function (channel, data) {
const dataj = JSON.parse(data);
console.log(data);
///// send data to client which is connected with me
io.emit("eventsend", {
"id": dataj.id,
"game": dataj.game,
});
});
io.on('connection', function (socket) {
/////////////////////// send by client
socket.on("eventrec", function (data) {
console.log(data);
});
//console.log("Connected!");
/////////////////////// send by client
socket.on("success_repo", function (data) {
////////// i want this response in my laravel file where i publish the message event
console.log(data);
});
socket.on('disconnect', function () {
redisClient.unsubscribe('message');
});
});