fur*_*ogu 4 websocket node.js socket.io react-native
我正在尝试完成作业,我需要为此使用 websocket。我使用 react native 作为客户端,使用 node.js 作为服务器。但是,即使我尝试了网络上的所有解决方案建议,也无法使其正常工作。
客户
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import SocketIO from 'socket.io-client';
const connectionConfig = {
jsonp: false,
reconnection: true,
reconnectionDelay: 100,
reconnectionAttempts: 100000,
transports: ['websocket'], // you need to explicitly tell it to use websockets
};
export default class App extends React.Component {
constructor() {
super();
this.socket = SocketIO('http://localhost:3000',connectionConfig);
this.socket.on('connect', () => {
console.log('connected to server');
});
}
Run Code Online (Sandbox Code Playgroud)
服务器
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const port = 3000;
var app = express();
var server = http.createServer(app);
var io = socketIO(server);
server.listen(port, () => {
console.log(port, 'is up');
});
io.on('connection', (socket) => {
console.log('new user connected');
socket.on('disconnect', () => {
console.log('User was disconnected');
});
});
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的版本,
socket.io:2.1.1
快递:4.16.4
socket.io 客户端:2.1.1
世博会:30.0.1
我想提前感谢您所做的一切努力。
我已经找到了解决我的问题的方法。
this.socket = SocketIO(' http://localhost:3000 ',connectionConfig);
我从函数调用中省略了 connectionConfig 并更改了我将本地主机写入的方式。
this.socket = SocketIOClient(' http://192.168.xxxx:3000 ');
它现在适用于 android 和 iOS。
归档时间: |
|
查看次数: |
3734 次 |
最近记录: |