我使用ws来实现我的websocket服务器,在api文档中我发现它有一个ping / pong事件,但是,我无法收到该事件。
服务器:
wss.on("connection", function connection(ws) {
ws.on('message', function(message) {
console.log('received: %s', message);
});
ws.onclose = function () {
console.log('client disappeared');
};
ws.ping('hello');//Send a ping
});
客户端:
<script>
var ws = new WebSocket('ws://localhost:8080');
function sendMesage() {
ws.send('hello');
}
ws.onopen = function () {
console.log('connection to server opened');
};
ws.onclose = function () {
console.log('closed');
}
ws.onping = function () {
console.log('ping');
};// expect to receive ping event
</script>