2

这是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...但没有一种能奏效。

4

0 回答 0