小编fur*_*ogu的帖子

将 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'); …
Run Code Online (Sandbox Code Playgroud)

websocket node.js socket.io react-native

4
推荐指数
1
解决办法
3734
查看次数

标签 统计

node.js ×1

react-native ×1

socket.io ×1

websocket ×1