我正在使用Rails5爲我的應用程序創建api提供程序,並且我需要使用websocket。未收到廣播
然後,我創建了測試一個非常簡單的代碼:
的routes.rb
Rails.application.routes.draw do
mount ActionCable.server => '/cable'
get '/testbroad', to: 'messages#testbroad'
end
messages_channel.rb
class MessagesChannel < ApplicationCable::Channel
def subscribed
stream_from 'messages'
end
end
messages_controller.rb
class MessagesController < ApplicationController
def testbroad
ActionCable.server.broadcast('messages',
message: 'foo')
head :ok
end
end
然後,我用捲曲
➜ ~ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Host: ws://localhost:3000/cable" http://localhost:3000/cable
HTTP/1.1 101 Web Socket Protocol Handshake
Upgrade: WebSocket
Connection: Upgrade
WebSocket-Location: ws://ws://localhost:3000/cable/cable
{"type":"welcome"}
這是工作開始的連接!但是,當我試圖發送廣播...
➜ ~ curl http://localhost:3000/testbroad
Rails的控制檯顯示成功,但我沒有收到任何消息在cURL中。爲什麼我沒有收到消息「foo」?如何解決它?
我發佈了一個答案,討論在這個問題上行動電纜如何工作。你能幫助我更好地理解這個問題嗎?謝謝 –