簡體   English   中英

websocket rails javascript無法正常工作

[英]websocket rails javascript not working

我使用websocket rails通道並從sidekiq worker觸發它。 像這樣,將觸發器正確記錄在websocket-rails日志中,並在StationSongs之前刪除<

我[2014-12-31 16:19:28.788] [頻道] [station10938] #StationSongs _id:54a41400446562680e17c102,名稱:“聯邦大劇院”,標題:“夢幻舞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)

然后,我設置為頻道station10938的訂閱,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!');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM