淘先锋技术网

首页 1 2 3 4 5 6 7

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')

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>

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

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;
    }