如果我尝试通过socket.io / redis发送消息,则不会收到任何消息,在这种情况下,我正在使用Laravel事件。 socket.js类创建服务器,如下所示:
<div class="form-group">
<div class="col-sm-12">
<div class="checkbox">
<label for="list1">
<input type="checkbox" name="item1" value="Gedaan" id="list1"/>
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="checkbox">
<label for="list2">
<input type="checkbox" name="item2" value="Gedaan" id="list2"/>
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="checkbox">
<label for="list3">
<input type="checkbox" name="item3" value="Gedaan" id="list3"/>Video
DVD
CD
</label>
</div>
</div>
</div>
我只是在ubuntu服务器上运行node socket.js。 我正在尝试接收消息并在event.js文件中显示如下通知:
var fs = require('fs');
var options = {
key: fs.readFileSync('/etc/nginx/ssl/mydomain.com/123/server.key')
cert: fs.readFileSync('/etc/nginx/ssl/mydomain.com/123/server.crt')
};
var app = require('https').createServer(options,handler);
var io = require('socket.io')(app);
var Redis = require('ioredis');
var redis = new Redis();
function handler(req, res) {
res.writeHead(200);
res.end('');
}
io.on('connection', function(socket) {});
// Channel if user signs up
var redisUserSignedUp = new Redis();
redisUserSignedUp.subscribe('signed-up-channel');
redisUserSignedUp.on('message', function(channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
// run server on port 8111
app.listen(8111, function () {
console.log('Der Server läuft!');
});
但是我没有输出,这是来自laravel的简单Event类:
// event.js if something happen some notification should appear
var url = window.location.origin.replace(/\/$/, "")+':8111';
var socket = io(url);
// ...
// UserSignedUp Event, is fired in Auth\RegisterController if a user signs up
socket.on('signed-up-channel:App\\Events\\UserSignedUp', (data) => {
this.signedUpUsers = data.username;
this.$toasted.info("Following User has registered: ", {
theme: "primary",
position: "top-right",
icon: {
name: 'chevron-right',
after: true,
},
iconPack: 'fontawesome',
duration: 10000
});
});
我从日志中得到以下输出:
<?php namespace App\Events; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class UserSignedUp extends Event implements ShouldBroadcast { use SerializesModels; public $username; public function __construct(String $username) { $this->username = $username; } public function broadcastOn() { return ['signed-up-channel']; } }
在本地Windows计算机上,如果发生该事件,我会收到正确的通知,因此我丢失了一些东西。