日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python webscoket(Autobahn)的使用

發(fā)布時(shí)間:2023/12/29 python 49 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python webscoket(Autobahn)的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

自從18年4月份入職游戲公司之后很少有時(shí)間寫博客了!

今天來(lái)聊聊python的websocket的使用,在游戲行業(yè)游戲客戶端和服務(wù)端需要大量的,快速的通訊,這里面就會(huì)用到websocket

Autobahn 是一個(gè)高性能的websocket 它采用了兩種實(shí)現(xiàn)方案
1 基于Twisted
2 基于asyncio

Autobahn 有如下的特點(diǎn):

framework for WebSocket and WAMP clients (Websocket框架和WAMP框架)
compatible with Python 2.7 and 3.3+ (兼容python2.7和python3.3以上的版本)
runs on CPython, PyPy and Jython (可以運(yùn)行在Cpython, PyPy 和Jython 上面)
runs under Twisted and asyncio (可以選擇Twisted 或者是asyncio的方式來(lái)運(yùn)行)
implements WebSocket RFC6455 (and draft versions Hybi-10+) (實(shí)現(xiàn)WebSocket RFC6455(以及草案版本Hybi-10 +))
implements WebSocket compression (實(shí)現(xiàn)WebSocket壓縮)
implements WAMP, the Web Application Messaging Protocol (實(shí)現(xiàn)Web應(yīng)用程序消息傳遞協(xié)議)
supports TLS (secure WebSocket) and proxies (支持TLS(安全WebSocket)和代理)
Open-source (MIT license) (開(kāi)源)

Autobahn 可以用來(lái)做什么

Autobahn 非常適用于交易系統(tǒng),多人游戲, 實(shí)時(shí)聊天等應(yīng)用程序的開(kāi)發(fā)

一個(gè)簡(jiǎn)單的webSocket服務(wù)端程序

from autobahn.twisted.websocket import WebSocketServerProtocol # or: from autobahn.asyncio.websocket import WebSocketServerProtocolclass MyServerProtocol(WebSocketServerProtocol):def onConnect(self, request):print("Client connecting: {}".format(request.peer))def onOpen(self):print("WebSocket connection open.")def onMessage(self, payload, isBinary):if isBinary:print("Binary message received: {} bytes".format(len(payload)))else:print("Text message received: {}".format(payload.decode('utf8')))## echo back message verbatimself.sendMessage(payload, isBinary)def onClose(self, wasClean, code, reason):print("WebSocket connection closed: {}".format(reason))

幾個(gè)重要的方法:

onConnect: 當(dāng)有新的客戶端連接進(jìn)來(lái)的時(shí)候調(diào)用該方法
onOpen: 當(dāng)連接成功時(shí)調(diào)用該方法
onMessage: 該方法是接收來(lái)自客戶端的消息
onClose: 當(dāng)關(guān)閉時(shí)調(diào)用該方法

安裝Autobahn

Autobahn 的運(yùn)行依賴于 Twisted 和 asyncio 因此安裝Autobahn 需要確認(rèn)你的python支持了
Twisted 和 asyncio

各個(gè)python 版本對(duì) Twisted 和 asyncio 支持如下

使用pip 安裝

pip install autobahn[twisted]
pip install autobahn[asyncio]

驗(yàn)證是否安裝成功

from autobahn import version
print(version)
0.9.1

啟動(dòng)webScoketServer

twisted 版本

import sysfrom twisted.python import logfrom twisted.internet import reactorlog.startLogging(sys.stdout)from autobahn.twisted.websocket import WebSocketServerFactoryfactory = WebSocketServerFactory()factory.protocol = MyServerProtocolreactor.listenTCP(9000, factory)reactor.run()

asyncio 版本

try:import asyncioexcept ImportError:## Trollius >= 0.3 was renamedimport trollius as asynciofrom autobahn.asyncio.websocket import WebSocketServerFactoryfactory = WebSocketServerFactory()factory.protocol = MyServerProtocolloop = asyncio.get_event_loop()coro = loop.create_server(factory, '127.0.0.1', 9000)server = loop.run_until_complete(coro)try:loop.run_forever()except KeyboardInterrupt:passfinally:server.close()loop.close()

創(chuàng)建WebSocketClient

class MyClientProtocol(WebSocketClientProtocol):def onOpen(self):self.sendMessage(u"Hello, world!".encode('utf8'))def onMessage(self, payload, isBinary):if isBinary:print("Binary message received: {0} bytes".format(len(payload)))else:print("Text message received: {0}".format(payload.decode('utf8')))

WebSocketClientProtocol 可以使用:
autobahn.twisted.websocket.WebSocketClientProtocol
autobahn.asyncio.websocket.WebSocketClientProtocol

使用Client

Twisted版本

import sysfrom twisted.python import logfrom twisted.internet import reactorlog.startLogging(sys.stdout)from autobahn.twisted.websocket import WebSocketClientFactoryfactory = WebSocketClientFactory()factory.protocol = MyClientProtocolreactor.connectTCP("127.0.0.1", 9000, factory)reactor.run()

asyncio版本

try:import asyncioexcept ImportError:## Trollius >= 0.3 was renamedimport trollius as asynciofrom autobahn.asyncio.websocket import WebSocketClientFactoryfactory = WebSocketClientFactory()factory.protocol = MyClientProtocolloop = asyncio.get_event_loop()coro = loop.create_connection(factory, '127.0.0.1', 9000)loop.run_until_complete(coro)loop.run_forever()loop.close()

發(fā)送消息

當(dāng)我們的server或者client 繼承了autobahn之后就可以使用 sendMessage 方法來(lái)發(fā)送消息

接收消息

當(dāng)我們的server或者client 實(shí)現(xiàn)了onMessage(self, payload, isBinary): 定義了該方法之后,就可以接收到消息

更多詳細(xì)介紹請(qǐng)查看: https://autobahn.readthedocs.io/en/latest/index.html

總結(jié)

以上是生活随笔為你收集整理的python webscoket(Autobahn)的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。