【发布时间】:2018-12-11 05:10:23
【问题描述】:
我正在尝试制作一个能够将数据发送到 Node.js 服务器的简单 React Native 应用程序。
但我收到“网络请求失败”错误...
这是我第一次尝试实施这种项目,所以我可能犯了一个愚蠢的错误,但我已经 2 天没找到了
感谢您的帮助或建议!
这是我的应用代码:
import React from 'react'
import {Text, View, TextInput, Button, StyleSheet, ImageBackground, Image, TouchableOpacity} from 'react-native' //import des components natifs
import {StackNavigator } from 'react-navigation';
const io = require('socket.io-client');
let socket = io('https://localhost:8080');
class Connexion extends React.Component{
constructor(props){
super(props);
}
validationConnexion(){
fetch('https://localhost:8080', {
method: 'POST',
headers: {
Accept: 'text/plain',
},
}).catch((error) => {
console.error(error);
});
}
在后端:
const express = require('express');
const https = require('https')
const socketio = require('socket.io');
const app = express();
const server = https.Server(app, function(req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
res.end();})
const websocket = socketio(server);
server.listen(8080, () => console.log('listening on *:8080'));
// The event will be called when a client is connected.
websocket.on('connection', (socket) => {
console.log('A client just joined on', socket.id);
});
【问题讨论】:
标签: node.js react-native socket.io