简体   繁体   English

反应本机和 socket.io 节点不工作

[英]React native and socket.io node not working

Below is my react native component and node server.js code that I am trying to connect to my node websocket backend.下面是我试图连接到我的节点 websocket 后端的我的本机组件和节点 server.js 代码。

My react code is running on the same computer as the server.我的反应代码与服务器在同一台计算机上运行。 I have found so many varying answers on here, and github, none of which I can get working.我在这里和 github 上找到了很多不同的答案,但我都无法使用。

I also found this question which was never answered, and this question has an answer, which I cannot get working (was asked over a year ago)我也发现了这个问题,这是从来没有回答,而这个问题有答案,我不能获得工作(被要求在一年前)

I have found this article and tried to amend my code based on these guidelines but this did not work.我找到了这篇文章,并试图根据这些指南修改我的代码,但这没有用。

react code反应代码

import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';

const io = require('socket.io-client/socket.io');
let socket = io('http://localhost:3000');

export default class App extends React.Component {

    constructor(props) {
        super(props);
        console.log(socket);
    }

    render() {
        return (
            <View>
                <Text>Websocket</Text>
            </View>
        );
    }
}

server.js服务器.js

const express = require('express');
const http = require('http')
const socketio = require('socket.io');

const app = express();
const server = http.Server(app);
const websocket = socketio(server);
server.listen(3000, () => console.log('listening on *:3000'));

console.log(websocket)

// The event will be called when a client is connected.
websocket.on('connection', (socket) => {
  console.log('A client just joined on', socket.id);
});

I am using the following versions of packages我正在使用以下版本的软件包

"expo": "^16.0.0",
"react": "16.0.0-alpha.6",
"react-native": "^0.43.4",
"socket.io-client": "^1.7.3"

I believe it should be我相信应该是

const io = require('socket.io-client');

Which does work for me.这对我有用。

I remember running into these sorts of issues when using react native.我记得在使用 React Native 时遇到过这类问题。 A lot of socket.io tutorials (including the one on their page) assume you're using an older style of importing JS via script tags in an HTML doc.许多 socket.io 教程(包括他们页面上的教程)假设您使用的是通过 HTML 文档中的脚本标签导入 JS 的旧样式。 It looks like socket.io changed the namespace as I do remember it being socket.io-client/socket.io some time ago if memory serves...看起来 socket.io 改变了命名空间,因为我记得它是socket.io-client/socket.io前一段时间,如果有内存的话......

Your server code seems fine.您的服务器代码看起来不错。 Though you should check if you can connect to its socket via another client.尽管您应该检查是否可以通过另一个客户端连接到它的套接字。

Anyway try this for react native code.不管怎样,试试这个来反应本机代码。

Import by:进口方式:

import io from 'socket.io-client/socket.io'

In your component do:在您的组件中执行以下操作:

componentDidMount () {
    const socket = io(YOURURL, {
      transports: ['websocket']
    })

    socket.on('connect', () => {
      console.log("socket connected")
      socket.emit('YOUR EVENT TO SERVER', {})
      socket.on('EVENT YOU WANNA LISTEN', (r) => {
      })
    })

    socket.on('connect_error', (err) => {
      console.log(err)
    })

    socket.on('disconnect', () => {
      console.log("Disconnected Socket!")
    })
  }

此外,在您的服务器端,将const websocket = socketio(server)更改为const websocket = socketio.listen(server);

Thanks, Peter.谢谢,彼得。 In my opinion, the article you have found isn't helpful because you are trying to connect to the host that serves the page.在我看来,您找到的文章没有帮助,因为您正在尝试连接到为该页面提供服务的主机。 According to the example on socket.io page , try to make sure that this line works: const io = require('socket.io-client/socket.io');根据socket.io 页面上的示例,尝试确保此行有效: const io = require('socket.io-client/socket.io'); and connect without link: let socket = io();并在没有链接的情况下连接: let socket = io();

It worked for me, but only in the browser on the same computer.它对我有用,但只能在同一台计算机上的浏览器中使用。 I think when you will test on the device you have to specify your IP instead of localhost.我认为当您在设备上进行测试时,您必须指定您的 IP 而不是 localhost。

Good luck :)祝你好运 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM