python websocket stomp作为客户端

= =!关于这些的资料真的少、我写一篇参考
(我也参考了github上一个前辈的代码https://github.com/akshayeshenoi/StompWS)前辈、如果介意请联系我、我立马删除
stomp是个协议、具体啥协议自己查。
stomp命令:
CONNECT
SEND
SUBSCRIBE
UNSUBSCRIBE
BEGIN
COMMIT
ABORT
ACK
NACK
DISCONNECT
websocket是个啥自己查
前提条件:服务器是webscoket、stomp协议的
实现思路:连接好websocket、
send:自己再把符合stomp协议的string 用websocket发出去
recv:while true websocket.recv()收到的东西用stomp进行解析
封装stomp报文的方法

BYTE = {
    'LF': '\x0A',
    'NULL': '\x00'
}
def _transmit(command, headers, msg=None):
    """
    Marshalls and transmits the frame
    """
    # Contruct the frame

    lines = []
    lines.append(command + BYTE['LF'])

    # add headers
    for key in headers:
        lines.append(key + ":" + headers[key] + BYTE['LF'])

    lines.append(BYTE['LF'])

    # add message, if any
    if msg is not None:
        lines.append(msg)

    # terminate with null octet
    lines.append(BYTE['NULL'])

    frame = ''.join(lines)

    # transmit over ws

    return frame

ws是webscoket连接实例
stomp–> conn

def conn_aps_stomp(ws, url):
    headers = {}
    headers['host'] = url
    headers['accept-version'] = '1.2'
    headers['heart-beat'] = '0,0'
    ws.send(_transmit('CONNECT', headers))

stomp–>subscribe

def subscribe_aps_stomp(ws, destination, user_id):
    """
    :param ws:
    :param destination: str
    :param user_id: str
    :return:
    """
    headers = {}
    headers['id'] = user_id
    headers['ack'] = 'client'
    headers['destination'] = destination
    ws.send(_transmit('SUBSCRIBE', headers))

stomp–>send message

def send(ws,destination, message):
    """
    Transmit a SEND frame
    """
    headers = {}

    headers['destination'] = destination
    headers['content-length'] = str(len(message))

    ws.send(_transmit('SEND', headers, msg=message))

stomp–>解析函数

def _parse_message(frame):
    """
    Returns:
        command
        headers
        body

    Args:
        frame: raw frame string
    """
    print fs_constant.FS_LOG_DEBUG + "will parse :" + frame
    lines = frame.split(BYTE['LF'])
    print fs_constant.FS_LOG_DEBUG + str(lines)
    command = lines[0].strip()
    headers = {}

    # get all headers
    i = 1
    if i >= len(lines):
        print  fs_constant.FS_LOG_WARN + "data is 0 size:-|" + str(lines)
        return command, headers, ''
    while lines[i] != '':
        # get key, value from raw header
        (key, value) = lines[i].split(':')
        headers[key] = value
        i += 1
        print fs_constant.FS_LOG_DEBUG + lines[i]
        if i >= len(lines):
            print "break parse----------->>>>>>>>>>"
            break

    # set body to None if there is no body
    body = None if lines[i + 1] == BYTE['NULL'] else lines[i + 1]

    return command, headers, body

处理返回
while True:
try:
info = ws.recv()
command, head, body = _parse_message(info)
# print command
# print head
# print body
# command还有好多、- -!根绝需要自行处理
if command == “MESSAGE”:
pass

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值