python tornado websocket ping_Python websockets发送ping fram

如果客户机和服务器都使用同一个库,PING和PONG帧不会自动发送,当一方要检查另一方是否在线时,它通过调用用户的PING()方法发送PING帧,另一方通过内部调用PONG()方法自动回复PING帧,所以我们不需要关心传入的PING我们自己帧并调用pong()。(可以将pong()看作一个私有函数。)def read_data_frame(self, max_size):

"""

Read a single data frame from the connection.

Process control frames received before the next data frame.

Return ``None`` if a close frame is encountered before any data frame.

"""

# 6.2. Receiving Data

while True:

frame = yield from self.read_frame(max_size)

# 5.5. Control Frames

if frame.opcode == OP_CLOSE:

# 7.1.5. The WebSocket Connection Close Code

# 7.1.6. The WebSocket Connection Close Reason

self.close_code, self.close_reason = parse_close(frame.data)

# Echo the original data instead of re-serializing it with

# serialize_close() because that fails when the close frame is

# empty and parse_close() synthetizes a 1005 close code.

yield from self.write_close_frame(frame.data)

return

elif frame.opcode == OP_PING:

# Answer pings.

# Replace by frame.data.hex() when dropping Python < 3.5.

ping_hex = binascii.hexlify(frame.data).decode() or '[empty]'

logger.debug("%s - received ping, sending pong: %s",

self.side, ping_hex)

yield from self.pong(frame.data)

elif frame.opcode == OP_PONG:

...

{a1}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值