mirror of
https://github.com/hanxi/xiaomusic.git
synced 2026-03-25 09:49:46 +08:00
feat: 加入了遥控网页播放,用于实现电视上使用 (#395)
* Create httppost.py for 3thplay,提交json到网页播放接口,参数地址和json * Update httppost.py * Update utils.py 用于3thplay.html的提交socketio,消息的函数 包含post主体, * Update httppost.py 将不再使用,移动到util * Update xiaomusic.py 加入新设备的播放功能 有3方设备打开 /static/3thplay.html 通过socketio连接返回true 忽律小爱音箱的播放,设备可以是手机平板笔记本等的浏览器 * Update utils.py * Update xiaomusic.py int_config thdplay -加入三方播放参数 * 主网页,用于其他设备播放 里面在按钮是特殊字符,无法在线编辑 * 手机浏览器初始化时,页面需要手工点击播放,必须有一段音频-结合3thplay使用 * Delete xiaomusic/static/(示例音乐)一剪梅.mp3 * 启动音频初始化窗口,手工点击播放 * Update 3thplay.html * Update 3thplay.html * Update utils.py * Update utils.py * Update utils.py * Update utils.py * Update xiaomusic.py 设置3thplay.html播放器的音量 * Update pyproject.toml 加入socketio * Update httpserver.py for socketio * Update cli.py change app to sockeio_app * Update xiaomusic.py set 3thd target to hostname:public 参数跳转到本机.socket 接收请求的地址 * Update 3thplay.html io "/"
This commit is contained in:
@@ -9,7 +9,7 @@ import urllib.parse
|
||||
from contextlib import asynccontextmanager
|
||||
from dataclasses import asdict
|
||||
from typing import TYPE_CHECKING, Annotated
|
||||
|
||||
import socketio
|
||||
if TYPE_CHECKING:
|
||||
from xiaomusic.xiaomusic import XiaoMusic
|
||||
|
||||
@@ -55,7 +55,13 @@ from xiaomusic.utils import (
|
||||
xiaomusic: "XiaoMusic" = None
|
||||
config = None
|
||||
log = None
|
||||
|
||||
from pydantic import BaseModel
|
||||
# 3thplay指令
|
||||
class Item(BaseModel):
|
||||
action:str
|
||||
args:str
|
||||
# 在线用户
|
||||
onlines= set()
|
||||
|
||||
@asynccontextmanager
|
||||
async def app_lifespan(app):
|
||||
@@ -104,6 +110,42 @@ app = FastAPI(
|
||||
openapi_url=None,
|
||||
)
|
||||
|
||||
# 创建Socket.IO实例
|
||||
sio = socketio.AsyncServer(
|
||||
async_mode='asgi',
|
||||
cors_allowed_origins='*' # 允许所有跨域请求,生产环境应限制
|
||||
)
|
||||
# 将Socket.IO挂载到FastAPI应用
|
||||
socketio_app = socketio.ASGIApp(
|
||||
socketio_server=sio,
|
||||
other_asgi_app=app,
|
||||
socketio_path='/socket.io'
|
||||
)
|
||||
|
||||
# Socket.IO事件处理
|
||||
@sio.event
|
||||
async def connect(sid, environ, auth):
|
||||
global onlines
|
||||
print(f'客户端连接: {sid}')
|
||||
onlines.update([sid])
|
||||
await sio.emit('message', {'data': '欢迎连接'}, room=sid)
|
||||
|
||||
@sio.event
|
||||
async def disconnect(sid):
|
||||
print(f'客户端断开: {sid}')
|
||||
onlines.discard(sid)
|
||||
|
||||
@sio.on("message")
|
||||
async def custom_event(sid, data):
|
||||
log.info(f'收到来自 {sid} 的数据: {data}')
|
||||
await sio.emit('response', {"action":'切歌','status':data})
|
||||
|
||||
@app.post("/items/")
|
||||
async def create_item(item: Item):
|
||||
result = {**item.dict()}
|
||||
await sio.emit('response',{"action":item.action,"args":item.args,"status":item.args},)
|
||||
return onlines
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"], # 允许访问的源
|
||||
@@ -135,11 +177,11 @@ class AuthStaticFiles(StaticFiles):
|
||||
|
||||
|
||||
def HttpInit(_xiaomusic):
|
||||
global xiaomusic, config, log
|
||||
global xiaomusic, config, log,onlines
|
||||
xiaomusic = _xiaomusic
|
||||
config = xiaomusic.config
|
||||
log = xiaomusic.log
|
||||
|
||||
onlines=set()
|
||||
folder = os.path.dirname(__file__)
|
||||
app.mount("/static", AuthStaticFiles(directory=f"{folder}/static"), name="static")
|
||||
reset_http_server()
|
||||
|
||||
Reference in New Issue
Block a user