鹏保宝播放器显示无法连接服务器,使用socket.io和nodejs强制客户端与服务器断开连接...

本文介绍了如何使用Socket.IO正确地断开客户端的连接。当客户端使用无效的access token连接到WebSocket服务器时,服务器会通知客户端关闭连接。通过调用`socket.disconnect(true)`可以强制关闭底层连接,而不仅仅是关闭客户端所连接的名字空间。这是一个具体的使用案例,展示了从服务器到客户端的通信流程,以及客户端如何确认并触发强制断开连接。
摘要由CSDN通过智能技术生成

This didn't work for me:

`socket.disconnect()`

This did work for me:

socket.disconnect(true)

Handing over true will close the underlaying connection to the client and not just the namespace the client is connected to Socket IO Documentation.

An example use case: Client did connect to web socket server with invalid access token (access token handed over to web socket server with connection params). Web socket server notifies the client that it is going to close the connection, because of his invalid access token:

// (1) the server code emits

socket.emit('invalidAccessToken', function(data) {

console.log(data); // (4) server receives 'invalidAccessTokenEmitReceived' from client

socket.disconnect(true); // (5) force disconnect client

});

// (2) the client code listens to event

// client.on('invalidAccessToken', (name, fn) => {

// // (3) the client ack emits to server

// fn('invalidAccessTokenEmitReceived');

// });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值