美文网首页
测试Kong网关的websocket功能

测试Kong网关的websocket功能

作者: 印随2018 | 来源:发表于2020-04-21 19:56 被阅读0次

    一、在线安装docker

    yum install -y yum-utils device-mapper-persistent-data lvm2
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    yum -y install docker-ce
    systemctl daemon-reload && systemctl restart docker
    

    二、安装python3以及依赖包

    yum install python36
    
    pip3.6 install websocket_client
    

    三、运行kong网关

    docker run -d --name kong \
        -e "KONG_DATABASE=off" \
        -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
        -p 8000:8000 \
        -p 8443:8443 \
        -p 8001:8001 \
        -p 8444:8444 \
        kong
    

    四、网关上注册websocket接口

    websocket server online: ws://123.207.136.134:9010/ajaxchattest

    cat > apis.yaml <<EOF
    _format_version: "1.1"
    services:
     - name: websocket-service
       url: http://123.207.136.134:9010
       routes:
       - name: websocket-route
         paths:
         - /
    EOF
    

    curl -F "config=@apis.yaml" http://127.0.0.1:8001/config

    五、客户端测试

    其中127.0.0.1:8000是Kong网关的数据口

    cat > client.py <<EOF
    #!/usr/bin/env python3
    
    from websocket import create_connection
    
    ws = create_connection("ws://127.0.0.1:8000/ajaxchattest")
    print("Sending 'Hello, World 1'...")
    ws.send("Hello, World 1")
    print("Sent")
    print("Receiving...")
    result =  ws.recv()
    print("Received '%s'" % result)
    
    print("Sending 'Hello, World 2'...")
    ws.send("Hello, World 2")
    print("Sent")
    print("Receiving...")
    result =  ws.recv()
    print("Received '%s'" % result)
    
    print("Sending 'Hello, World 3'...")
    ws.send("Hello, World 3")
    print("Sent")
    print("Receiving...")
    result =  ws.recv()
    print("Received '%s'" % result)
    
    ws.close()
    EOF
    
    chmod +x client.py
    
    ./client.py
    

    相关文章

      网友评论

          本文标题:测试Kong网关的websocket功能

          本文链接:https://www.haomeiwen.com/subject/ujopihtx.html