以下是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("broadcast-channel", function(err, count) {
});
redis.on('message', function(channel, message) {
const event = JSON.parse(message);
io.emit(channel + ':' + event.event, event.data);
});
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
这是客户端代码
var Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':3000'
});
Echo.channel("broadcast-channel")
.listen("FieldWasUpdated", function(e) {
console.log(e);
});
在chrome开发工具中,我看到事件来自websockets,但是从不调用console.log。我为io.emit
(event.event, channel, event.data
,channel, event
,etc...
)调用尝试了不同的格式,但没有一种格式有效。