首页 Node.js核心模块手册

参数说明


on(event, callback)
event         事件名称
callback(     回调函数
  e           
)      

事件列表


事件 示例 描述
data socket.on("data", function(data){})

服务器接收到客户端数据时触发,数据是Buffer类型,需用toString()转换,或在使用socket.setEncoding("utf8")指定

end socket.on("end", function(){}) 客户端连接关闭时触发
timeout socket.on("timeout", function(){}) socket.setTimeout 触发,定时函数
close socket.on("close", function(bol){})

错误导致socket端口关闭时触发

bol值:

true 由错误引起关闭

false 正常关闭

drain socket.on("drain", function(){}) 缓存区数据全部发送完毕触发
error

socket.on("error", function(e){})

参数:e.code 错误编码

通讯中客户端意外断开连接