当我运行我的react-native移动应用程序时,服务器说它连接到PostChannel.它会创建帖子,但是我在"收到"函数中没有得到任何响应,所以它不会实时更新.
这是我的反应原生代码:
import ActionCable from 'react-native-actioncable';
const APP = {};
APP.cable = ActionCable.createConsumer("http://localhost:3000/cable");
const addSocket = (dispatch) => {
const self = this;
this.subscription = APP.cable.subscriptions.create(
"PostChannel",
{
connected: () => {
console.log("connected: action cable");
},
disconnected: () => {
console.log("disconnected: action cable");
},
received: (data) => {
console.log('received', data); // no response here
dispatch(receivePost(data.post));
}
});
};
Run Code Online (Sandbox Code Playgroud)
Rails代码:
渠道
class PostChannel < ApplicationCable::Channel
def subscribed
stream_from "post_channel"
end
end
Run Code Online (Sandbox Code Playgroud)
模型
class Post < ApplicationRecord
validates :user_id, :venue_id, presence: true …Run Code Online (Sandbox Code Playgroud)