简体   繁体   English

socket.io连接问题

[英]socket.io connect issue

I keep getting this GET /socket.io/?EIO=3&transport=polling&t=MfRfeJD 404 4.438 ms - 149 error and I don't know where it's coming from. 我不断收到此GET /socket.io/?EIO=3&transport=polling&t=MfRfeJD 404 4.438 ms-149错误,我不知道它从哪里来。

I'm trying to integrate a live chat into my application using react, socket.io and express and I keep getting this not found error with the sockets. 我正在尝试使用react,socket.io和express将实时聊天集成到我的应用程序中,而我一直在使用套接字发现此未找到的错误。 I'm not sure if the problem is on the client or server side. 我不确定问题出在客户端还是服务器端。 It appears to be trying to continuously poll the server, but is getting 404's back. 似乎正在尝试不断轮询服务器,但又找回了404。 That sounds like socket.io isn't running, but it all looks okay to me. 听起来好像socket.io没有运行,但对我而言一切正常。 It may also have something to do with paths, but I don't really know. 它也可能与路径有关,但我真的不知道。 I've tried adding different route to the io like " http://localhost:5000/ " but still it still can't find the socket. 我尝试过向io添加不同的路由,例如“ http:// localhost:5000 / ”,但仍然找不到套接字。

I get the page to show up and when I click send the message shows up but I can't get the sockets to connect. 我看到要显示的页面,当我单击“发送”时,出现了消息,但我无法连接套接字。

In app.js 在app.js中

    const express = require('express');

  const http = require('http')
    const bodyParser = require('body-parser')
    const socketIo = require('socket.io')

    var app = express();
    const server = http.createServer(app)
    const io = socketIo(server)

    var PORT = process.env.PORT || 5000;

    app.post('/', (req, res) => {
      const { Body, From} = req.body
      const message = {
        body: Body,
        from: From.slice(8),

      }
      io.emit('message', message)
      res.send(`
               <Response>
                <Message>Thanks for texting!</Message>
               </Response>
               `)
    })

    io.on('connection', socket => {
      socket.on('message', body => {
        socket.broadcast.emit('message', {
          body,
          from: socket.id.slice(8)
        })
      })
    })

    server.listen(PORT);

In Chat.js 在Chat.js中

    import React from "react";
    import io from "socket.io-client";



    class Chat extends React.Component {
        constructor (props) {
          super(props)
          this.state = { messages: [] }
        }

        componentDidMount () {
          this.socket = io('http://localhost:5000/')
          this.socket.on('message', message => {
            this.setState({ messages: [message, ...this.state.messages] })
          })
        }

        handleSubmit = event => {
          const body = event.target.value
          if (event.keyCode === 13 && body) {
            const message = {
              body,
              from: 'Me'
            }
            this.setState({ messages: [message, ...this.state.messages] })
            this.socket.emit('message', body)
            event.target.value = ''
          }
        }

        render () {
          const messages = this.state.messages.map((message, index) => {
            return <li key={index}><b>{message.from}:</b>{message.body} </li>
          })
          return (
            <div>
               <h1>Admin Chat</h1>
              <input type='text' placeholder='Enter a message...' onKeyUp={this.handleSubmit} />
              {messages}
            </div>
          )
        }
      }



export default Chat;

404 is clearly saying no such page 404明确说没有这样的页面

149 will be the line number of the failure, your importing other code so it can be on any of the other code that the line 149 exists 149将是失败的行号,您将导入其他代码,因此它可以在第149行存在的任何其他代码上

i do see a maybe in app.js and the path 
 "app.post('/', (req, res) => {"        Refers to an absolute path
     try changing "http://localhost:5000/" 
       to "http://localhost:5000" or "http://localhost/:5000"

it looks like the "/" on the end puts the 5000 in the path not the port 它看起来像最后的“ /”将5000放在路径而不是端口中

--- EDIT --- on closer look at GET /socket.io/?EIO=3&transport=polling&t=MfRfeJD ---编辑---仔细查看GET /socket.io/?EIO=3&transport=polling&t=MfRfeJD

if chat.js is running on the client and connecting to http://localhost:5000 than; 如果chat.js在客户端上运行并连接到http:// localhost:5000 ,则;

http://localhost/socket.io/?EIO=3&transport=polling&t=MfRfeJD would be the attempted connection http://localhost/socket.io/?EIO = 3&transport = polling&t = MfRfeJD将是尝试的连接

it looks like client is trying to connect back to itself. 看来客户端正在尝试重新连接到自身。

how do you have the client / server setup? 您如何进行客户端/服务器设置?

if they are separate machines this would be looking for a non existing url. 如果它们是单独的计算机,则将查找不存在的URL。

either way is happening in the socket.io library. 这两种方式都在socket.io库中发生。

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

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