Rails ActionCable 和 React Native
Rails ActionCable and React Native
我正在开发一个 React Native 应用程序来配合我的 RoR 网络应用程序,并且想使用 ActionCable (websockets) 构建聊天功能。我无法让我的 React Native 应用程序与 ActionCable 通信。
我尝试了很多库,包括 react-native-actioncable,但都没有成功。初始连接似乎正常工作(我知道这一点是因为我之前遇到过错误,当我传递正确的参数时错误已经消失了)。
这是我的 React Native 代码的简化版本:
import ActionCable from 'react-native-actioncable'
class Secured extends Component {
componentWillMount () {
var url = 'https://x.herokuapp.com/cable/?authToken=' + this.props.token + '&client=' + this.props.client + '&uid=' + this.props.uid + '&expiry=' + this.props.expiry
const cable = ActionCable.createConsumer(url)
cable.subscriptions.create('inbox_channel_1', {
received: function (data) {
console.log(data)
}
})
}
render () {
return (
<View style={styles.container}>
<TabBarNavigation/>
</View>
)
}
}
const mapStateToProps = (state) => {
return {
email: state.auth.email,
org_id: state.auth.org_id,
token: state.auth.token,
client: state.auth.client,
uid: state.auth.uid,
expiry: state.auth.expiry
}
}
export default connect(mapStateToProps, { })(Secured)
任何有更多经验将 ActionCable 连接到 React Native 的人可以帮助我吗?
您附加到的 url 端点不是 websocket,所以这可能是您的问题。 The example app 他们列出的是 2 个月前更新的,基于 RN 0.48.3,所以我猜它可能仍然有效。您是否尝试过克隆并 运行 它?
看来您还需要设置提供商 ()
import RNActionCable from 'react-native-actioncable';
import ActionCableProvider, { ActionCable } from 'react-actioncable-provider';
const cable = RNActionCable.createConsumer('ws://localhost:3000/cable');
class App extends Component {
state = {
messages: []
}
onReceived = (data) => {
this.setState({
messages: [
data.message,
...this.state.messages
]
})
}
render() {
return (
<View style={styles.container}>
<ActionCable channel={{channel: 'MessageChannel'}} onReceived={this.onReceived} />
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<View>
<Text>There are {this.state.messages.length} messages.</Text>
</View>
{this.state.messages.map((message, index) =>
<View key={index} style={styles.message}>
<Text style={styles.instructions}>
{message}
</Text>
</View>
)}
</View>
)
}
}
export default class TestRNActionCable extends Component {
render() {
return (
<ActionCableProvider cable={cable}>
<App />
</ActionCableProvider>
);
}
}
我正在开发一个 React Native 应用程序来配合我的 RoR 网络应用程序,并且想使用 ActionCable (websockets) 构建聊天功能。我无法让我的 React Native 应用程序与 ActionCable 通信。
我尝试了很多库,包括 react-native-actioncable,但都没有成功。初始连接似乎正常工作(我知道这一点是因为我之前遇到过错误,当我传递正确的参数时错误已经消失了)。
这是我的 React Native 代码的简化版本:
import ActionCable from 'react-native-actioncable'
class Secured extends Component {
componentWillMount () {
var url = 'https://x.herokuapp.com/cable/?authToken=' + this.props.token + '&client=' + this.props.client + '&uid=' + this.props.uid + '&expiry=' + this.props.expiry
const cable = ActionCable.createConsumer(url)
cable.subscriptions.create('inbox_channel_1', {
received: function (data) {
console.log(data)
}
})
}
render () {
return (
<View style={styles.container}>
<TabBarNavigation/>
</View>
)
}
}
const mapStateToProps = (state) => {
return {
email: state.auth.email,
org_id: state.auth.org_id,
token: state.auth.token,
client: state.auth.client,
uid: state.auth.uid,
expiry: state.auth.expiry
}
}
export default connect(mapStateToProps, { })(Secured)
任何有更多经验将 ActionCable 连接到 React Native 的人可以帮助我吗?
您附加到的 url 端点不是 websocket,所以这可能是您的问题。 The example app 他们列出的是 2 个月前更新的,基于 RN 0.48.3,所以我猜它可能仍然有效。您是否尝试过克隆并 运行 它?
看来您还需要设置提供商 (
import RNActionCable from 'react-native-actioncable';
import ActionCableProvider, { ActionCable } from 'react-actioncable-provider';
const cable = RNActionCable.createConsumer('ws://localhost:3000/cable');
class App extends Component {
state = {
messages: []
}
onReceived = (data) => {
this.setState({
messages: [
data.message,
...this.state.messages
]
})
}
render() {
return (
<View style={styles.container}>
<ActionCable channel={{channel: 'MessageChannel'}} onReceived={this.onReceived} />
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<View>
<Text>There are {this.state.messages.length} messages.</Text>
</View>
{this.state.messages.map((message, index) =>
<View key={index} style={styles.message}>
<Text style={styles.instructions}>
{message}
</Text>
</View>
)}
</View>
)
}
}
export default class TestRNActionCable extends Component {
render() {
return (
<ActionCableProvider cable={cable}>
<App />
</ActionCableProvider>
);
}
}