Advertisement
PlotnikovPhilipp

Untitled

Nov 1st, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export class Chatting extends Component {
  2.     constructor(props) {
  3.         super(props);
  4.         this.socket = new WebSocket('ws://192.168.1.2:4000/message.php');
  5.         this.socket.onopen = function() {
  6.             alert("Соединение установлено.");
  7.           };
  8.          
  9.         this.socket.onclose = function(event) {
  10.             if (event.wasClean) {
  11.               alert('Соединение закрыто чисто');
  12.             } else {
  13.               alert('Обрыв соединения'); // например, "убит" процесс сервера
  14.             }
  15.             alert('Код: ' + event.code + ' причина: ' + event.reason);
  16.             alert(event);
  17.           };
  18.          
  19.         this.socket.onmessage = function(event) {
  20.             alert("Получены данные " + event.data);
  21.           };
  22.          
  23.         this.socket.onerror = function(error) {
  24.             alert("Ошибка " + error.message);
  25.           };
  26.         this.inputValue = null;
  27.         this.state = {data: []};
  28.     }
  29.     render() {
  30.         return(
  31.         <View style={styles.general}>
  32.             <FlatList
  33.                 data={this.state.data}
  34.                 style={styles.list}
  35.                 keyExtractor={(item, index) => index.toString()}
  36.                 renderItem={({item}) => {
  37.                     return(
  38.                         <View>
  39.                            
  40.                         </View>
  41.                     );
  42.                 }}
  43.             />
  44.             <View>
  45.                 <TextInput onChangeText={(data) => this.inputValue = data} placeholder={'Введите сообщение'} placeholderTextColor={'gray'} style={styles.input}></TextInput>
  46.                 <TouchableNativeFeedback onPress={() => {this.socket.send(this.inputValue)}}>
  47.                     <View style={styles.button}>
  48.                         <Text style={{textAlign: 'center', fontWeight: 'bold', fontSize: 16}}>Отправить</Text>
  49.                     </View>
  50.                 </TouchableNativeFeedback>
  51.             </View>
  52.         </View>)
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement