Websocket Rails JavaScript 不起作用


我使用 websocket rails

通道并从 sidekiq worker 触发它。 触发器会正确记录在 websocket-rails 日志中,如下所示,并在 StationSongs 之前删除<:

I [2014-12-31 16:19:28.788] [频道] [station10938

] #StationSongs _id: 54a41400446562680e17c102, 名称: "Fedde & Di-Rect Le Grand", 标题: "梦之舞 Vol.73 - 14 - 我们属于的地方", 信息: 无, 周: 1, 年份: 2014, 日期: 2014-12-31 15:19:28 UTC, station_id: 10938>

它像这样触发:

channel = "station" + station_id.to_s
WebsocketRails[:"#{channel}"].trigger(:new, stationsong)

然后我为频道站10938设置订阅,javascritp代码如下所示:

var dispatcher = new WebSocketRails('localhost/websocket');
var stationid = $('#songhistorylist').data("id"); // is 10938
var stationname = 'station' + String(stationid);
var channel = dispatcher.subscribe(stationname);
console.log(channel);
channel.bind('new', function (song) {
        console.log(song);
        console.log('a new song about ' + song.name + " - " + song.title + ' arrived!');
});

这只会打印出频道,而不会打印出其他任何内容,即使频道一直出现在日志中。

我做错了什么?

在行

WebsocketRails[:"#{channel}"].trigger(:new, stationsong) 上,删除 ":"。

它应该是:WebsocketRails["station#{station_id}"].trigger(:new, stationsong)在 JS 中,调度程序应如下所示: var dispatcher = new WebSocketRails(window.location.host + '/websocket')是因为您的服务器端口可能会更改。

您的服务器应该具有(假设stationsong设置正确):

channel = "station" + station_id.to_s
WebsocketRails["#{channel}"].trigger(:new, stationsong)

你的客户端JS应该有:

var dispatcher = new WebSocketRails(window.location.host + '/websocket')
var stationid = $('#songhistorylist').data("id"); // is 10938
var stationname = 'station' + String(stationid);
var channel = dispatcher.subscribe(stationname);
console.log(channel);
channel.bind('new', function (song) {
        console.log(song);
        console.log('a new song about ' + song.name + " - " + song.title + ' arrived!');
});

相关内容

  • 没有找到相关文章

最新更新