2

在 socket.io 中,您可以向发件人回复消息,例如:

 socket.on('records', function(sheet_id){
    records = Record.all
    //send message to sender
    socket.emit('records', records);
  });

但在导轨中:

 class BoardChannel < ApplicationCable::Channel
   def subscribed
     stream_from "board:#{params[:board]}"
   end

   def speak
     # client will call @perform('speak')
     result = do_something()
     # how to send 'result' to sender?
   end
 end
4

1 回答 1

6

使用ActionCable::Connection::Base#transmit方法

class BoardChannel < ApplicationCable::Channel
  def subscribed
    stream_from "board:#{params[:board]}"
  end

  def speak
    result = do_something()
    transmit(result) # send message to current connection (sender)
  end
end
于 2016-07-18T16:15:18.483 回答