简体   繁体   English

react-native socket.io 发射/打开不起作用

[英]react-native socket.io emit / on doesn't work

Hello guys I studying a simple chat app with RN大家好,我正在与 RN 学习一个简单的聊天应用程序

I checked connection with connection event at Server code Also server can get my socket's id我在服务器代码中检查了与连接事件的连接服务器也可以获取我的套接字的 ID

But, Any Emit or On doesn't work.但是, Any Emit 或 On 不起作用。

Here's my server Code这是我的服务器代码

var http = require('http');
var express = require('express'),
app = module.exports.app = express();
console.log("serverStarted");
var server = http.createServer(app);
const io = require('socket.io')(server);
server.listen(3000);  //listen on port 80
io.on('connection',function(socket){
    console.log("Client Connected."+socket.id);
    socket.on('Button',function(data){
        console.log("ButtonPressed");
    });
    socket.emit('userid',socket.id);
});

And this is Client side code这是客户端代码

import React, { Component } from 'react';
import {
  Button,
  Alert,
  Platform,
  StyleSheet,
  Text,
  View
} from 'react-native';
import SocketIOClient from 'socket.io-client';

let socket;
type Props = {};
export default class App extends Component<Props> {
    constructor(){
    super();
    socket = SocketIOClient('http://myip:3000');
    Alert.alert("Socket is Connected.");
    socket.on('userid',(id)=>{
      this.setState({userid:{id}});
      Alert.alert(id);
    })
  }

  state = {
    userid:"id"
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
        <Button title = "pres" onPress={()=>{
    socket.emit('Button',"button");
    }}>
          </Button>
        <Text style={styles.instructions}>
          {this.state.userid}
        </Text>
        <Text style={styles.instructions}>
          {instructions}
        </Text>
      </View>
    );
  }
}

With this code, When I press Button I think that the socket.on Works in Server使用此代码,当我按下 Button 时,我认为 socket.on 在服务器中有效

Also when socket is connected in server, the client change the Text in Render.同样,当套接字在服务器中连接时,客户端会更改渲染中的文本。

But both doesn't work.但两者都不起作用。

Please help me...请帮我...

I Found Problem.....我发现问题.....

The Socket.io is updated so I need to replace Socket.io 已更新,因此我需要更换

import io from 'socket.io-client' 

to

import io from 'socket.io-client/dist/socket.io';

First of all goto you project root directory and install首先转到你的项目根目录并安装

npm i socket.io-client npm i socket.io-client

import SocketIOClient from 'socket.io-client';


constructor(props) {

    super(props);

    //use your own local ip
     this.socket = SocketIOClient('http://localhost:9000/');

     this.socket.on('response', (messages) => {
      Alert.alert("response." + JSON.stringify(messages));
      console.log("response" + JSON.stringify(messages));
    });
  }
  componentDidMount(){
      this.socket.emit('Request_name', '1');
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM