如何用actioncable回复消息给发件人客户端?
how to reply message to the sender client with actioncable?
在 socket.io 中,您可以像这样回复发件人的消息:
socket.on('records', function(sheet_id){
records = Record.all
//send message to sender
socket.emit('records', records);
});
但在 rails:
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
使用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
在 socket.io 中,您可以像这样回复发件人的消息:
socket.on('records', function(sheet_id){
records = Record.all
//send message to sender
socket.emit('records', records);
});
但在 rails:
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
使用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