在使用websocket進行工作時,我非常喜歡與pusher一起工作。他們很簡單地設置。 它們都是免費的200K消息/天
這是heroku documentation對於推杆
1)安裝寶石
gem install pusher
2)安裝插件
heroku addons:create pusher:sandbox
3)設置您的初始化程序
#config/initializers/pusher.rb
require 'pusher'
Pusher.app_id = '000000'
Pusher.key = '000000000000000000'
Pusher.secret = '00000000000000000000'
Pusher.cluster = 'xxx'
Pusher.logger = Rails.logger
Pusher.encrypted = true
一旦你有你的地方鍵所有你需要的是在服務器上是真正創建一個客戶端(JS)和服務器
在客戶端的代碼是這樣的
<!DOCTYPE html>
<head>
<title>Pusher Test</title>
<script src="https://js.pusher.com/4.1/pusher.min.js"></script>
<script>
// Enable pusher logging - don't include this in production
Pusher.logToConsole = true;
var pusher = new Pusher("#{key}", {
cluster: "#{cluster}",
encrypted: true
});
var channel = pusher.subscribe('my-channel');
channel.bind('my-event', function(data) {
alert(data.message);
});
</script>
</head>
現在簡單
require 'pusher'
pusher_client = Pusher::Client.new(
app_id: ENV[:app_id],
key: ENV[:key],
secret: ENV[:secret],
cluster: ENV[:cluster],
encrypted: true
)
pusher_client.trigger('my-channel', 'my-event', {
message: 'hello world'
})
我希望這有助於出
你找到一個解決的辦法?我遇到了同樣的問題 – gwalshington