我已经在RN应用程序上使用react-native-static-server创建了一个服务器。我想使用套接字连接到此服务器,并尝试了socket.io,react native tcp套接字有什么办法吗?我的服务器应用程序和客户端应用程序在同一部手机上。我希望这两个应用程序能够相互通信
我的静态服务器应用
import React, {useState, useRef, useEffect, useCallback} from 'react';
import StaticServer from 'react-native-static-server';
import RNFS from 'react-native-fs';
let path = RNFS.ExternalDirectoryPath + '/www';
const io = require('socket.io')();
import {Text} from 'react-native';
export default function App() {
useEffect(() => {
io.on('connection', client => {
let server = new StaticServer(8000, path, {localOnly: true});
server.start().then(url => {
console.log('Serving at URL', url);
});
});
io.listen(8000);
});
return <Text>hello</Text>;
}
1条答案
按热度按时间mepcadol1#
您应该使用React Nativetcp-socket https://github.com/Rapsssito/react-native-tcp-socket