这是我的Routes.php文件。
Route::get('hello1',function(){
Redis::publish('test-channel', json_encode(['foo' => 'bar']));
});
这是我的app.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();
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
redis.subscribe('test-channel', function () {
console.log('Redis: test-channel subscribed');
});
redis.on('message', function(channel, message) {
console.log('Redis: Message on ' + channel + ' received!');
console.log(message);
message = JSON.parse(message);
io.emit(channel, message.payload)
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function(){
console.log('user disconnected');
});
});
http.listen(6379, function(){
console.log('listening on *:6379');
});
my index.html
<!doctype html>
<html>
<head>
<title>Socket.IO</title>
</head>
<body>
<ul id="messages">
<li>Hardcoded Message</li>
</ul>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
socket.on("test-channel", function(message) {
console.log(message);
$('#messages').append($('<li>').text(message));
});
</script>
</body>
</html>
现在无论是运行 php artisan redis:subscribe 还是 hello1 路由,我都会遇到如下错误:
AbstractConnection.php 文件的第 146 行 ConnectionException 错误:从服务器读取行时出错。[tcp://localhost:6379]