index.js 3.0 KB
Newer Older
ItbGcthate's avatar
ItbGcthate 已提交
1
import React, {Component} from 'react';
ItbGcthate's avatar
ItbGcthate 已提交
2
import {View, StyleSheet, Dimensions, Image, ToastAndroid} from 'react-native';
ItbGcthate's avatar
ItbGcthate 已提交
3 4
import Img1 from './img/img1.png';
import Img2 from './img/img2.png';
ItbGcthate's avatar
ItbGcthate 已提交
5
import MyButton from '../MyButton';
ItbGcthate's avatar
ItbGcthate 已提交
6 7
import {login, logout, connect} from '../MyTest';
import {websocket} from '../MyUtilities';
ItbGcthate's avatar
ItbGcthate 已提交
8 9 10 11 12 13 14 15

const {width, height} = Dimensions.get('window');

class TwoPlayerPKHome extends Component {
  constructor() {
    super();
  }

ItbGcthate's avatar
ItbGcthate 已提交
16 17 18 19
  componentDidMount() {
    login();
  }

ItbGcthate's avatar
ItbGcthate 已提交
20 21
  componentWillUnmount() {
    this.cancelSearching();
ItbGcthate's avatar
ItbGcthate 已提交
22 23 24 25 26 27
    // 卸载异步操作设置状态
    this.setState = (state, callback) => {
      return;
    };
    websocket.disconnect();
    logout();
ItbGcthate's avatar
ItbGcthate 已提交
28 29
  }

ItbGcthate's avatar
ItbGcthate 已提交
30
  state = {isStarted: false, isCanceling: false};
ItbGcthate's avatar
ItbGcthate 已提交
31

ItbGcthate's avatar
ItbGcthate 已提交
32 33 34 35 36
  resolveMessage = msg => {
    const data = JSON.parse(msg.data);
    console.log(data);
    if (false) {
      const opponent = null;
ItbGcthate's avatar
ItbGcthate 已提交
37
      //this.props.navigation.navigate('双人PK结算');
ItbGcthate's avatar
ItbGcthate 已提交
38
      this.props.navigation.navigate('双人PK中', {opponent: opponent});
ItbGcthate's avatar
ItbGcthate 已提交
39 40 41 42 43
      this.setState({isStarted: false, isCanceling: false, opponent: null});
    }
    if (data.hasOwnProperty('type') && data.type === 'MATCH_CANCEL_SUCCESS') {
      this.setState({isStarted: false, isCanceling: false, opponent: null});
      websocket.disconnect();
ItbGcthate's avatar
ItbGcthate 已提交
44
    }
ItbGcthate's avatar
ItbGcthate 已提交
45 46
  };

ItbGcthate's avatar
ItbGcthate 已提交
47 48 49
  joinMatch = () => {
    this.setState({isStarted: true, isCanceling: false});
    console.log('websocket 已打开');
ItbGcthate's avatar
ItbGcthate 已提交
50
    // console.log(websocket.get().readyState);
ItbGcthate's avatar
ItbGcthate 已提交
51 52 53 54 55 56 57 58
    websocket.joinMatch();
  };

  serverOnerror = () => {
    console.log('websocket 发生了错误');
    ToastAndroid.show('服务器发生了错误', ToastAndroid.SHORT);
  };

ItbGcthate's avatar
ItbGcthate 已提交
59
  startSearching = () => {
ItbGcthate's avatar
ItbGcthate 已提交
60 61 62 63 64 65 66
    console.log();
    // connect();
    websocket.connect({
      onopen: this.joinMatch,
      onmessage: this.resolveMessage,
      onerror: this.serverOnerror,
    });
ItbGcthate's avatar
ItbGcthate 已提交
67 68
  };

ItbGcthate's avatar
ItbGcthate 已提交
69
  cancelSearching = () => {
ItbGcthate's avatar
ItbGcthate 已提交
70 71
    this.setState({isCanceling: true});
    websocket.cancelJoining();
ItbGcthate's avatar
ItbGcthate 已提交
72 73
  };

ItbGcthate's avatar
ItbGcthate 已提交
74
  render() {
ItbGcthate's avatar
ItbGcthate 已提交
75
    const {isStarted, isCanceling} = this.state;
ItbGcthate's avatar
ItbGcthate 已提交
76 77
    return (
      <View style={styles.container}>
ItbGcthate's avatar
ItbGcthate 已提交
78 79
        <Image source={isStarted ? Img2 : Img1} style={styles.image} />
        {isStarted ? (
ItbGcthate's avatar
ItbGcthate 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92
          isCanceling ? (
            <MyButton
              containerStyle={styles.myButton.container}
              title="取消中"
              disabled={isCanceling}
            />
          ) : (
            <MyButton
              containerStyle={styles.myButton.container}
              title="取消匹配"
              onPress={this.cancelSearching}
            />
          )
ItbGcthate's avatar
ItbGcthate 已提交
93 94 95 96 97 98 99
        ) : (
          <MyButton
            containerStyle={styles.myButton.container}
            title="开始匹配"
            onPress={this.startSearching}
          />
        )}
ItbGcthate's avatar
ItbGcthate 已提交
100 101 102 103 104 105 106 107 108 109 110
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    backgroundColor: '#F2E2CE',
    //opacity: 0.5,
    flex: 1,
  },
ItbGcthate's avatar
ItbGcthate 已提交
111 112 113
  image: {
    width: null,
    height: null,
ItbGcthate's avatar
ItbGcthate 已提交
114 115 116
    flex: 3,
  },
  myButton: {
ItbGcthate's avatar
ItbGcthate 已提交
117 118 119
    container: {
      flex: 1,
    },
ItbGcthate's avatar
ItbGcthate 已提交
120 121 122 123
  },
});

export default TwoPlayerPKHome;