关闭服务器连接
你可以在事件处理程序中的任何时候调用 server.close() 来关闭原始服务器连接。
¥You can close the original server connection by calling server.close() at any point in your event handler.
api.addEventListener('connection', ({ client, server }) => {
server.connect()
client.addEventListener('message', (event) => {
if (event.data === 'hello world') {
event.preventDefault()
server.close()
client.send('hello from mock')
}
})
})在上面的例子中,一旦客户端向
'hello world'发送了一条短信,我们就会关闭服务器连接。此外,我们通过调用event.preventDefault()来阻止该消息被转发,而是向客户端'hello from mock'返回一个模拟消息。¥In the example above, we are closing the server connection once the client sends a text message
'hello world'. Additionally, we prevent that message from being forwarded by callingevent.preventDefault()and instead send back a mock message to the client'hello from mock'.
当底层客户端连接关闭时,原始服务器连接将自动关闭。
¥The original server connection will be closed automatically when the underlying client connection closes.
API 参考
¥API reference
server.close()
The `server.close()` API reference.