You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron中TCP Socket阻塞问题:是否源于渲染进程异常?

Electron渲染进程中TCP Socket阻塞问题

在Electron模板中使用TCP Socket时出现阻塞情况,怀疑该问题与Electron渲染进程有关。

依赖版本

"electron": "^2.0.4",
"electron-builder": "^20.19.2",
"electron-debug": "^1.5.0",
"electron-devtools-installer": "^2.2.4",

Socket测试代码

var net = require('net')
const ctime = new Date().getTime()
console.log(ctime)
this.client = new net.Socket()
this.client = net.connect({ port: 29999, host: '127.0.0.1' }, function() {
  console.log('Connected:')
  console.log(' local= %s:%s', this.localAddress, this.localPort)
  console.log(' remote= %s:%s', this._remoteAddress, this._remoteAddress)
  this.setTimeout(500)
  this.setEncoding('utf8')
  this.on('data', function(data) {
    console.log(' From Server:' + data.toString())
    this.end()
  })
  this.on('end', function() {
    console.log('Client disconnection')
  })
  this.on('error', function(err) {
    console.log('Socket Error:' + JSON.stringify(err))
  })
  this.on('timeout', function() {
    console.log('Socket Timed Out')
  })
  this.on('close', function() {
    console.log('Socket Closed')
  })
})
console.log('连接成功')
this.client.write('aaa')

问题现象

  1. 启动上述函数时,程序出现阻塞:
    程序阻塞截图

  2. 当页面渲染一个表格时,Socket能成功连接并打印相关信息:
    Socket连接成功截图

  3. 清空页面后,使用以下仅含测试按钮的组件代码,点击测试按钮时程序仍然阻塞:

<template>
  <div>
    <el-button type="success" round @click="test">测试</el-button>
  </div>
</template>

<script>

var net = require('net')

export default {
  methods: {
    test() {

      const ctime = new Date().getTime()
      console.log(ctime)
      this.client = new net.Socket()
      this.client = net.connect({ port: 29999, host: '127.0.0.1' }, function() {
        console.log('Connected:')
        console.log(' local= %s:%s', this.localAddress, this.localPort)
        this.setTimeout(500)
        this.setEncoding('utf8')
        this.on('data', function(data) {
          console.log(' From Server:' + data.toString())
          this.end()
        })
        this.on('end', function() {
          console.log('Client disconnection')
        })
        this.on('error', function(err) {
          console.log('Socket Error:' + JSON.stringify(err))
        })
        this.on('timeout', function() {
          console.log('Socket Timed Out')
        })
        this.on('close', function() {
          console.log('Socket Closed')
        })
      })
      console.log('连接成功')
      this.client.write('aaa')
    }
  }
}
</script>

内容的提问来源于stack exchange,提问作者QaQ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 11:37:02