我正在尝试建立一个通知系统,但我不知道为什么代码不能正常工作...... redis会拍摄事件,并且会收到事件......在CMD中的节点server.js上“回归:
C:\xampp\htdocs\test>node socket.js
Listening on Port 3000
{"message":"teste","user":1} //returns when publish redis in controller.
控制器:
public function fire(){
$redis = LRedis::connection();
$data = ['message' => 'teste', 'user' => 1];
if($redis->publish('message', json_encode($data))){
return response()->json(['sucesss' => 1]);
} else {
return response()->json(['error' => 0]);
}
}
Template.blade.php:
<script>
var socket = io('http://localhost:3000');
//console.log(socket); IS CONNECTED...
socket.on('message', function (data) {
console.log(data);
});
</script>
socket.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('message', function(err, count) {
});
redis.on('message', function(channel, message) {
console.log(message);
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
CONSOLE.LOG
什么都不返回......
如果您帮我开展工作,我如何通知特定用户?例如,用户1创建链接,用户2评论链接,通知用户1 ......