Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义样例onOnline onOffline没有正确运行 #8

Open
MHfudie opened this issue Jun 13, 2024 · 1 comment
Open

自定义样例onOnline onOffline没有正确运行 #8

MHfudie opened this issue Jun 13, 2024 · 1 comment

Comments

@MHfudie
Copy link

MHfudie commented Jun 13, 2024

自定义样例onOnline onOffline没有正确运行

@MHfudie
Copy link
Author

MHfudie commented Jun 13, 2024

`import { echo } from 'coa-echo'
import { _ } from 'coa-helper'
import { createServer, Server, Socket } from 'net'
import { CoaClient } from './CoaClient'
import { CoaClientPool } from './CoaClientPool'

export class CoaApplication {
private readonly listenPort: number
private readonly clientPool: CoaClientPool
private readonly tcpServer: Server

constructor(clientPool: CoaClientPool, listenPort: number) {
this.clientPool = clientPool
this.listenPort = listenPort
this.tcpServer = createServer((socket) => this.connectionListener(socket))
}

// 监听
start() {
this.tcpServer.listen(this.listenPort, () => {
echo.cyan('[TCP] Listening on port ' + this.listenPort)
})
}

// 监听连接事件
private connectionListener(socket: Socket) {
let client: T
// 只有首次收到数据,才开始初始化一个客户端

socket.on('data', async (raw) => {
  if (client == undefined) {
    client = await this.clientPool.connect(socket);
    await client.onOnline(client!.clientId);
  }
  await client.onData(raw);
})
// 关闭连接的时候销毁数据
socket.on('close', async () => {
  if (client) await this.clientPool.close(client)
  await client.onOffline(client.clientId);
})

socket.on('error', _.noop)
// 如果超过300秒没有任何响应,则断开连接
socket.on('timeout', () => socket.end(() => socket.destroy()))
socket.setTimeout(300 * 1000)

}
}
`

这样似乎就成功修复了问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant