RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1445822
Accepted
g_0008
g_0008
Asked:2022-09-05 13:09:49 +0000 UTC2022-09-05 13:09:49 +0000 UTC 2022-09-05 13:09:49 +0000 UTC

无法连接到套接字服务器

  • 772

我正在实时(套接字)写一个绘图板(画布)!在一台笔记本电脑上,如果您同时以多个用户的身份登录以检查板子,则板子工作正常。如果我通过另一台连接到同一个 wi-fi 的笔记本电脑,那么另一台笔记本电脑将无法连接到服务器。显示错误 ERR_CONNECTION_REFUSED。你能建议我做错了什么吗?

服务器代码:

const express = require('express')
const app = express()
const socketIO = require('socket.io')
const http = require('http').createServer(app)
const io = socketIO(http, {
    cors: {
      origin: "*",
    },
  })

const port = 3000

io.on('connection', (socket) => {
    console.log(`${socket.id} has connectd`)

    socket.on('openRoom', (room) => {
      socket.join(room)

      socket.on('draw', (data) => {
          socket.broadcast.to(room).emit('data', { x: data.x, y: data.y, strokeWidth: data.strokeWidth, strokeColor: data.strokeColor })
      })
    })

    socket.on('disconnect', (socket) => console.log(`${socket.id} has disconnected`))
})

http.listen(port, () => {
  console.log(`server has been started on port ${port}`)
})

里面的一段客户端代码:

created () {
    this.socket = io('http://127.0.0.1:3000')
    this.socket.on('connect', () => {
      console.log('connected', this.socket)
      this.socket.emit('openRoom', roomNumb)
    })
  },
methods: {
    mousedown (e) {
      this.isDrawing = true
      const rect = this.canvas.getBoundingClientRect()
      const x = e.clientX - rect.left
      const y = e.clientY - rect.top
      this.startX = x
      this.startY = y
      this.context.beginPath()
      this.context.moveTo(this.startX, this.startY)
    },
    mousemove (e) {
      const rect = this.canvas.getBoundingClientRect()
      const x = e.clientX - rect.left
      const y = e.clientY - rect.top
      const strokeWidth = this.currentWidth
      const strokeColor = this.colorArray[this.currentColor]
      if (this.isDrawing) {
        this.socket.emit('draw', { x, y, strokeWidth, strokeColor })
        this.context.lineTo(x, y)
        this.context.lineWidth = strokeWidth
        // this.context.globalCompositeOperation = 'source-over'
        this.context.lineCap = 'round'
        this.context.strokeStyle = strokeColor
        this.context.stroke()
        this.startX = x
        this.startY = y
        this.points.push({
          x: x,
          y: y
        })
      }
    },
    mouseup (e) {
      this.allPoints.push(this.points)
      console.log(this.allPoints)
      this.isDrawing = false
      this.points = []
    },
    toGetPoints: function () {
      this.socket.on('data', ({ x, y, strokeWidth, strokeColor }) => {
        this.context.lineTo(x, y)
        this.context.lineWidth = strokeWidth
        this.context.lineCap = 'round'
        this.context.strokeStyle = strokeColor
        this.context.stroke()
      })
    }
  }
vue.js
  • 0 0 个回答
  • 0 Views

0 个回答

  • Voted
  1. Best Answer
    S.H.
    2022-09-05T13:25:09Z2022-09-05T13:25:09Z

    看看你是不是认真的。

    客户专线:

        this.socket = io('http://127.0.0.1:3000')
    

    这意味着客户端正在连接localhost,或者换句话说,在您的代码中它说“客户端和服务器必须在同一台计算机上”!

    如果您真的在客户端中准确地写了,那么这是第一个更正它的地方。

    尝试将服务器放在某台计算机上,然后在本地网络中查找其地址(例如,在 wifi 内)

    在 Windows 上,这是通过 command 完成的ipconfig,在 Linux 上 - 类似于ip a或ifconfig

    假设您已将运行服务器的计算机的地址定义为192.168.1.101. 尝试将此特定地址设置给客户端,然后客户端应该可以在多台计算机上正常工作。

    如果它“不起作用” - 你需要寻找原因,最有可能的原因是防火墙!

    总的来说,我认为开始就是这样。只要它在本地工作,就可以将服务器带到互联网上,然后你可以写另一个问题。

    • 1

相关问题

  • Vue.js 如何拖放表格内容

  • 如何完成切换并单击组?

  • 如何将内容加载到属于循环元素的块中?

  • 带有计数器的 VueJs 循环

  • Vue.js 如何制作导航箭头

  • 启动 npm serve 时抛出错误

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    我看不懂措辞

    • 1 个回答
  • Marko Smith

    请求的模块“del”不提供名为“default”的导出

    • 3 个回答
  • Marko Smith

    "!+tab" 在 HTML 的 vs 代码中不起作用

    • 5 个回答
  • Marko Smith

    我正在尝试解决“猜词”的问题。Python

    • 2 个回答
  • Marko Smith

    可以使用哪些命令将当前指针移动到指定的提交而不更改工作目录中的文件?

    • 1 个回答
  • Marko Smith

    Python解析野莓

    • 1 个回答
  • Marko Smith

    问题:“警告:检查最新版本的 pip 时出错。”

    • 2 个回答
  • Marko Smith

    帮助编写一个用值填充变量的循环。解决这个问题

    • 2 个回答
  • Marko Smith

    尽管依赖数组为空,但在渲染上调用了 2 次 useEffect

    • 2 个回答
  • Marko Smith

    数据不通过 Telegram.WebApp.sendData 发送

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5