当前位置:   article > 正文

flask + uwsgi + flask-socketio + nginx部署_uwsgi flask_socket

uwsgi flask_socket

python代码

from flask import Flask
from flask_socketio import SocketIO, emit
import threading

app = Flask(__name__)
Socket = SocketIO(app, cors_allowed_origins="*")

video_lock = threading.Lock()
run_thread = None


#连接成功时触发
@Socket.on('connect', namespace='/videos')
def connected_msg():
    print("连接成功")
    print('client connected.')


#断开连接时触发
@Socket.on('disconnect', namespace='/videos')
def disconnect_msg():
    print('client disconnected.')


# 自定义事件
@Socket.on('video', namespace='/videos')
def videos(v):
    emit('get_video', '1', namespace='/videos')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

html代码

<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js" integrity="sha512-q/dWJ3kcmjBLU4Qc47E4A9kTB4m3wuTY7vkFJDTZKjTs8jhyGQnaUrxa0Ytd0ssMZhbNua9hE+E7Qv1j+DyZwA==" crossorigin="anonymous"></script>
<script type="text/javascript" charset="utf-8">
    var socket = io('http://127.0.0.1:20000/videos');
    socket.on('connect', function() {  # 连接成功后发功信息給自定义事件
			socket.emit('video', {data: 'I\'m connected!'});
			
    });
	socket.on('get_video', (data)=>{  # 监听get_video
		console.log('13132');
		console.log(data);
	});
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

uwsgi.ini 配置

[uwsgi]
home = /python/yolov5/venv/
http = 127.0.0.1:8080
chdir = /python/yolov5/
wsgi-file = flask_yolov5.py
callable = app
chmod-socket = 777
master = true
disable-logging = false
daemonize = /tmp/mylog.log
http-websockets=true
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

nginx配置文件

    listen       20000;
        server_name  192.168.3.251;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        limit_rate   1000k;
        location / {
            add_header Access-Control-Allow-Origin *; #这里可换成页面的域名
            add_header Access-Control-Allow-Headers X-Requested-With;
            add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
            uwsgi_send_timeout 600;        # 指定向uWSGI传送请求的超时时间,完成握手后向uWSGI传送请求的超时时间。
            uwsgi_connect_timeout 600;   # 指定连接到后端uWSGI的超时时间。
            uwsgi_read_timeout 600;
            root html;
            index  index.html index.htm;
        }

     location /socket.io/ {
        proxy_pass http://127.0.0.1:8080/socket.io/;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/532792
推荐阅读
相关标签
  

闽ICP备14008679号