您的当前位置:首页正文

PythonWebSocket长连接心跳与短连接的示例

来源:个人技术集锦
PythonWebSocket长连接⼼跳与短连接的⽰例

安装

pip install websocket-client

先来看⼀下,长连接调⽤⽅式:

ws = websocket.WebSocketApp(\"ws://echo.websocket.org/\ on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever()

长连接,参数介绍:(1)url: websocket的地址。

(2)header: 客户发送websocket握⼿请求的请求头,{'head1:value1','head2:value2'}。

(3)on_open:在建⽴Websocket握⼿时调⽤的可调⽤对象,这个⽅法只有⼀个参数,就是该类本⾝。

(4)on_message:这个对象在接收到服务器返回的消息时调⽤。有两个参数,⼀个是该类本⾝,⼀个是我们从服务器获取的字符串(utf-8格式)。

(5)on_error:这个对象在遇到错误时调⽤,有两个参数,第⼀个是该类本⾝,第⼆个是异常对象。(6)on_close:在遇到连接关闭的情况时调⽤,参数只有⼀个,就是该类本⾝。

(7)on_cont_message:这个对象在接收到连续帧数据时被调⽤,有三个参数,分别是:类本⾝,从服务器接受的字符串(utf-8),连续标志。

(8)on_data:当从服务器接收到消息时被调⽤,有四个参数,分别是:该类本⾝,接收到的字符串(utf-8),数据类型,连续标志。

(9)keep_running:⼀个⼆进制的标志位,如果为True,这个app的主循环将持续运⾏,默认值为True。(10)get_mask_key:⽤于产⽣⼀个掩码。

(11)subprotocols:⼀组可⽤的⼦协议,默认为空。

长连接关键⽅法:ws.run_forever(ping_interval=60,ping_timeout=5)

如果不断开关闭websocket连接,会⼀直阻塞下去。另外这个函数带两个参数,如果传的话,启动⼼跳包发送。ping_interval:⾃动发送“ping”命令,每个指定的时间(秒),如果设置为0,则不会⾃动发送。ping_timeout:如果没有收到pong消息,则为超时(秒)。

ws.run_forever(ping_interval=60,ping_timeout=5)#ping_interval⼼跳发送间隔时间#ping_timeout 设置,发送ping到收到pong的超时时间

我们看源代码,会发现这样⼀断代码:ping的超时时间,要⼤于ping间隔时间

if not ping_timeout or ping_timeout <= 0: ping_timeout = None

if ping_timeout and ping_interval and ping_interval <= ping_timeout: raise WebSocketException(\"Ensure ping_interval > ping_timeout\")

长连接:⽰例1:

import websockettry:

import thread

except ImportError:

import _thread as thread

import time

def on_message(ws, message): print(message)def on_error(ws, error): print(error)

def on_close(ws):

print(\"### closed ###\")

def on_open(ws): def run(*args):

ws.send(\"hello1\") time.sleep(1) ws.close()

thread.start_new_thread(run,())

if __name__ == \"__main__\": websocket.enableTrace(True)

ws = websocket.WebSocketApp(\"ws://echo.websocket.org/\ on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open

ws.run_forever(ping_interval=60,ping_timeout=5)

⽰例2:

import websocket

from threading import Threadimport timeimport sys

class MyApp(websocket.WebSocketApp): def on_message(self, message): print(message) def on_error(self, error): print(error)

def on_close(self):

print(\"### closed ###\")

def on_open(self): def run(*args): for i in range(3):

# send the message, then wait # so thread doesn't exit and socket # isn't closed

self.send(\"Hello %d\" % i) time.sleep(1)

time.sleep(1) self.close()

print(\"Thread terminating...\") Thread(target=run).start()

if __name__ == \"__main__\": websocket.enableTrace(True) if len(sys.argv) < 2:

host = \"ws://echo.websocket.org/\" else:

host = sys.argv[1] ws = MyApp(host) ws.run_forever()

短连接:

from websocket import create_connection

ws = create_connection(\"ws://echo.websocket.org/\")print(\"Sending 'Hello, World'...\")

ws.send(\"Hello, World\")print(\"Sent\")

print(\"Receiving...\")result = ws.recv()

print(\"Received '%s'\" % result)ws.close()

以上就是Python WebSocket长连接⼼跳与短连接的⽰例的详细内容,更多关于Python WebSocket连接的资料请关注其它相关⽂章!

因篇幅问题不能全部显示,请点此查看更多更全内容