我正在尝试完成作业,我需要为此使用 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)