React Native Socket IO Client,不调用Ack

时间:2015-11-18 20:00:46

标签: socket.io react-native

我正在使用带有Socket IO的React Native开发Android应用程序,并且我的应用程序上没有调用确认。 socket.emit正在工作,因为我正在调试服务器并且它接收到事件,但是ack没有在客户端上执行,这是我的代码。

'use strict';

var React = require('react-native');
var {
  StyleSheet,
  Text,
  View,
  TextInput,
  TouchableHighlight
  } = React;

var socket = require('../../common/socket/socket');

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    padding: 24
  },
  welcome: {
    padding: 8,
    fontSize: 20,
    textAlign: 'center',
    fontWeight: 'bold'
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5
  },
});

var LoginView = React.createClass({
  getInitialState : function getInitialState() {
    return {
      username  : '',
      password  : '',
      isLoading : false,
      error     : false,
      buttonText : 'Ingresar'
    };
  },
  loginAttempt : function loginAtempt () {;
    socket.emit('login-attempt',
      {
        username : this.state.username,
        password : this.state.password
      },
      // This is Not Working
      function (err, user, token) {
        // Just for testing purposes ...  
        this.setState({
          buttonText : 'derp'
        });
      }
    );
  },
  usernameChange : function usernameChange (event) {
    this.setState({
      username : event.nativeEvent.text
    });
  },
  passwordChange : function passwordChange (event) {
    this.setState({
      password : event.nativeEvent.text
    });
  },
  render : function render () {

    return (
      <View style={styles.container}>

        <Text style={styles.welcome}>
          Login
        </Text>
        <TextInput
          placeholder="Usuario"
          onChange={this.usernameChange}
          value={this.state.username}
        />
        <TextInput
          secureTextEntry={true}
          placeholder="Clave"
          onChange={this.passwordChange}
          value={this.state.password}
        />
        <TouchableHighlight
          onPress={this.loginAttempt}>
          <Text>{this.state.buttonText}</Text>
        </TouchableHighlight>
      </View>
    );
  }
});

module.exports = LoginView;

服务器代码(这是有效的,我已经在浏览器中测试过了)

var LoginController = function LoginController() {
  BaseController.call(this);
  this.name = 'LoginController';

  this.authenticate = function authenticate (username, password, callback) {

    if (typeof callback !== 'function') {
      throw new Error('callback is not a function');
    }

    User.findOne({
      where : {
        username : username
      }
    }).then(function (user) {

      var err = {
        code  : 0,
        msg   : ''
      };

      if (user !== null) {
        if (user.password !== password) {
          err = {
            code  : 1,
            msg   : 'password is incorrect'
          };
        }
      } else {
        err = {
          code  : 2,
          msg   : 'user not found'
        };
      }

      callback(err, user, 'token');

    }).catch(function (err) {
      callback(err, null);
    });

    return this;
  };

};

0 个答案:

没有答案