最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在Koa中检测客户端断开连接

IT培训 admin 10浏览 0评论

在Koa中检测客户端断开连接

我的node.js Koa服务器如何在发送响应之前检测并记录客户端已断开连接。

通常顺序是:

  1. node.js koa服务器启动
  2. 客户端发送http请求
  3. node.js koa服务器花费一些时间来处理请求
  4. node.js koa服务器将响应发送到客户端

如果客户端在2完成之后但在4完成之前断开连接,node.js Koa可以检测并记录吗?

我已经使用这个简单的脚本进行了测试,并从另一个终端运行curl,然后在node.js进入睡眠状态的10秒钟延迟期间,我终止(ctrl-c)curl命令。

const Koa = require('koa');
const app = new Koa();

/**
 * synchronously delay/sleep/block for time ms
 */
const delay = time => new Promise(res=> {
    console.log(`About to sleep for ${time} ms`)
    setTimeout(res,time)
});

app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
});

app.use(async ctx => {
  ctx.req.on('close', () => {
    console.log('Request closed');
  });

  console.log('Hello World Started')

  await delay(10000)

  console.log('Hello World Ended');
  ctx.body = 'Hello World !!!';

});

app.listen(3000, () => console.log('running on port 3000'));

ctx.req.on('close'在两种情况下都会一次发出事件:

  1. 客户端在发送响应之前断开连接。
  2. 服务器响应仍在连接的客户端,等待响应。

我正在使用:

node --version
v13.8.0

这里讨论不同版本的节点何时发出req.on('close')事件:。

假设没有特定的“客户端在您发送响应之前已断开连接”事件,什么是最好的模式,通常可以检测到这种情况,所以我可以将其记录下来。

回答如下:

我们可以使用变量进行评估。当客户端在请求处理完成之前关闭时,下面提到的代码记录错误消息。这是一个肮脏的修复程序,但是它可以工作

const Koa = require('koa');
const app = new Koa();

/**
 * synchronously delay/sleep/block for time ms
 */
const delay = time => new Promise(res=> {
    console.log(`About to sleep for ${time} ms`)
    setTimeout(res,time)
});

app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
});

app.use(async ctx => {
  let requestProcessingCompleted = false    
  ctx.req.on('close', () => {
    console.log('Request closed');
    if(!requestProcessingCompleted){
        console.log("Client connection closed before request processing completed")
    }
  });

  console.log('Hello World Started')

  await delay(10000)

  console.log('Hello World Ended');
  ctx.body = 'Hello World !!!';
  requestProcessingCompleted = true
});

app.listen(3000, () => console.log('running on port 3000'));

在Koa中检测客户端断开连接

我的node.js Koa服务器如何在发送响应之前检测并记录客户端已断开连接。

通常顺序是:

  1. node.js koa服务器启动
  2. 客户端发送http请求
  3. node.js koa服务器花费一些时间来处理请求
  4. node.js koa服务器将响应发送到客户端

如果客户端在2完成之后但在4完成之前断开连接,node.js Koa可以检测并记录吗?

我已经使用这个简单的脚本进行了测试,并从另一个终端运行curl,然后在node.js进入睡眠状态的10秒钟延迟期间,我终止(ctrl-c)curl命令。

const Koa = require('koa');
const app = new Koa();

/**
 * synchronously delay/sleep/block for time ms
 */
const delay = time => new Promise(res=> {
    console.log(`About to sleep for ${time} ms`)
    setTimeout(res,time)
});

app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
});

app.use(async ctx => {
  ctx.req.on('close', () => {
    console.log('Request closed');
  });

  console.log('Hello World Started')

  await delay(10000)

  console.log('Hello World Ended');
  ctx.body = 'Hello World !!!';

});

app.listen(3000, () => console.log('running on port 3000'));

ctx.req.on('close'在两种情况下都会一次发出事件:

  1. 客户端在发送响应之前断开连接。
  2. 服务器响应仍在连接的客户端,等待响应。

我正在使用:

node --version
v13.8.0

这里讨论不同版本的节点何时发出req.on('close')事件:。

假设没有特定的“客户端在您发送响应之前已断开连接”事件,什么是最好的模式,通常可以检测到这种情况,所以我可以将其记录下来。

回答如下:

我们可以使用变量进行评估。当客户端在请求处理完成之前关闭时,下面提到的代码记录错误消息。这是一个肮脏的修复程序,但是它可以工作

const Koa = require('koa');
const app = new Koa();

/**
 * synchronously delay/sleep/block for time ms
 */
const delay = time => new Promise(res=> {
    console.log(`About to sleep for ${time} ms`)
    setTimeout(res,time)
});

app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
});

app.use(async ctx => {
  let requestProcessingCompleted = false    
  ctx.req.on('close', () => {
    console.log('Request closed');
    if(!requestProcessingCompleted){
        console.log("Client connection closed before request processing completed")
    }
  });

  console.log('Hello World Started')

  await delay(10000)

  console.log('Hello World Ended');
  ctx.body = 'Hello World !!!';
  requestProcessingCompleted = true
});

app.listen(3000, () => console.log('running on port 3000'));

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论