javascript - GatsbyJS 中的 Socket IO 客户端不起作用

标签 javascript node.js reactjs socket.io gatsby

friend 们大家好,我是新来的,我发现了一个难题,我在 React js 中使用了 Socket IO Client,没有任何问题,与我在 Gatsby 中使用的语法相同,我发现了这个问题。我的 Node.js 服务器无法检测到新客户端何时加入。

这是我的脚本。

React JS 组件(有效)

import React, { useEffect } from 'react';
import './App.css';

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

const socketEndPoint = 'http://localhost:32/';

const ioSocket = socketIOClient(socketEndPoint); 

// nameSpace: public_chat
const ns= 'public_chat';
const salaSocket = socketIOClient(socketEndPoint + ns)
const messageKey = 'test_message'; // Message id

function App() {
  function sendMessage(){
    salaSocket.emit(messageKey, 'Prueba de mensaje desde el cliente: '+Math.random())
  }

  function recepMessage(){
    salaSocket.on(messageKey, (received) => {
      alert('Message: '+ received)
    })
  }

  useEffect(() => recepMessage() , [])

  return (
    <div className="App">
      <button style={{marginTop: '50', padding: '10px'}} onClick={sendMessage}>
        Send message
      </button>
    </div>
  );
}

export default App;

使用 GatsbyJS 组件(不工作)

import React, { useEffect } from 'react'
import './Chat.scss'
import { Button } from '@material-ui/core'
import socketIOClient from 'socket.io-client'

const socketEndPoint = 'http://localhost:32'
const sala = 'public_chat'
const salaSocket = socketIOClient(socketEndPoint + sala)
const messageKey = 'test_message'

function Chat() {
    function initSocket() {
        salaSocket.on(messageKey, received => alert('Message received: '+ received))
    }
    function sendMessage() {
        salaSocket.emit(messageKey, `Send a message to server`)
    }
    useEffect(() => {
        initSocket()
    }, [])
    return(
        <div className='chat'>
            <Button onClick={sendMessage}>send</Button>
        </div>
    )
}

export default Chat

如您所见,两者都有相同的脚本或有关套接字 io 使用的相同逻辑,但我不知道为什么在 React 中有效而 gatsbyjs 无效。也许是一个错误或者是什么问题?

服务器(没问题)

const express = require('express')
const http = require('http')
const socketIo = require('socket.io')
const PORT = process.env.PORT || 32
const app = express()

const server = http.createServer(app)
const io = socketIo(server, {origins: '*:*'})

app.get('/', (req, res) => res.sendFile(`${__dirname}/index.html`))
server.listen(PORT, () => console.log(`Socket runing at port: ${PORT}`))

const sala = io.of('public_chat')
const messageKey = 'test_message'
sala.on('connection', socket => {
    console.log('New user connected', socket.id)
    socket.on(messageKey, data => {
        console.log('received: ', data)
        sala.emit(messageKey, data)
    })
})

感谢您的支持:)

最佳答案

我解决了这个问题,问题是客户端的软件包版本我更改为“2.2”,因为之前的“2​​.3”不符合我的逻辑,它现在可以工作:D

关于javascript - GatsbyJS 中的 Socket IO 客户端不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014205/

相关文章:

javascript - 替换文本区域中的一个特定单词

javascript - d3.js 文本旋转时消失

javascript - 映射数学和 Javascript

javascript - React-Native 将 var 添加到 View 标签中

javascript - 在jade中一键点击运行两个javascript函数

javascript - DynamoDB Javascript – 按主键和范围键数组查询?

javascript - nodejs 中模块 require 的奇怪行为

javascript - axios 的包装函数

reactjs - 使用Semantic UI React,如何设置图像头像的宽度和高度?

ruby-on-rails - React with Rails 5.1 将数据库对象调用到 React 组件中