【发布时间】:2017-10-20 15:31:44
【问题描述】:
我是 React Native 的新手,但对 React 非常熟悉。正如我在文档中看到的那样,作为初学者,我希望在云服务器和使用 websockets 的 react-native 之间建立连接。不幸的是,没有像样的例子可以帮助我。到目前为止,这就是我所拥有的一切:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Button
} from 'react-native';
export default class raspberry extends Component {
constructor(props) {
super(props);
this.state = { open: false };
this.socket = new WebSocket('ws://127.0.0.1:3000');
this.emit = this.emit.bind(this);
}
emit() {
this.setState(prevState => ({ open: !prevState.open }))
this.socket.send("It worked!")
}
render() {
const LED = {
backgroundColor: this.state.open ? 'lightgreen' : 'red',
height: 30,
position: 'absolute',
flexDirection: 'row',
bottom: 0,
width: 100,
height: 100,
top: 120,
borderRadius: 40,
justifyContent: 'space-between'
}
return (
<View style={styles.container}>
<Button
onPress={this.emit}
title={this.state.open ? "Turn off" : "Turn on"}
color="#21ba45"
accessibilityLabel="Learn more about this purple button"
/>
<View style={LED}></View>
</View>
);
}
componentDidMount() {
this.socket.onopen = () => socket.send(JSON.stringify({ type: 'greet', payload: 'Hello Mr. Server!' }))
this.socket.onmessage = ({ data }) => console.log(JSON.parse(data).payload)
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('raspberry', () => raspberry);
一切正常,但是当我按下按钮发送消息时,这是我得到的错误:
无法发送消息。未知的 WebSocket id 1
我还用一个 js 客户端进行了测试,一切都很顺利..看看我如何修复这个问题或一些我可以解决的示例源。
【问题讨论】:
-
你是不是不小心没有打开还没打开的socket?
-
我来自 socket.io 那里关闭套接字不是一件事,但是对于 ws 这真的可能是一件事。我回家后会尝试关闭它们,请给小费!
-
只是想知道,您不使用 socket.io 有什么原因吗?
-
收到一个未“验证的错误”,知道如何解决吗?
标签: reactjs react-native websocket